1#include"iostream"2#include"Iomanip"3#include"time.h"4 using namespacestd;5 6 #defineNum 287typedefintType//type is int8 9 /*Ten * Adjust the sub-sequence with k root in array A as the heap, where the largest element is labeled M One * Suppose that the left and right sub-trees with the root of 2k,2k+1 are heaps A */ - voidSift (type array[],intKintm) - { the inti,j,x; - BOOLFinished =false; - -x =array[k];//temporarily save current grounding +i = k;//indicate vacancy -j =2*i;//J Point to the left child node first + while(J<=m &&!finished)//OK i node is not leaf node and search is not finished A { at if((j<m) && (array[j]<array[j+1]) J + +;//let J point to the largest of the children in the left and right - if(X>=array[j]) finished =true;//Wakahara root max, place search and Shang end flag - Else{ -Array[i] = Array[j];//big Kid, node value up . -i = j;//continue down Shang: I indicates a new vacancy, J changes accordingly -j*=2; in } - } toArray[i] = x;//fills the original root value into the current empty location you are searching to + } - the /* * * An array of elements labeled 1~n in a set of arrays sorted by a heap sorting algorithm $ */Panax Notoginseng voidHeap_sort (Type array[],intN) - { the inti; + time_t start,end; AStart =clock (); the for(i=n/2; i>=1; i--) + { -Sift (array,i,n);//Build the initial heap $ } $ for(i=n;i>=2; i--)//controlling the sorting process - { - type temp; thetemp = array[1]; -array[1] =Array[i];WuyiArray[i] =temp; theSift (Array,1, I-1);//Adjust sub-sequence array[1]~array[i-1] to heap - } WuEnd =clock (); -cout<<"The merge_sorted Array:"<<Endl; About for(i=1; i<=n;i++) $ { -COUT<<SETW (5) <<Array[i]<<" "; - if(i%Ten==0) cout<<Endl; - } Acout<<Endl; + thecout<<"performnace Time:"; -cout<< (Double) (End-start)/ +<<"Seconds"<<Endl; $ } the intMain () the { theType array[num+1]; the inti; -cout<<"Initialize Array:"<<Endl; in for(i=1; i<=num;i++) the { theArray[i] = rand ()% +; AboutCOUT<<SETW (5) <<Array[i]<<" "; the if(i%Ten==0) cout<<Endl; the } thecout<<Endl; + Heap_sort (array,num); - return 0; the}
1) ② sorting algorithm selection sort [2] heap sort