Sort-bubble sort, sort bubble

Source: Internet
Author: User

Sort-bubble sort, sort bubble

During the interview, I was occasionally asked about the sorting algorithm. Sometimes I was asked to write the sorting algorithm, but it was never taken seriously. During the interview the day before yesterday, we were asked to write a program on the spot to sort the given array. At that time, I wanted to write a Bubble Sorting Algorithm. At the end of the writing, I found that I had written a nondescribable sorting program, which was bubble-free, non-bubble, and inserted, when I came back, I thought I needed to study some of these sorting algorithms. So I decided to summarize these commonly used sorting algorithms.

Sort requirements: Sort array a with the length of n in the ascending order.

Bubble sort idea: given an array of n length a, loop n-1 times, find the maximum number of a [0] to a [n-i-1] in each loop, then place the number in the position of a [n-1.

How do I find the maximum number from a [0] to a [n-i-1? : Cyclically compare the sizes of a [I] And a [I + 1], for example, a [I] <a [I + 1, then, judge the size of a [I + 1] and a [I + 2] and end the cycle.

Sorting example:

Original array: 2, 0, 3, 6, 8, 4, 9, 5, 1, 7,
1st cyclic sorting results: 0, 2, 3, 6, 4, 8, 5, 1, 7, 9,
2nd cyclic sorting results: 0, 2, 3, 4, 6, 5, 1, 7, 8, 9,
3rd cyclic sorting results: 0, 2, 3, 4, 5, 1, 6, 7, 8, 9,
4th cyclic sorting results: 0, 2, 3, 4, 1, 5, 6, 7, 8, 9,
5th cyclic sorting results: 0, 2, 3, 1, 4, 5, 6, 7, 8, 9,
6th cyclic sorting results: 0, 2, 1, 3, 4, 5, 6, 7, 8, 9,
7th cyclic sorting results: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
8th cyclic sorting results: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
9th cyclic sorting results: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,

 

public void myBubbleSort(int[] array) {        for (int i = 0; i < array.length-1; i++) {            for (int j = 0; j < array.length - i - 1; j++) {                if (array[j] > array[j + 1]) {                    int num = array[j];                    array[j] = array[j + 1];                    array[j + 1] = num;                }            }        }    }

 

Note the range of the two cycles:

Number of external cycles: n-1, fixed, even if the sorting is completed before n-m (0 <m <n (Pay attention to the 7.8.9 loop in the sorting example.), The program still needs to loop n-m to n-1.

Number of internal cycles: According to the external cycle changes, the number of times is (n-i-1) + ...... + 2 + + 1



 

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.