Java selects sort and bubble sort, java selects sort bubble

Source: Internet
Author: User

Java selects sort and bubble sort, java selects sort bubble

Characteristics and differences between sorting and Bubble Sorting

++ ++

Select sort
The basic idea of this simple sorting method is:
R [n]
The first time from R [0] ~ Select the minimum value in R [n-1] and exchange it with R [0;
The second time from R [1] ~ Select the minimum value in R [n-1] and exchange it with R [1;
....,
I times from R [I-1] ~ Select the minimum value in R [n-1] and exchange with R [I-1;
.....,
N-1-1 from R [N-2] ~ Select the minimum value in R [n-1] and exchange with R [N-2;
A total of N-1 requests are used to obtain an ordered sequence in ascending order of sorted codes.
Features
The average time complexity of sorting is O (n².
 1 for(int i = 0;i<arr.length;i++){ 2          3       for(int j= 1 ; j<arr.length;j++){ 4           if(arr[j-1]>arr[j]){ 5               int temp = arr[j]; 6               arr[j] = arr[j-1]; 7               arr[j-1] = temp; 8  9           }10      }11}

 

++ ++

Bubble Sorting
Compare two adjacent numbers in sequence, place decimal places in front, and place large numbers in the back.
That is, the first trip:
First, compare the numbers of 1st and 2nd, and put the decimal places before and after the large numbers;
Then compare the numbers of 2nd and 3rd. Place the decimal places before and after the decimal places, and continue until the last two digits are compared. Place the decimal places before and after the decimal places;
So far, the first stop is over, and the maximum number is placed at the end.
In the second trip:
The comparison starts from the first logarithm (because of the exchange of 2nd numbers and 3rd numbers, the number of 1st numbers is no longer less than 2nd). Place the decimal places before and after the large number;
Always compare to the last-to-second number (the last-to-last position is already the largest );
At the end of the second round, a new maximum number is obtained at the penultimate position (in fact, it is the second largest number in the entire series ).
In this case, repeat the above process until the sorting is completed.
Features
The average time complexity of Bubble Sorting is the same as that of insert sorting. It is also a square level, but it is also very easy to implement.
1 for (int I = 0; I <arr. length-1; I ++) {// traverse 2 3 for (int j = 0; j <arr. length-1-i; j ++) {// 4 5 if (arr [j]> arr [j + 1]) {// The preceding number is greater than the following number. 6 int temp = arr [j]; // then switch, sort the big back to 7 arr [j] = arr [j + 1]; 8 arr [j + 1] = temp; 9} 10} 11}

 

Momo said: the selection of sorting (including shaker Sorting/heap sorting) and Bubble sorting are both part of the exchange sorting.

Computer sorting algorithms: insert sorting, Bubble sorting, select sorting, fast sorting, heap sorting, Merge Sorting, base sorting, and Hill sorting

[Each sorting algorithm has an average time complexity]

Related Article

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.