1) ② sorting algorithm selection sort [2] heap sort

Source: Internet
Author: User

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&LT;&LT;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&LT;&LT;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.