These few very basic sorts are very useful, and I've re-organized the code
1#include <iostream>2#include <algorithm>3 4 using namespacestd;5 6 voidBouble_sort (int* Arry,intLenth)//Bubble Sort7 {8 inti,k;9 Ten intFlag =0; One A for(i = lenth-1; I >=0; i--) - { - for(k=0; k<i;k++) the { - if(Arry[k] > arry[k+1]) - { -Swap (arry[k],arry[k+1]);//If there is a larger case than the previous one, exchange two digits + -Flag =1;//The role of flag is to determine if the array has been sequenced, and if it is already in order, it will not be sorted. + } A } at - if(Flag = =0) - Break; - } - } - in //Insert Sort - to voidInsert_sort (int* Arry,intlenth) + { - inti,k; the * intTempnumb =0 ; $ Panax Notoginseng for(i =0; i < lenth; i++) - { theTempnumb = Arry[i];//all numbers from 0 to I + A for(k = i;k>0&& Arry[k] > tempnumb;k++)//If there is a number in front of it, greater than him, then the number of the back of a few all move backwards one the { +arry[k+1] = Arry[k];//Overall Backward Movement - } $ $ARRY[K] =Tempnumb; - } - } the /* - The idea of inserting a sort: the number at the beginning of the selection is at the far left, so there is no comparison, keep at the far left, then the second number will be compared to the first number, if the largerWuyi then the first number moves backwards, noting that the range moved here is always at the outermost level of the loop, the size of I. the */ - Wu - //Merge Sort About voidMerge (int* Arry,int* Temparry,intLeft,intRight,intrightend) $ { - intLeftend = right-1; - - intTotalnumb = Rightend-left +1; A + intPointer =Left ; the - inti; $ the while(left <= leftend && right <=rightend) the { the if(Arry[left] <Arry[right]) thetemparry[pointer++] = arry[left++]; - Else intemparry[pointer++] = arry[right++]; the } the About while(Left <=leftend) thetemparry[pointer++] = arry[left++]; the the while(Right <=rightend) +temparry[pointer++] = arry[right++]; - the for(i=0; i<totalnumb;i++,rightend--)Bayi { theArry[rightend] =Temparry[rightend]; the } - - } the the voidMerge_sort (int* Arry,int* Temparry,intLeft,intRightend)//Insert sort uses the idea of divide and conquer the { the intMid; - the if(Rightend >Left ) the { theMid = (left + rightend)/2;94 the Merge_sort (arry,temparry,left,mid); the theMerge_sort (arry,temparry,mid+1, rightend);98 AboutMerge (arry,temparry,left,mid+1, rightend); - }101 }102 103 intPartation (int* Arry,intLow,intHigh)//looking for middle values104 { the intKeynumb; 106 107Keynumb = Arry[low];//first, the default leftmost value is Keynumb108 109 while(Low <High ) the {111 while(Low < High && Arry[low] < Arry[high])//when high > Low, the low pointer moves forward until Arry.high < Arry. Low, thelow++;113 theSwap (Arry[low],arry[high]);//Exchange two values the the while(Low < High && Arry[high] >Arry[low])117high--;118 119 swap (Arry[low],arry[high]); - }121 122 returnLow;//at this point the position of low, arry[low] has been sequenced, that is, the position is the final position123 }124 the 126 127 voidQuick_sort (int* Arry,intLow,intHigh)//Quick Sort - {129 intKeynumb; the 131 if(High >Low ) the {133Keynumb = Partation (Arry,low,high);//Divide and conquer algorithm134 135Quick_sort (Arry,low,keynumb-1);//recursive to the left136 137Quick_sort (arry,keynumb+1, high);//recursive to the right138 139 } $ }141 142 intMain ()143 {144 inti;145 146 intarry[Ten] = {Ten,9,8,7,6,5,4,3,2,1};147 148 inttemparry[Ten] = {0};149 Max //Bouble_sort (arry,10);151 the //Insert_sort (arry,10);153 154 //Merge_sort (arry,temparry,0,10);155 156 //Quick_sort (arry,0,9);157 158 for(i=0;i<Ten; i++)159 { thecout << Arry[i] <<' ';161 }162}
Bubble sort, insert sort, merge sort, quick sort of learning notes