A common sorting algorithm

Source: Internet
Author: User

Bubble sort ( English:Bubble sort) is a simple sort algorithm. It repeatedly visited the sequence to sort, comparing two elements at a time, and swapping them out if they were wrong in the order. The work of the sequence of visits is repeated until no more need to be exchanged, that is, the sequence is sorted. The algorithm is named because the smaller elements will slowly "float" through the switch to the top of the sequence.

Time complexity O (n2)

voidBubble_sort (int*array,intLen) {  inti =0 ; intj =0 ; inttemp =0;  for(i = len; i >0; i--)  {     for(j =1; J < i;j++)    {         if(array[j-1] >Array[j]) {Temp=Array[j]; ARRAY[J]= array[j-1]; Array[j-1] =temp; }       }     }}

Insert Sort ( English: insertion Sort) is a simple and intuitive sorting algorithm. It works by constructing an ordered sequence, for unsorted data, to scan from backward forward in the sorted sequence, to find the appropriate position and insert it.

Time complexity O (n2)

/** Starting with the first element, the element can be thought to have been sorted * Take out the next element, scan from backward forward in the ordered sequence of elements * if the element (sorted) is greater than the new element, move the element to the next position * Repeat step 3 until you find the sorted element is less than or equal to the position of the new element * After inserting a new element into the position * optimal time O (n) * Worst time O (N2) * Average time O (n2)*/voidInsert_sort (int*array,intLen) {  inti =0 ; intj =0 ; inttemp =0;  for(i =1; I < len;i++) {Temp=Array[i];  for(j = i;j >0&& array[j-1] > temp;j--) Array[j]= array[j-1]; ARRAY[J]=temp; }}

Merge sort

Time complexity O (n * log2 (n))

/*decomposition: Dividing n elements into sub-sequences with N/2 elements * Resolution: Sort two sub-sequences recursively by merge sort * Merge: Merge two sorted sub-sequences to get sort results * time complexity O (N*LOG2 (n))*/voidmerge_sort_recursive (intArr[],intReg[],intLeftintRight ) {  if(Left >=Right )return; intLen = Right-left, mid = (len >>1) +Left ; intLeft1 = left, right1 =mid; intleft2 = mid +1, right2 =Right ;  Merge_sort_recursive (arr, Reg, LEFT1, right1);  Merge_sort_recursive (arr, Reg, left2, right2); intK =Left ;  while(left1 <= right1 && left2 <=right2) Reg[k+ +] = Arr[left1] < arr[left2]? arr[left1++]: arr[left2++];  while(Left1 <=right1) Reg[k+ +] = arr[left1++];  while(Left2 <=right2) Reg[k+ +] = arr[left2++];  for(k = left, K <= right; k++) Arr[k]=reg[k];}voidMerge_sort (intArr[],Const intLen) {  intReg[len]; Merge_sort_recursive (arr, Reg,0, Len-1); }

Binary heap Sequencing

Static voidSwapint*a,int*b) {intindex = *A; *a = *b; *b =index;}/** Assuming that the first element has an index of 0 in the array, the parent and child nodes are positioned as follows: * (01) The index of the left child indexed to I is (2*i+1); * (02) The index of the left child indexed to I is (2*i+2); * (03) The index of the parent node indexed to I is f Loor ((i-1)/2); *//*Top Down*/voidMax_heapify (intArray[],intStartintend) {printf ("Start is%d\n", start); intParent =start; intChild = parent *2+1;//left child   while(Child <=end) {    if(Child +1<= End && Array[child] < array[child+1]) child++; if(Array[parent] >Array[child])return; Else{Swap (&array[parent],&Array[child]); Parent=Child ; Child= (Parent <<1) +1; }     }}voidHeap_sort (intArray[],intLen) {    /*Initialize, I adjust from the last root node node, bottom-up, a[0] is the root node*/   for(inti = len/2-1; I >=0; i--) max_heapify (Array,i,len-1);  for(inti = len-1; I >=0; i--)  {    //swap the first element with the previous one, and then reorder the elements .Swap (&array[0],&Array[i]); Max_heapify (Array,0I1); }}

Quick Sort

Static voidSwapint*a,int*b) {  intindex = *A; *a = *b; *b =index;}Static intMEDIAN3 (int*array,intLeftintRight ) {  intCenter = (left + right) >>1; if(Array[left] >Array[center]) swap (&array[left],&Array[center]); if(Array[left] >Array[right]) swap (&array[left],&Array[right]); if(Array[center] >Array[right]) swap (&array[center],&Array[right]); /*Array[left] < Array[center] < Array[right]*/  returnarray[center];}Static voidQuick_array (int*array,intLeftintRight ) {  inti; intJ; intPrivot; intCenter; if((Right-left) >=2) {Privot=median3 (array,left,right); Center= (left + right) >>1; printf ("Left Center Right--->%d%d%d\n", Array[left],array[center],array[right]); Swap (&array[center],&array[right-1]);  for(inti =0; I <Ten; i++) printf ("array[%d] =%d", I,array[i]); printf ("\ n"); I=Left ; J= Right-1;  for(;;) {          while(Array[++i] <Privot);  while(Array[--j] >Privot); if(I <j) Swap (&array[i],&Array[j]); Else         Break; } Swap (&array[i],&array[right-1]); Quick_array (Array,left,i-1); Quick_array (Array,i+1, right); }Else{    if((right-left) = =0)      return; Else if((right-left) = =1)    {         if(Array[left] >Array[right]) swap (&array[left],&Array[right]); }  }}voidQuick_sort (int*array,intLen) {Quick_array (array,0, len-1);}

A common sorting algorithm

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.