1. Pseudo Bubble sort
//Simple exchange sequencing (pseudo bubble sort), inefficient, will move smaller records to the last Static voidBubblesort_verson1 (list<int>list) { intTempVal =0; for(inti =0, IMax = list. Count; i < IMax; ++i) { for(intj = i +1, Jmax = list. Count; J < Jmax; J + +) { if(List[i] >List[j]) {TempVal= List[i] +List[j]; List[i]= TempVal-List[i]; LIST[J]= TempVal-List[i]; } } } }
2. Bubble Sort:
Static voidBubblesort_version2 (list<int>list) { inttemp =0; for(inti =0, IMax = list. Count-1; i < IMax; ++i) { for(intj =0, Jmax = list. Count-1I J < Jmax; ++j) {if(List[j] > list[j +1]) {temp= List[j] + list[j +1]; LIST[J]= Temp-List[j]; List[j+1] = temp-List[j]; } } } }
3, the optimized bubble sort:
//Bubble Sort Optimization: If most of the elements in the list are ordered, there is no meaningful comparison in the case of optimizations, so the problem can be avoided after optimization Static voidBubblesort_version3 (list<int>list) { inttemp =0; BOOLFlag =true;//Bubble Optimization for(inti =0, IMax = list. Count-1; I < IMax && flag; ++i) {flag=false; for(intj =0, Jmax = list. Count-1I J < Jmax; ++j) {if(List[j] > list[j +1]) {temp= List[j] + list[j +1]; LIST[J]= Temp-List[j]; List[j+1] = temp-List[j]; Flag=true; } } } }
4, simple selection of sort:
Static voidSimpleselectsort (list<int>list) { intMinindex, TempVal; for(inti =0, IMax = list. Count-1; i < IMax; ++i) {Minindex=i; for(intj = i +1, Jmax = list. Count; J < Jmax; ++j) {if(List[j] <List[minindex]) Minindex=J; } if(Minindex! =i) {tempval= List[minindex] +List[i]; List[i]= TempVal-List[i]; List[minindex]= TempVal-List[i]; } } }
5. Direct Insert Sort:
1 //Direct Insert Sort: Inserts a record from an unordered table into an ordered table that is already sorted, directing the insertion to the end2 Static voidInsertsort (list<int>list)3 {4 intI, IMax, J, TempVal;5 6 //I=1 begins because the initial assumption is that the array No. 0 bits form an ordered array7 for(i =1, IMax = list. Count; i < IMax; ++i)8 {9 //find the last small record in an ordered array without the array, insertTen if(List[i] < list[i-1]) One { A //store the records you want to put in to prevent data loss -TempVal =List[i]; - the //start the ordered array from i-1 (relative to the entire array), and then move back one (the reason that the tempval retains the position I, thereby providing an empty space), is to find the appropriate insertion position - for(j = i-1; J >=0&& List[j] > tempval; --j) -List[j +1] =List[j]; - +List[j +1] = TempVal;//insert the saved records into the appropriate location to find out - } + } A}
Common sorting algorithms