1. Bubble sort
Technical point: This process is very simple, is the first record of the keyword and the second keyword to compare, if in reverse order, then the data exchange, then the second and third, and so on. Until the N-1 record is compared with the keywords of n records.
Static voidMain (string[] args) { //Bubble SortBubblesorter B =NewBubblesorter (); int[] List = {2, -, -, +, *, +, A, A, -, -, the, - }; B.sort (list); foreach(varIteminchlist) {Console.WriteLine (item); } console.readline (); }
Set up the Bubblesorter class
Public classBubblesorter { Public voidSort (int[] list) { intI, J, temp; BOOLDone =false; J=1; while(J<list. Length) && (!Done )) { Done=true; for(i =0; I < list. Length-j; i++) { if(list[i]>list[i+1]) { done=false; Temp=List[i]; List[i]=list[i+1]; List[i+1] =temp; }} J++; } } }
The effect is as follows:
2. Select sort
Technical point: The main operation of this sort of selection is the comparison between the keywords, it is clear that the simple sort from n data, that is, from 1 to N-1, for the N-1 to select the operation.
Static voidMain (string[] args) { //Select SortSelectionsorter s =NewSelectionsorter (); int[] List = {2, -, -, +, *, +, A, A, -, -, the, - }; S.sort (list); foreach(varIteminchlist) {Console.WriteLine (item); } console.readline (); }
Set up the Selectionsorter class
Public classSelectionsorter {Private intmin; Public voidSort (int[]list] { for(inti =0; I < list. length-1; i++)//iterates through the data in the array without the last one. {min= i;//reads the current data. for(intj = i+1; J < list. Length; J + +)//traverse the data after the current data. { if(List[j]<list[min])//judge the current value, if it is the minimum, then put it where you want it to be. {min=J; } } intt=List[min]; List[min]=list[i];//Exchange data. List[i] =T; } } }
The effect is as follows:
3. Insert Sort
Technical points: To achieve the insertion sort, the main is to find an ordered sequence, and then insert the next keyword into the ordered sequence above, and then select the next inserted object from the rest of the keywords, repeat until the entire sequence is ordered.
classProgram {Static voidMain (string[] args) {Insertionsorter I=NewInsertionsorter (); int[] List = { Wu, A, One, A, -, $,5, About, Wu, -}; I.sort (list); foreach(varIteminchlist) {Console.WriteLine (item); } console.readkey (); } }
Set up the Insertionsorter class
Public classInsertionsorter { Public voidSort (int[]list] { for(inti =1; I < list. Length; i++)//iterates through the current data and does not contain the first and last. { intT=list[i];//gets the current value. intj = i;//records the current merit tag. while((j>0) && (list[j-1]>T))//Insert. {List[j]=list[j-1];//Exchange Order. --J; } List[j]=T; } } }
The effect is as follows:
4. Hill sort
Technical points: First, the entire sequence to be sorted into a number of sub-sequences to be directly inserted into the order, the entire series of records in the "Basic order" when the whole record of a direct insertion sort. The feature is that the composition of the subsequence is not a simple ' piecemeal ', but a sub-sequence of records that are separated by an ' increment '.
classProgram {//Hill Sort Algorithm Static voidMain (string[] args) {Shellsorter s=NewShellsorter (); int[] List = { A, $, -, +,5, $, -, About, the, A, -}; S.sort (list); foreach(varIteminchlist) {Console.WriteLine (item); } console.readkey (); } }
Set up the Shellsorter class
Public classShellsorter { Public voidSort (int[] list) { intInc; for(inc =1; Inc <= list. Length/9; inc =3* Inc +1) ;//iterates through the current array. for(; Inc >0; Inc/=3)//iterates through the current value. { for(inti = inc+1; I <=list. Length; i+=Inc) { intt=list[i-1]; intj = i;//gets the value. while((J>inc) && (list[j-inc-1]>T))//Hill Sort. {list[j-1]=list[j-inc-1];//Exchange data. J-=Inc; } list[j-1]=T; } } } }
The effect is as follows:
20 kinds of classical algorithms and their applications