Reference Http://www.cnblogs.com/zyb428/p/5673738.html#commentform
Modified some of the code errors.
First, direct insertion sort
Cycle take I=1, to N, then ++i; small loop takes j=i, to 0, then--j; Loop statement: Use temp to compare from small to large.
voidInsertsort (intArr[],intN) { for(inti =1; I < n; ++i) { for(intj = i; J >0; --j) {if(Arr[j] <arr[j-1]) { inttemp = Arr[j];//swap (arr[j],arr[j-1]);ARR[J] = arr[j-1]; Arr[j-1] =temp; } }}}
Second, bubble sort
Cycle (i=0;i<n-1;i++) full traversal, small loop (j=0;j<n-i;j++) for all unordered elements other than I 22 exchange, equivalent to the maximum (minimum) that element slowly to the top, the top of which is I of the ordered area, The next J loop is not in the sorted order area.
voidBubblesort (intArr[],intN) { for(inti =0; I < n-1; i++) { for(intj =0; J < N-i-1; J + +) { if(Arr[j] > arr[j +1]) { inttemp =Arr[j]; ARR[J]= Arr[j +1]; Arr[j+1] =temp; } } }}
Modify: ① does not need to iterate over an ordered area. ② initial is ordered, it needs to be judged.
①i do not need <n-1, as long as less than the ordered area can be, the beginning of the ordered area is n-1, with the last instead, in turn--。 That is i<last, ordered areas are no longer sorted. Exits the iteration when the ordered area is lsat=0.
② If the initial sort succeeds, it is not exchanged, a token flat is initialized to 0, and if the iteration completes the discovery that there is no exchange operation then the flat remains at 0 and is assigned to last and jumps directly out of the iteration. If the operation is flat assigned to I, so that it cannot equal 0, to prevent accidental jumping out.
voidBubblesort (intArr[],intN) { intI, temp; intflat, last = N-1; while(Last >0) { for(i = flat =0; I <=last; ++i) {if(Arr[i] < arr[i-1]) {temp=Arr[i]; Arr[i]= Arr[i-1]; Arr[i-1] =temp; Flat=i; }} last=flat; }}
Three, quick sort
Recursive thinking, find median oncesort (), each time the quicksort function contains a median function and two quicksort functions (respectively, median left and right sides).
The idea of finding the median: I at the far left, J at the far right, while satisfying i<j and Arr[i]<arr[j], J slowly shifts to the left, if ARR[I]>ARR[J], then exchanges, and changes to I move to the right. Until the i<j is not satisfied. Return I is the median position.
intOncesort (intArr[],intFirstintend) { inti = First;intj =end; while(I <j) { while(I < j&&arr[i] <=Arr[j]) {J=j-1; } if(I <j) {inttemp =Arr[i]; Arr[i]=Arr[j]; ARR[J]=temp; } while(I < j&&arr[i] <=Arr[j]) {i=i+1; } if(I <j) {inttemp =Arr[i]; Arr[i]=Arr[j]; ARR[J]=temp; } } returni;}voidQuickSort (intArr[],intFirstintend) { if(First <end) { intFlat =Oncesort (arr, first, end); QuickSort (arr, first, flat-1); QuickSort (arr, flat+1, end); }}
Iv. sequencing of Heaps
voidHeapify (intArr[],intFirstintend) { intFather =First ;; intson =2* Father +1; while(Son <end) { if(Son +1< End&&arr[son] < Arr[son +1]) {son= son +1; } if(Arr[father] >Arr[son]) Break; Else { inttemp =Arr[son]; Arr[son]=Arr[father]; Arr[father]=temp; Father=Son; Son=2* Father +1; } }}voidHeapsort (intArr[],intN) { for(inti = n/2-1; I >=0; --i) {heapify (arr, I, N); } for(inti = n-1; i >0; --i) {inttemp =Arr[i]; Arr[i]= arr[0]; arr[0] =temp; Heapify (arr,0, i); }}
Five, merge sort
voidMerge (intArr[],intReg[],intStartintend) { if(Start >= end)return; intLen = End-start, mid = (len >>1) +start; intStart1 = start, End1 =mid; intStart2 = mid +1, End2 =end; Merge (arr, Reg, Start1, end1); Merge (arr, Reg, Start2, end2); intK =start; while(Start1 <= end1 && start2 <=end2) Reg[k+ +] = Arr[start1] < Arr[start2]? arr[start1++]: arr[start2++]; while(Start1 <=end1) Reg[k+ +] = arr[start1++]; while(Start2 <=end2) Reg[k+ +] = arr[start2++]; for(k = start; k <= end; k++) Arr[k]=reg[k];}voidMergeSort (intArr[],Const intN) {int*reg=New int[n]; Merge (arr, Reg,0N1); Delete[] reg;}
Main program: Array generation and sorting display
#include"stdafx.h"#include<Sort.h>#include<cmath>#include<iostream>using namespacestd;intMain () {//////////************************ original array creation ******************************///////////////// intarr[ -]; for(inti =0; I < -; i++) {Arr[i]= (rand ()% -+1); } intnum =sizeof(arr)/sizeof(int);//////////***************************** Sort *************************///////////////// //Insertsort (Arr,num); //bubblesort (arr, num); //QuickSort (arr, 0, num-1); //heapsort (arr, num); //mergesort (arr, num);;//////////******************************** Display **********************///////////////// for(intj =0; J < num; J + +) {cout<< Arr[j] <<" "; } System ("Pause");return 0;}
C + + sorting algorithm