Select sort and Bubble sort

Source: Internet
Author: User

This morning whim, and want to see the bubble sort ...

As a degree Niang, found the degree Niang to me this article, the preceding text description or is simple and understandable, but the code to give a demonstration of some irrelevant.

And then a wiki. The code given above is found to be a sort of selection, and summarized as follows:


Let's assume that there is an array: 624159

The corresponding index is: 0-->5, if I want to describe the second position, that is, the position of the number 2, I use index[1] to describe.

The comparison method of bubble sort is: Compare 62 first, find 6 is bigger, so move 6 to index[1] position, then compare index[1] and index[2], discover or 6 big, move 6 to index[2] ... And so on, and finally found that 9 is the largest. At this point the first round of comparison is completed, the second round, the second round will give up the largest number, that is, the last one, the principle of comparison and the above, the final maximum number is like bubbles, one out of a ...


The choice of sorting method is: First compare 62, found that 6 is larger, so move 2 to index[0], and then compare index[0] and index[2], found 2 small, so do not move, and then compare index[0] and index[3], found 1 small, So move 1 to index[0] ... And so on, the final index[0] is the smallest number, then the next comparison from Index[1], and in addition to index[0] This number of comparisons, put the smallest in the index[1] ...


It can be found that the key is that the bubble is compared to the adjacent, the largest or smallest launch. And the choice is to select a number, each contrast, the largest or smallest to take over ...

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;} 



Reference documents

: 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

Select sort and Bubble sort

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.