Common Classic Sorting algorithms
1. Hill sort
2. Two-point insertion method
3. Direct Insertion method
4. Direct sequencing with Sentinel
5. Bubble sort
6. Select Sort
7. Quick Sort
8. Heap Sequencing
Comparison of sorting algorithms
I. Hill (Shell) Sort method (also known as the small increments of the house, which was presented by D.l.shell in 1959)
/*Shell Sort Method*/#include<stdio.h>voidSortintV[],intN) { intgap,i,j,temp; for(gap=n/2;gap>0; Gap/=2)/*set the step size of the sort, the gap is halved each time until it is reduced to 1*/ { for(i=gap;i<n;i++)/*navigate to each of the elements*/ { for(J=i-gap; (J >=0) && (V[j] > V[j+gap]); J-= Gap)/*Compare the size of the two elements that are far apart from the gap and decide how to swap them according to the sort direction*/{Temp=V[j]; V[J]=v[j+Gap]; V[j+gap]=temp; } } }}
Two. Two-point insertion method
/*two-part insertion method*/voidHalfinsertsort (intA[],intLen) { intI, j,temp; intLow , high, mid; for(i=1; i<len; i++) {Temp= A[i];/*Save But former element*/ Low=0; High= I1; while(Low <= High)/*in A[low...high] binary find the position of an orderly insert*/{mid= (low + high)/2;/*find the middle element*/ if(A[mid] > Temp)/*if the middle element is larger than the previous element, the current element is inserted to the left of the middle element*/{ High= mid-1; } Else /*if the intermediate element is smaller than the current element, but the former element is inserted to the right of the middle element*/{ Low= mid+1; } } /*find the position of the current element, between low and high*/ for(j=i-1; j>high; j--)/*element Move back*/{a[j+1] =A[j]; } A[high+1] = temp;/*Insert*/ }}
Three. Direct Insertion method
/*Direct Insertion Method*/voidInsertionsort (intInput[],intLen) { inti,j,temp; for(i =1; i < Len; i++) {Temp= Input[i];/*operation of the current element, first saved in other variables*/ for(j = i-1; j>-1&&INPUT[J] > temp; j--)/*find the right position starting from the previous element of the current element*/{input[j+1] = Input[j];/*move the element while looking for one side*/Input[j]=temp; } }}
Four. Direct sequencing with Sentinel
/** * with Sentinel Direct insertion sort, the first element of the array is not used to store valid data * will input[0] as Sentinel, you can avoid the decision INPUT[J], the array is out of bounds * because in the process of j--, when J reduced to 0 o'clock, the input[0] and I NPUT[0] * Self-comparison, it is obvious at this time that the number before position I is smaller than input[i] * position I on the number does not need to move, directly into the next round of insertion comparison. * */voidInsertionsortwithpiquet (intInput[],intLen) { inti,j; for(i =2; i < Len; i++)/*ensures that the stored data for the first element of the array input is invalid, and that the second data begins with the element before it*/{input[0] =Input[i]; for(j = i-1; INPUT[J] > input[0] ; j--) {input[j+1] =Input[j]; INPUT[J]= input[0];/*Input[j] has always been the largest of the sorted elements*/ } }}
Five. Bubbling method
/*Bubble Sort Method*/voidBublesort (intA[],intN) { inti,j,k; for(j=0; j<n;j++)/*Bubble method to sort n times*/ { for(i=0; i<n-j;i++)/*when the value of a larger element sinks, only the maximum value of the remaining elements can be sunk again.*/ { if(a[i]>a[i+1])/*Sink the value of a larger element to the end*/{k=A[i]; A[i]=a[i+1]; A[i+1]=K; } } }}
Six. Select the Sorting method
/*algorithm principle: first with an element as the benchmark, starting from One Direction scanning, * such as from left to right scanning, to a[0] as the benchmark. Next from a[0] ... A[9] * To find the smallest element and swap it with a[0]. Then move the datum position right * one bit, repeat the above action, for example, A[1] as the benchmark, find the smallest in the * a[1]~a[9], and exchange it with a[1]. The sort ends when the base bit is moved to the last element of the array (all elements on the left of the datum are incremented and ordered, and the datum is the last element, so the sorting is done). */voidSelectsort (intA[],intN) {inti,j,min,temp; for(i=0; i<n;i++) {min=i; for(j=i+1; j<=n;j++)/*The data from J forward is all lined up, so start with J to find the smallest of the remaining elements*/ { if(A[min]>a[j])/*Put the smallest of the remaining elements in A[i]*/{Temp=A[i]; A[i]=A[j]; A[J]=temp; } } } }
Seven. Quick Sort
/*Fast Sorting (quick sort). In this method, * n elements are divided into three segments (groups): Left segment, right and middle middle. Middle * contains only one element. Each element in the left segment is less than equal to the middle element, and the elements in the right segment are greater than or equals the middle * segment element. So the meta-elements in left and right can be sorted independently and do not have to be merged with the ordered results of left and *. * Sort a[0:n-1 by using the Quick Sort method * Select an element from a[0:n-1] as the middle, * this element splits the remaining elements into two segments left * and right, so that the left element is less than * equals the fulcrum, and right The elements in all are greater than or equal to the pivot * recursively sort left by using quick sort * Recursively sort right by using the quick Sort method * The result is left+middle+right*/voidQuick_sort (intData[],intLowintHigh ) { intmid; if(low<High ) {Mid=Partition (Data,low,high); Quick_sort (Data,low,mid-1);/*Recursive invocation*/Quick_sort (Data,mid+1, high); } }/*be careful to see how the following data is replaced, * First select an intermediate value, which is the first element Data[low], * and then start from the far right of the element to find a smaller element than it, and copy the element to its median value in its original position (Data[low]=data[high ]), * then find the element larger than it from the far left of the element, copy the element to the position of the element that was just found above (Data[high]=data[low]), and then load the newly vacated position in the middle value (data[low]=data[0]), * This will be larger than mid to the right side of the mid, smaller than mid to the left, * The last line, the return of the low is the position of the middle element, left and right respectively recursive can be ordered. */intPartition (intData[],intLowintHigh ) { intmid; data[0]=Data[low]; mid=Data[low]; while(Low <High ) { while(Low < High) && (Data[high] >=mid)) {--High ; } Data[low]=data[high];/*from the position of high to the direction of low, find a smaller than data[low] element, save to Data[low]*/ while(Low < High) && (Data[low] < mid))/*The newly obtained data[low] is certainly smaller than the original Data[low] that is mid*/ { ++Low ; } Data[high]=data[low];/*from the position of low to the high direction, find the element larger than Data[high], exist in Data[high]*/} Data[low]=data[0];/*Save the new position of low to the original Data[low] Data*/ returnLow/*when recursive, make it the low of the right element*/}
Eight. Heap sequencing
/************************************************************** * Heap definition n elements of sequence {k1,k2,..., kn} When and only if the following relationships are met, * is called a heap: * Ki<=k2i ki<=k2i+1 (i=1,2,..., N/2) * or * ki>=k2i ki>=k2i+1 (i=1,2,..., n/2) * Heap Sorting ideas: * Set up in tree-shaped select sort The first element of the sequence (the top element of the heap) must be the largest element in the sequence after the pending sequence is built (initial heap generation); * Swap it with the last element of the sequence and subtract one of the sequence length; After the sequence is built into the heap (heap adjustment), the top element of the heap remains the largest element in the sequence. Swap it again with the last element of the sequence and shorten the sequence length; * Repeat this process until the sequence length is one, and the resulting sequence is the sorted result. **************************************************************/voidHeapadjust (intData[],intSintM/*to arrange piles of form*/{ intJ,RC; RC=data[s];/*Saving processing elements*/ for(j=2*s;j<=m;j*=2)/*dealing with Father elements*/ { if(J<m && data[j]<data[j+1]) ++j;/*take a larger child node*/ if(Rc>data[j]) Break; Data[s]=DATA[J];/*parent node Larger child nodes are interchangeable, ensuring that the parent node is larger than all child nodes (the parent node is stored in front)*/s=J; } Data[s]=RC;/*equivalent to DATA[J]=RC*/}voidHeap_sort (intData[],intLong_n)/*Heap Sort function*/{ inti,temp; for(i=long_n/2;i>0;-I.)/*I do not understand the reasons for such treatment, I hope you will not hesitate to enlighten*/{heapadjust (data,i,long_n);/*after processing, data[i] is the maximum value of the second half of the array*/ } for(i=long_n;i>0;--i) {temp=data[1];/*Put the root element (the largest of the remaining elements) to the end, and the next time you have the remaining number .*/data[1]=Data[i]; Data[i]=temp; Heapadjust (data,1. io1); }}
What are the advantages and disadvantages of each algorithm, you can refer to Baidu Library
Address: http://wenku.baidu.com/view/c3054c0f7cd184254b353516.html
This article was reproduced from: http://blog.csdn.net/wengwuzi/archive/2008/10/05/3017968.aspx
Common Classic Sorting algorithms