My Java development and learning journey ------ & gt; Bubble Sorting of Java classic sorting algorithm, ------ java

Source: Internet
Author: User

My Java development and learning journey ------> Java classic sorting algorithm-Bubble sorting, ------ java

Bubble Sort is a simple sorting algorithm. 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 name of this algorithm comes from because the smaller elements will slowly "float" to the top of the series through the exchange.


I. algorithm principles

The Bubble Sorting Algorithm operates as follows:

1. Compare adjacent elements. If the first is bigger than the second, exchange the two of them.

2. perform the same operation on each adjacent element, starting from the first pair to the last one at the end. At this point, the final element should be the largest number.

3. Repeat the preceding steps for all elements except the last one.

4. Continue to repeat the above steps for fewer and fewer elements until there is no need to compare any number.


Ii. Algorithm Analysis

Time Complexity
Algorithm stability Bubble Sorting is to adjust small elements forward or large elements backward. The comparison is an adjacent comparison between two elements, and the Exchange also occurs between these two elements. Therefore, if the two elements are equal, I think you will not be bored to exchange them. If the two equal elements are not adjacent, even if the two are adjacent through the previous two exchanges, at this time, the sequence of the same elements is not changed, so the Bubble Sorting is a stable sorting algorithm. Algorithm Drill
The following animation shows how to compare the last number with the previous one. If the number is smaller than the previous one, the position is switched.

Ps: You can also open the following link, set the array to be sorted, and perform sorting drills.

Bubble sort animation demo (http://student.zjzk.cn/course_ware/data_structure/web/flashhtml/maopaopaixu.htm)



Iii. Code Implementation
Public class BubbleSortTest {/*** bubble sort * @ param source * array to be sorted */public static void bubbleSort (int [] source) {int length = source. length; for (int I = 0; I <length-1; I ++) {// N number of N-1 trip, after each trip is complete, A large element is routed to the maximum end of the array for (int j = 0; j <length-1-I; j ++) {// compare N-I times for each trip if (source [j]> source [j + 1]) {swap (source, j, j + 1 );} system. out. print ("\ n outer loop number" + (I + 1) + "Times, inner loop number" + (j + 1) + "Times, sorting result :"); printArray (source);} System. out. println ("");}} /*** exchange the values pointed by x and y subscripts ** @ param source * the array to be exchanged * @ param x * array subscript x * @ param y * array subscript x * /private static void swap (int [] source, int x, int y) {int temp = source [x]; source [x] = source [y]; source [y] = temp ;} /*** print the array and print * @ param source the array to be printed */private static void printArray (int [] source) {for (int I = 0; I <source. length; I ++) {System. out. print ("\ t" + source [I]) ;}} public static void main (String [] args) {int [] source = {24, 19, 26, 39, 36, 7, 31, 29, 38, 23}; System. out. println ("Before sorting:"); printArray (source); System. out. println ("\ n"); bubbleSort (source); System. out. println ("\ n after sorting:"); printArray (source );}}


Iv. Running results
Before sorting: 2419263936731293823 external loops, 1st internal loops, 1st external loops, 1924263936731293823 internal loops, 1st external loops, 2nd internal loops, 1924263936731293823 external loops, 3rd internal cycles, 1924263936731293823 external cycles, 1st internal cycles, 4th external cycles, 1924263639731293823 internal cycles, and 1st internal cycles. sorting results: 1924263673931293823 external loops 1st times, 6th internal loops 1924263673139293823 times, sorting results: 1st external loops 7th times, 1924263673129393823 internal loops 1st times, sorting results: 8th external loops times, internal loops, sorting result: 1924263673129383923 external loops, 1st internal loops, 9th external loops, 1924263673129382339 internal loops, 2nd external loops, 1st internal loops, 1924263673129382339 external Loops, 2nd internal cycles, 1924263673129382339 external cycles, 2nd internal cycles, 3rd external cycles, 1924263673129382339 internal cycles, and 2nd internal cycles. sorting results: 1924267363129382339 external loops 2nd times, 5th internal loops 1924267313629382339 times, sorting results: 2nd external loops 6th times, 1924267312936382339 internal loops 2nd times, sorting results: 7th External loops times, internal loops, sorting result: 1924267312936382339 external loops, 2nd internal loops, 8th external loops, 1924267312936233839 internal loops, 3rd external loops, 1st internal loops, 1924267312936233839 external Loops, 2nd internal cycles, 1924267312936233839 external cycles, 3rd internal cycles, 3rd external cycles, 1924726312936233839 internal cycles, and 3rd internal cycles. sorting results: 1924726312936233839 external loops 3rd Times, 5th internal loops 1924726293136233839 times, sorting results: 3rd external loops 6th times, 1924726293136233839 internal loops 3rd Times, sorting results: 7th External loops times, internal loops, sorting result: 1924726293123363839 external loops, 4th internal loops, 1st external loops, 1924726293123363839 internal loops, 4th external loops, 2nd internal loops, 1972426293123363839 external Loops, 3rd internal cycles, 1972426293123363839 external cycles, 4th internal cycles, 4th external cycles, 1972426293123363839 internal cycles, and 4th internal cycles. sorting results: 1972426293123363839 external loops 4th times, 6th internal loops 1972426292331363839 times, sorting results: 5th external loops 1st times, 7192426292331363839 internal loops 5th times, sorting results: 2nd external loops times, internal loops, sorting result: 7192426292331363839 external loops, 5th internal loops, 3rd external loops, 7192426292331363839 internal loops, 5th external loops, 4th internal loops, 7192426292331363839 external Loops, 5th internal cycles, 7192426232931363839 external cycles, 6th internal cycles, 1st external cycles, 7192426232931363839 internal cycles, and 6th internal cycles. sorting results: 7192426232931363839 external loops 6th times, 3rd internal loops 7192426232931363839 times, sorting results: 6th external loops 4th times, 7192423262931363839 internal loops 7th times, sorting results: 1st external loops times, internal loops, sorting result: 7192423262931363839 external loops, 7th internal loops, 2nd external loops, 7192423262931363839 internal loops, 7th External loops, 3rd internal loops, 7192324262931363839 external Loops, 1st internal loops, 7192324262931363839 external loops, 8th internal loops, 2nd external loops, 7192324262931363839 internal loops, 9th internal loops, and 1st internal loops. sorting result: 7192324262931363839 after sorting: 7192324262931363839




V. Principle of bidirectional bubble Sorting Algorithm: the operation of the traditional Bubble Sorting Algorithm is as follows: (from the back to the front)
  1. Compares adjacent elements. If the first is bigger than the second, exchange the two of them.
  2. Perform the same operation on each adjacent element, from the first to the last. At this point, the final element should be the largest number.
  3. Repeat the preceding steps for all elements except the last one.
  4. Continue to repeat the above steps for fewer and fewer elements until there is no need to compare them.
The two-way bubble algorithm works as follows:
  1. The conventional Bubble Sorting is performed in two directions. First, the Bubble Sorting is performed from left to right, and then the Bubble Sorting is performed from right to left.
  2. Use the left and right flags to record the positions of sorted elements on both sides of the left and right sides.
Algorithm example: An Example of sorting is as follows: sorting before: 45 19 77 81 13 28 18 1977 11 to the right: 19 45 77 13 28 18 19 7711 [81] To the left: [11] 19 45 77 13 28 1819 77 [81] Sort To the right: [11] 19 45 13 28 18 19 [77 77 81] Sort To the left: [11 13] 19 45 18 28 19 [77 77 81] Sort To the right: [11 13] 19 18 28 19 [45 77 77 81] Sort To the left: [11 13 18] 19 19 28 [45 77 77 81] Sort To the right: [11 13 18] 19 19 [28 45 77 77 81] Sort To the left: [11 13 18 19 19] [28 45 77 77 81] As shown above, brackets indicate the completed left and right sides. When left> = right, the sorting is completed.


6. Code Implementation
Package cn. fuxi. ms. sort; public class DoubleBubbleSortTest {/*** bidirectional bubble sorting * in each sort, the positive Bubble Sorting removes the larger elements from all the remaining elements to the rightmost end of the remaining elements, reverse Bubble Sorting removes small elements from all remaining elements to the leftmost end of the remaining elements * @ param source * array to be sorted */public static void doubleBubbleSort (int [] source) {int length = source. length; for (int I = 0, j; I <length/2; I ++) {// N count requires N/2 times for (j = I; j <length-1-I; j ++) {// compare N-I times for each trip if (source [j]> source [j + 1]) {swap (source, j, j + 1);} System. out. println ("no." + (I + 1) + "secondary positive bubble, sorting result:"); printArray (source);} System. out. println (); // Add a loop from the right to the left at the same time, when the for loop ends, a small number will pop up to the left for (-- j; j> I; j --) {if (source [j-1]> source [j]) {swap (source, j-1, j);} System. out. println ("nth" + (I + 1) + "secondary reverse bubble, sorting result:"); printArray (source);} System. out. println ("");}} /*** exchange the values pointed by x and y subscripts ** @ param source * the array to be exchanged * @ param x * array subscript x * @ param y * array subscript x * /private static void swap (int [] source, int x, int y) {int temp = source [x]; source [x] = source [y]; source [y] = temp ;} /*** print the array and print the ** @ param source * array to be printed */private static void printArray (int [] source) {for (int I = 0; I <source. length; I ++) {System. out. print ("\ t" + source [I]);} System. out. println ();} public static void main (String [] args) {int [] source = {24, 19, 26, 39, 36, 7, 31, 29, 38, 23}; System. out. println ("Before sorting:"); printArray (source); doubleBubbleSort (source); System. out. println ("\ n after sorting:"); printArray (source );}}


VII. Print the result

Before sorting: 2419263936731293823 1st positive bubbles, sorting result: 1924263936731293823 1st positive bubbles, sorting result: 1924263936731293823 1st positive bubbles, sorting result: 1924263936731293823 1st positive bubbles, sorting result: Positive bubbles, sorting result: 1924263639731293823 1st positive bubbles, sorting result: 1924263673931293823 1st positive bubbles, sorting result: 1924263673139293823 1st positive bubbles, sorting result: 1924263673129393823 1st positive bubbles, sorting result: Positive bubbles, sorting result: 1924263673129383923 1st positive bubbles, sorting result: 1924263673129382339 1st reverse bubbles, sorting result: 1924263673129233839 1st reverse bubbles, sorting result: 1924263673123293839 1st reverse bubbles, sorting result: reverse bubbles, sorting result: 1924263672331293839 1st reverse bubbles, sorting result: 1924263672331293839 1st reverse bubbles, sorting result: 1924267362331293839 1st reverse bubbles, sorting result: 1924726362331293839 1st reverse bubbles, sorting result: reverse bubbles, sorting result: 1972426362331293839 1st reverse bubbles, sorting result: 7192426362331293839 2nd positive bubbles, sorting result: 7192426362331293839 2nd positive bubbles, sorting result: 7192426362331293839 2nd positive bubbles, sorting result: Positive bubbles, sorting result: 7192426362331293839 2nd positive bubbles, sorting result: 7192426233631293839 2nd positive bubbles, sorting result: 7192426233136293839 2nd positive bubbles, sorting result: 7192426233129363839 2nd positive bubbles, sorting result: Positive bubbles, sorting result: 7192426233129363839 2nd reverse bubbles, sorting result: 7192426233129363839 2nd reverse bubbles, sorting result: 7192426232931363839 2nd reverse bubbles, sorting result: 7192426232931363839 2nd reverse bubbles, sorting result: reverse bubbles, sorting result: 7192423262931363839 2nd reverse bubbles, sorting result: 7192324262931363839 2nd reverse bubbles, sorting result: 7192324262931363839 3rd 7192324262931363839 positive bubbles, sorting result: 3rd positive bubbles, sorting result: 7192324262931363839 3rd positive bubbles, sorting result: 7192324262931363839 3rd positive bubbles, sorting result: 7192324262931363839 3rd positive bubbles, sorting result: 7192324262931363839 3rd reverse bubbles, sorting result: 7192324262931363839 3rd reverse bubbles, sorting result: 7192324262931363839 3rd reverse bubbles, sorting result: 7192324262931363839 3rd reverse bubbles, sorting result: 7192324262931363839 4th forward bubbles, sorting result: 7192324262931363839 4th positive bubbles, sorting result: 7192324262931363839 4th positive bubbles, sorting result: 7192324262931363839 4th reverse bubble, sorting result: 7192324262931363839 4th reverse bubble, sorting result: reverse bubble, sorting result: 7192324262931363839 5th positive bubbles, sorting result: 7192324262931363839 after sorting: 7192324262931363839

========================================================== ========================================================== ============================

Author: Ouyang Peng: Welcome to repost. sharing with others is the source of progress!

Reprinted Please retain the original address: http://blog.csdn.net/ouyang_peng

========================================================== ========================================================== ============================




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.