Several common sorting algorithms: Insert sort, quick sort, merge sort
#include <stdio.h>#include<stdlib.h>#include<stdbool.h>/************************************************ * Insert Sort method * 1th cycles: 5, 3, 4, 6, 2, 1, 5, 5, 4, 6, 2, 1, 3, 5, 4 , 6, 2, 1 * 2nd cycles: 3, 5, 4, 6, 2, 1, 3, 5, 5, 6, 2, 1, 3, 4, 5, 6, 2, 1 * 4th cycles: 3, 4, 5, 6, 2, 1, 5, 6, 1, 2, 3, 4, 5, 6, 1 * 5th cycles: 2, 3, 4, 5, 6, 1, 2, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6 ****************** ******************************/voidInsert_sort (int*arr,intN) { inti =0, j =0, TMP =0; for(i =1; I < n; i++ ){ if(arr[i-1] >Arr[i]) {tmp=Arr[i]; for(j = i-1; ARR[J] > tmp && J >=0; j--) {arr[j+1] =Arr[j]; } arr[j+1] =tmp; } /*printf ("The%d Times iteration:", I); for (int j = 0; J < N; j + +) {printf ("%2d", Arr[j]); } printf ("\ n"); */ }}/************************************************ * Quick Sort method ************************************************/intPartitionint*arr,intLowintHigh ) { intTMP =Arr[low]; while(Low <High ) { while(Low < high && arr[high]>= tmp) high--; Arr[low]=Arr[high]; while(Low < High && Arr[low] <= tmp) low++; Arr[high]=Arr[low]; } Arr[low]=tmp; returnLow ;}voidQ_sort (int*arr,intLowintHigh ) { intPivot =0; while(Low <High ) {Pivot= partition (Arr,low,high);//to arr in splitQ_sort (arr,low,pivot-1);//recursive ordering of low sub-tablesLow = pivot +1;//Tail recursion }}/************************************************ * 2 merge sort ************************************************/voidMerge_sort (int*arr,int*tmp,intS_idx,intM_idx,inte_idx) { inti = s_idx, j = m_idx+1, k =S_idx; while(I! = m_idx+1&& J! = e_idx+1 ){ if(Arr[i] <Arr[j]) {Tmp[k+ +] = arr[i++]; } Else{tmp[k+ +] = arr[j++]; } } while(I! = m_idx+1) {tmp[k+ +] = arr[i++]; } while(J! = e_idx+1) {tmp[k+ +] = arr[j++]; } for(i = s_idx;i <= e_idx;i++) {Arr[i]=Tmp[i]; }}voidM_sort (int*arr,int*tmp,intS_idx,inte_idx) { if(S_idx <e_idx) { intMid = (S_idx + e_idx)/2;//divide arr into arr[0..mid] and arr[mid+1..n-1]M_sort (Arr,tmp,s_idx,mid);//recursively merge Arr[0..mid] into ordered Tmp[0..mid]M_sort (arr,tmp,mid+1, E_IDX);//recursively merge arr[mid+1..n-1] into ordered Tmp[mid+1..n-1]Merge_sort (ARR,TMP,S_IDX,MID,E_IDX);//merge Arr[0..mid] and arr[mid+1..n-1] into tmp[0..n-1] }}/************************************************ * Reverse sequence ************************************************/voidReverseint*arr,intN) { intS_idx =0, E_idx = n-1; intTMP =0; while(S_idx <e_idx) {tmp=Arr[s_idx]; Arr[s_idx++] =Arr[e_idx]; Arr[e_idx--] =tmp; }}voidShow_arr (int*arr,intN) { for(inti =0; I < n; i++) {printf ("%2d", Arr[i]); } printf ("\ n");}/************************************************ * Binary search * Array 1 2 3 4 5 6 Find 5 * | | | * Low mid High * Cycle 1 times: * 1 2 3 4 5 6 Find 5 * | | | * Low mid high ************************************************/BOOLBinary_search (int*arr,intNintkey) { if(NULL = = arr)return false; intLow =0, high = n-1, Mid =0;//Low is the first of the array, high is the number of the group while(Low <High ) {Mid= Low + ((high-low) >>1);//prevents spills and shifts more efficiently. Each cycle is updated. if(Key < Arr[mid]) {//If the keyword is less than the medianHigh = mid-1;//Adjust the mark to the middle and low 1 } Else if(Key > Arr[mid]) {//If the keyword is greater than the medianLow = mid +1;//lower subscript adjustment to the middle and lower standard plus 1 } Else{ return true; } } return false;}intMain () {inti =0; intArr[] = {5,3,4,6,2,1, }; intLen =sizeof(arr)/sizeof(arr[0]); intTmp[len]; M_sort (Arr,tmp,0, len-1); printf ("m_sort:\n"); Show_arr (Arr,len); Reverse (Arr,len); Insert_sort (Arr,len); printf ("insert_sort:\n"); Show_arr (Arr,len); Reverse (Arr,len); Q_sort (arr,0, len-1); printf ("q_sort:\n"); Show_arr (Arr,len); intKey =5; printf ("%d is%s in the arr\n", Key, (Binary_search (Arr,len,key))?"":" not");}
Output:
M_sort:
1 2 3 4 5 6
Insert_sort:
1 2 3 4 5 6
Q_sort:
1 2 3 4 5 6
5 is in the ARR
C Sorting algorithm