(c) Java data structures and algorithms--bubbling, selection, insertion sorting algorithm

Source: Internet
Author: User

First, bubble sort
    • The bubbling algorithm operates in the following rules:

①, comparing adjacent elements. If the first one is bigger than the second one, swap them both.

②, for each pair of adjacent elements to do the same work, from the beginning of the first pair to the end of the last pair. When this is done, the final element will be the maximum number (that is, the first wave bubbling is done).

③, repeat the above steps for all elements, except for the last one.

④, continue repeating the above steps each time for fewer elements until there is no pair of numbers to compare.

    • Code implementation
    Public Static int[] Sort (int[] Array) {         //The For loop here indicates how many rounds to compare in total         for(inti=1;i<array.length;i++){        //The range of J is critical, and the range is gradually shrinking, because each round will have the largest one on the right.             for(intj=0;j<array.length-i;j++){                if(array[j]>array[j+1]){                    inttemp = array[j+1]; Array[j+1] =Array[j]; ARRAY[J]=temp; }            }        }        returnArray; }
    • Test
 Public Static void Main (string[] args) {        int [] array = {9,222,100,4,8,6,35,2,4,1,0};        Maopao.sort (array);          for (int i=0;i<array.length;i++) {            System.out.println (array[i]);        }    }
    • Results

    • Bubble Sort Explanation:

The bubble sort is made up of two for loops, and the first for loop variable I represents the total number of round comparisons that are required, and the second for loop variable J indicates that each round of the element that participates in the comparison is labeled "0,1,......,length-i" because a maximum value is placed on the right side of each round. So the number of elements per round is less than one, which is why the range of J is gradually decreasing. It is not difficult to write a bubble sort quickly after you understand it.

    • Performance Analysis

Assuming that the number of array elements participating in the comparison is N, then the first round of sorting has a N-1 comparison, the second round has N-2, and so on, and so on, the summation formula for this sequence is:

(N-1) + (N-2) +...+1 = N (N-1)/2

When the value of N is very large, the algorithm compares the number of N2/2 times, ignoring minus 1.

Assuming that the data is random, it is possible to swap locations each time, possibly without swapping, assuming a probability of 50%, then the number of interchanges is N2/4. However, if the worst-case scenario is that the initial data is reversed, the position is swapped for each comparison.

The number of exchanges and comparisons is proportional to the N2. Because constants are not large in O notation, ignoring 2 and 4, the O (N2) time level is required for the bubbling sort run.

In fact, whenever you see a loop nested in another loop, we can suspect that the algorithm runs at O (N2) level, the outer loop executes n times, and the inner loop executes n times for each outer loop (or a few n times). This means that you need to perform a basic operation of N2 at approximately the same time.

(c) Java data structures and algorithms--bubbling, selection, insertion sorting algorithm

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.