Select sort and bubble sort, and select sort bubble sort

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.