Java Basic series-bubble sort

Source: Internet
Author: User

Original works, can be reproduced, but please mark the source address: https://www.cnblogs.com/V1haoge/p/9064218.html

1. Introduction to Algorithms

Counterfeit sorting is a familiar sort, although it is rarely used, but often appears in interviews, bubbling means progressive meaning, that is, progressive sequencing.

2. Algorithm principle

The bubble sort is sorted by the 22 comparison interchange of the neighboring elements, each of the two elements of the comparison contains the last element of the previous comparison, that is, each time the comparison is associated with each other, the associated element moves backwards, just like bubbling until the surface (in order).

Each time the end of the bubble means that an element is set, that is, an element is ordered, generally we are in a backward bubbling way to achieve the sort, then it means that the order of the element homing is from the tail start.

Each bubble starts with the first element and ends with the last one that is not sorted. It also means that each bubble is compared 1 times less than the previous bubble, because each time the bubble finishes, the ordering of an element is completed at the end, and the unordered element is less than one.

Understanding the above, this is conducive to our implementation of programming algorithms, but the beginner must be confused, do not worry, you need to follow the implementation of the source code below to understand the above content, then you will deepen understanding.

3. Algorithm implementation

1  Public classBubblesort {2 3      Public Static voidMain (string[] args) {4         int[] INTs = {2, 6, 4, 9, 12, 98, 5, 32, 90, 33, 24, 65, 37, 12, 4};5 sort (ints);6     }7 8      Public Static voidSortint[] ints) {9          for(inti = ints.length-1; i > 0; i--) {Ten              for(intj = 0; J < I; J + +) { One                 if(Ints[j] > ints[j + 1]){ A                     inttemp =Ints[j]; -INTS[J] = ints[j + 1]; -Ints[j + 1] =temp; the                 } -             } -         } -          for(inti:ints) { +System.out.print (i + "")); -         } +     } A}

4. Algorithm analysis

From the above code we carry out the principle of parsing, so that we can deepen the understanding of the principle, we learn the algorithm is not to recite such a simple code, we learn is the principle, is thinking, although I can not completely do, but the effort, because after we fully understand its principle, The code is pretty easy to implement, and you don't have to back up the code.

Bubble sort Implementation code in the 9th behavior of the outer loop, used to control the number of bubbles, as the principle of the bubble sort is from the tail start, so our loop variable i from the tail start loop, the end of the element subscript is the Ints.length-1,i range of i>0, here does not contain 0 The reason is because when we order the sequence of ints.length-1 elements, only one element left, then this one element automatically has been sequenced, no need to bubble again, if the order from small to large, then the last remaining element is the first element, it is the smallest element, do not have to perform bubble sort again. Then we know that the number of bubbles in the outer loop control is minus 1 times the length of the sequence.

The content of line 10th is inner loop, because each bubble we start from the first element, slowly right to the last (here is not the end), a bubble is a number of 22 compare Exchange (11th to 14th line of code) implementation, so our code inside the loop variable j= 0, each time is from 0 subscript elements start bubbling, J change range of J <i, here is our focus, is the inner and outer layer of the correlation point of the cycle, we each bubble compare the number of times are different, careful point will find that each time will be less once, this logic control is here, J is less than I, And I do i--every time the bubble starts, and that's exactly what we're doing, and for that reason, we're not at the end of each bubble, it's the end of the unsorted element, and the last one moves forward with the bubble, each time, until the second is done with the last bubble to complete the sort.

Forget to say a bit, here j<i, why not i-1, or i+1? Because in the last time we compare each bubble, it is a two-bit comparison between the end and the bottom, and then it ends, not the remaining unsorted elements, but the number minus 1 times, here J<i, just minus 1 times.

The essence of bubble sorting is within these two layers of the cycle, after fully understanding the meaning of the implementation is very simple.

4.1. Complexity of Time

The bubble sort outer loop executes n-1 times, where n represents the number of sequence elements, the number of loops in the loop is N (n-1)/2 times, and the time complexity of the bubbling sorting algorithm is O (N2).

4.2. Complexity of space

The bubble sort algorithm uses the temporary space for the element exchange, through optimization, we can achieve, space complexity of O (1).

5. Summary

Bubble sort is now more in the face of a number of questions, to see the interviewer's only extension, the actual use is rarely used, because the algorithm's time complexity is still unsatisfactory. Now more or faster sorting algorithms are used.

  

  

Java Basic series-bubble sort

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.