1. Bubble sort
To say bubble should be very simple sort of, the idea is the name, the data like bubble one by one rise.
/** Bubble Sort*/voidBubblesort (int*array,intlength) { //set flag to indicate whether it is ordered to optimize the bubble sort intFlag; //Total executive n-1 trip for(inti =0; I < length-1; i++) { //default to already orderedFlag =0; //Judging from the last element, because each trip will be good one elements, so each trip will be less compared for(intj = Length-1; J > i; j--) { //comparison is smaller than the previous element if(array[j-1] >Array[j]) { //if small, swap position and mark as an array of non-sequentialSwip (&array[j-1], &Array[j]); Flag=1; } } //If the default order is not in progress if(!flag) Break; }}
Here is the flag, if ordered, such as 1, 2, 3, 4, 5, 6 arrays, but the inner loop will still execute, but not exchange elements. You can exit the loop when the execution is done without an element exchange, which means that the array is already in order.
2. Simple selection of sorting
A simple choice of sorting is to find the current position to be the smallest, to determine if there is a smaller than the current element, if present and the current element exchange position. Compared to the bubble sorting simple selection of more than the number of exchanges n-1 times.
/** Simple selection of sorting*/voidSelectsort (int*array,intlength) { //same as bubble sort, also executes n-1 times for(inti =0; I < length-1; i++) { //guess the current position is the smallest element intMin =i; //executes n-i-1 times to find the smallest element for(intj = i+1; J < Length; J + +) { //if the current element is smaller than the Min position, the current position is min if(Array[min] >array[j]) min=J; } //if the position of min is changed, that is, the current position is not min, then the swap position if(min! =i) Swip (&array[min], &Array[i]); }}
3. Direct Insert Sort
The idea of direct insertion is also very simple, not much to say.
/** Direct Insert Sort*/voidInsertsort (int*array,intlength) { //Several elements are executed several times, with a minimum of two elements, starting with the second element for(inti =1; i < length; i++) { //if the current position is smaller than the previous position element if(Array[i] < array[i-1]) { //Save current element inttemp =Array[i]; intJ; //all elements before the current element that are larger than the current element are all moved back one for(j = i1; ARRAY[J] > Temp && J >=0; j--) {array[j+1] =Array[j]; } //assign a saved element to an empty positionarray[j+1] =temp; } }}
4. Hill sort
Hill sort is an upgrade that inserts the sort directly, so that the sequence is first relatively orderly and then re-grouped in decreasing intervals to make the whole order.
/** Hill Sort*/voidShellsort (int*array,intlength) { //set the interval of the hill sort (each/2) intincrement = length/2; //when the interval becomes 0 o'clock end while(Increment >=1) { //start at the first interval, and at the end, divide the group into a direct insert sort for each group . for(inti = increment; i < length; i++) { //and the direct insertion sort is the same, except that the interval from the directly inserted 1 becomes increment if(Array[i] < array[i-Increment]) { intJ; inttemp =Array[i]; for(j = i-increment; Array[j] > Temp && J >=0; J-=increment) {Array[j+increment] =Array[j]; } array[j+increment] =temp; } } //Update IntervalIncrement/=2; }}
Summary of common sorting algorithms