Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

ive posted the question different times but no one seems to fully explain my question, okay this code is implemented with a quicksort and thats

ive posted the question different times but no one seems to fully explain my question, okay this code is implemented with a quicksort and thats not what i need, but what i want to do is get rid of that quicksort and instead implement a heap sort, I need a HEAP SORT for comparison with the other sorts, what would be the code necessary for the heap sort instead of where the quicksort is.

for the code listed below in c++, how would a heapsort be implemented to replace the quicksort in the code

#include #include #include #include using namespace std; long length = 1000; const long max_length = 300000; 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; } } } } void insertionSort() { int temp; for(long i = 1; i < length; i++) { temp = list[i]; long j; for(j = i-1; j >= 0 && list[j] > temp; j--) { list[j+1] = list[j]; } 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; } void quickSort(long left, long right) { if (left < right) { long pivot = partition(left, right); quickSort(left, pivot-1); quickSort(pivot+1, 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 "; read(); t1 = clock(); insertionSort(); t2 = clock(); cout << "Insertion Sort\t: " << (t2 - t1)/CLK_TCK << " sec "; read(); t1 = clock(); quickSort(0, length - 1); t2 = clock(); cout << "Quick Sort\t: " << (t2 - t1)/CLK_TCK << " sec "; switch (length) { case 1000 : length = 5000; break; case 5000 : length = 10000; break; case 10000 : length = 50000; break; case 50000 : length = 100000; break; case 100000 : length = 200000; break; case 200000 : length = 300000; break; case 300000 : length = 300001; break; } } return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning C# 5.0 Databases

Authors: Vidya Vrat Agarwal

2nd Edition

1430242604, 978-1430242604

More Books

Students also viewed these Databases questions