Select sort and bubble sort, and select sort bubble sort
This morning, I want to see the bubble sort again...
As a result, I found the article that DU Niang gave me. The text above is still easy to understand. Unfortunately, some examples of the Code are not correct.
As a result, we found that the code shown above is like sorting, so the summary is as follows:
Let's assume there is an array: 624159
The corresponding index is: 0 --> 5. If I want to describe the second position, that is, the number 2, I will use index [1] to describe it.
The comparison method of Bubble Sorting is as follows: first, we compared 62 and found that 6 was relatively large, so we moved 6 to the position of index [1, then compare index [1] and index [2]. If the size is 6, Move 6 to index [2]... and so on. Finally, 9 is the largest. so far, the first round of comparison sorting is complete, and the second round of comparison will give up the maximum number, that is, the last one. The comparison principle is the same as above, eventually, the largest number is like a bubble, one by one...
The method for selecting sorting is: first compare 62 and find that 6 is relatively large, so move 2 to index [0], then compare index [0] and index [2], we found that it was still 2 small, so we didn't move it. Then we compared index [0] and index [3] and found that 1 is small, so we moved 1 to index [0]... in this case, the final index [0] is the smallest number, and then the next comparison starts from index [1] and compares it with the number except index [0, place the smallest value in index [1]...
In summary, we can find that the key is that the bubble is compared to the adjacent one, and the biggest or the smallest one is introduced. The choice is to select a number, compare one by one, and take the largest or the smallest one...
The reference code is as follows:
/*** @ Param args */public static void main (String [] args) {int [] x = {6, 2, 4, 1, 5, 9 }; for (int xx: x) {System. out. print (xx);} System. out. println (); int [] xxxx = sortit2 (x); for (int xxx: xxxx) {System. out. print (xxx) ;}// select private static int [] sortit (int [] unsorted) {for (int I = 0; I <unsorted. length; I ++) {for (int j = I; j <unsorted. length; j ++) {if (unsorted [I]> unsorted [j]) {int temp = unsorted [I]; unsorted [I] = unsorted [j]; unsorted [j] = temp ;}} return unsorted;} // bubble private static int [] sortit2 (int [] number) {int temp = 0; for (int I = 0; I <number. length-1; I ++) {for (int j = 0; j <number. length-1-i; j ++) {if (number [j]> number [j + 1]) {temp = number [j]; number [j] = number [j + 1]; number [j + 1] = temp;} // if end} return number ;}
References
: Http://zh.wikipedia.org/wiki/%E9%80%89%E6%8B%A9%E6%8E%92%E5%BA%8F
Http://zh.wikipedia.org/wiki/%E9%80%89%E6%8B%A9%E6%8E%92%E5%BA%8F