Differences between Java Bubble sorting and selective sorting

Source: Internet
Author: User

A few days ago, the Bubble Sorting was regarded as the selected sorting by mistake. Fortunately, Boyou corrected it in time. Let's talk about the difference between the two today.

Bubble Sorting

It repeatedly visits the series to be sorted, compares two elements at a time, and exchanges them if their order is wrong. The work of visiting a sequence is repeated until there is no need for exchange, that is, the sequence has been sorted.

The Code is as follows:

                   []nums = {5,4,3,2,1         ( i = 0; i < nums.length; i++             ( j = 0; j < nums.length-i-1; j++                 (nums[j] > nums[j+1                      temp =                     nums[j] = nums[j+1                     nums[j+1] =               ( x = 0;x < nums.length;x++                  System.out.print(nums[x]+","              System.out.print("\n"   }

The output after each round of comparison is as follows:

 4,3,2,1,5 3,2,1,4,5 2,1,3,4,5 1,2,3,4,5 1,2,3,4,5,

The algorithm flow of Bubble Sorting can be clearly understood from the output.

Select sort

Each trip selects the smallest (or largest) element from the data element to be sorted, and places it at the end of the sorted series until all the data elements to be sorted are arranged.

The Code is as follows:

                   []nums = {5,4,3,2,1         ( i = 0; i < nums.length; i++             ( j = i+1; j < nums.length; j++                 (nums[i] >                      temp =                     nums[i] =                     nums[j] =               ( x = 0;x < nums.length;x++                  System.out.print(nums[x]+","              System.out.print("\n"   }

From the code, we can see that nums [I] is compared with every element in the array during each round of comparison.

The output after each round of comparison is as follows:

 1,5,4,3,2 1,2,5,4,3 1,2,3,5,4 1,2,3,4,5 1,2,3,4,5,

From the output results, it is easy to see the difference between it and the bubble sort algorithm.

 

Thank you very much ~ Or I may never know that I was wrong. If any errors occur, please kindly advise me!

 

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.