Question: for the code listed below in c++ how would code for a heap sort be implemented? I need a heap sort for comparison with the
for the code listed below in c++ how would code for a heap sort be implemented? I need a heap sort for comparison with the bubble sort
#include #include #include #include
using namespace std;
long length = 1000; const long max_length = 30000;
int list[max_length];
void read() { ifstream fin("random.dat", ios::binary); for (long i = 0; i < length; i++) { fin.read((char*)&list[i], sizeof(int)); } fin.close(); }
void bubbleSort() { int temp; for(long i = 0; i < length; i++) { for(long j = 0; j < length-i-1; j++) { if (list[j] > list[j+1]) { temp = list[j]; list[j] = list[j+1]; list[j+1] = temp; } } } }
long partition(long left, long right) { int pivot_element = list[left]; int lb = left, ub = right; int temp;
while (left < right) { while(list[left] <= pivot_element) left++; while(list[right] > pivot_element) right--; if (left < right) { temp = list[left]; list[left] = list[right]; list[right] = temp; } } list[lb] = list[right]; list[right] = pivot_element; return right; }
int main() { double t1, t2;
for (length = 1000; length <= max_length; ) { cout << " Length\t: " << length << ' ';
read(); t1 = clock(); bubbleSort(); t2 = clock(); cout << "Bubble Sort\t: " << (t2 - t1)/CLK_TCK << " sec ";
switch (length) { case 1000 : length = 5000; break; case 5000 : length = 10000; break; case 10000 : length = 15000; break; case 15000 : length = 20000; break; case 20000 : length = 25000; break; case 25000 : length = 30000; break; case 30000 : length = 30001; break; } } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
