Select Sort, each time the smallest number is selected, repeat two actions, compare, swap, put on the leftmost, and so on, and so on, with the array to select the sorting
Exchange two numbers
Public classSwaptwo {/** *@authorChengdu *@paramargs*/ Private int[] bubble; Public voidSetbubble (int[] bubble) { This. Bubble =bubble; } Public int[] getbubble () {returnbubble; } Public voidSwaptwonumber (intXintY) {//The index position of the incoming array, the two values of the interactive array intLenbubble =bubble.length; if(X > Lenbubble-1 | | y > lenbubble-1) {System.out.println ("Array Out of Bounds"); } Else { inttemp; if(Bubble[x] >Bubble[y]) {Temp=Bubble[x]; BUBBLE[X]=Bubble[y]; Bubble[y]=temp; }} System.out.println ("From small to Large:" +bubble[x]+ "," +Bubble[y]); } Public Static voidMain (string[] args) {//TODO auto-generated Method StubSwaptwo st =NewSwaptwo (); int[] bubble = {1, 9, 8, 5, 2, 6, 4, 3, 7}; System.out.println (bubble.length); St.setbubble (bubble); St.swaptwonumber (0, 1); }}
Select sort
Public classSelectsort {/** * @authorChengdu *@paramargs*/ PrivateSwaptwo Swaptwo; Public voidsetswaptwo (Swaptwo swaptwo) { This. Swaptwo =Swaptwo; } Publicswaptwo Getswaptwo () {returnSwaptwo; } Public voidSelectsortmethod (int[] Array) { intPos//Times intLenarray =Array.Length; Swaptwo.setbubble (array); for(Pos=1; pos < Lenarray; pos++) {System.out.println ("Number of--------" +POS); for(inti=pos-1; i < lenarray-1; i++) {Swaptwo.swaptwonumber (pos-1, i+1); } } } Public Static voidMain (string[] args) {//TODO auto-generated Method StubSwaptwo Swaptwo =NewSwaptwo (); Selectsort Selectsort=NewSelectsort (); Selectsort.setswaptwo (Swaptwo); int[] Array = {1, 9, 6, 3, 4, 2, 5, 13, 15, 10}; Selectsort.selectsortmethod (array); for(Integer i:array) {System.out.println (i); } }}
Perform:
Count--------1
From small to large: 1,9
From small to large: 1,6
From small to large: 1,3
From small to large: 1,4
From small to large, in turn:
From small to large: 1,5
From small to large: 1,13
From small to large: 1,15
From small to large: 1,10
Count--------2
From small to large: 6,9
From small to large: 3,6
From small to large: 3,4
From small to large: 2,3
From small to large: 2,5
From small to large: 2,13
From small to large: 2,15
From small to large: 2,10
Count--------3
From small to large: 6,9
From small to large: 4,6
From small to large: 3,4
From small to large: 3,5
From small to large: 3,13
From small to large: 3,15
From small to large: 3,10
Count--------4
From small to large: 6,9
From small to large: 4,6
From small to large: 4,5
From small to large: 4,13
From small to large: 4,15
From small to large: 4,10
Count--------5
From small to large: 6,9
From small to large: 5,6
From small to large: 5,13
From small to large: 5,15
From small to large: 5,10
Count--------6
From small to large: 6,9
From small to large: 6,13
From small to large: 6,15
From small to large: 6,10
Count--------7
From small to large: 9,13
From small to large: 9,15
From small to large: 9,10
Count--------8
From small to large: 13,15
From small to large: 10,13
Count--------9
From small to large: 13,15
1
2
3
4
5
6
9
10
13
15
Java selection Sort