# My Java Development learning journey------The bubble sort of >java classic sorting algorithm

Source: Internet
Author: User

Bubble sort (Bubble sort) is a simple sort algorithm. It repeatedly visited the sequence to sort, comparing two elements at a time, and swapping them out if they were wrong in the order. The work of the sequence of visits is repeated until no more need to be exchanged, that is, the sequence is sorted. The algorithm is named because the smaller elements will slowly "float" through the switch to the top of the sequence.

first, the principle of the algorithm

The bubbling Sorting algorithm works as follows:

1, compare the adjacent elements. If the first one is bigger than the second one, swap them both.

2, 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. At this point, the last element should be the maximum number.

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

4. Repeat the above steps each time for less and fewer elements until there are no pairs of numbers to compare.

second, the algorithm analysis

Complexity of Time
algorithm StabilityThe bubble sort is to move the small element forward or the large element back. The comparison is an adjacent two element comparison, and the interchange also occurs between these two elements. So, if the two elements are equal, I think you will not be bored to exchange them again, if the two equal elements are not adjacent, then even through the preceding 22 exchange two adjacent together, this time will not be exchanged, so the same elements of the order has not changed, so bubble sort is a stable sorting algorithm.

Third, the code implementation
`public class Bubblesorttest {/** * bubble sort * @param source * Array to sort */public static void Bubblesort (int[] Source {int length = source.length;for (int i = 0; i < length-1; i++) {//n The number of N-1 trips, after each trip, the larger element will run to the end of the array for (int j = 0; J &L T Length-1-I; J + +) {//per trip need to compare n-i times compared if (Source[j] > source[j + 1]) {swap (source, J, j + 1);} System.out.print ("\ n Outer Loop First" + (i + 1) + "times, Inner Loop" + (j + 1) + "times, sort result:");p Rintarray (source);} System.out.println ("");}} /** * swap x, y subscript the value that points to * * @param source * Array to be swapped * @param x * array subscript x * @param y * array subscript x */p rivate static void Swap (int[] source, int x, int y) {int temp = source[x];source[x] = source[y];source[y] = temp;} /** * Iterate through the array and print * @param source to print the array */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:");p Rintarray (sourCE); System.out.println ("\ n"); Bubblesort (source); System.out.println ("\ n-Sort:");p Rintarray (source);}}`

iv. Results of Operation
`Sort before: 2419263936731293823 outer loop 1th time, inner loop 1th time, sort result: 1924263936731293823 outer loop 1th time, Inner loop 2nd time, sort result: 1924263936731293823 outer loop 1th times, Inner Loop 3rd time, sort result: 1924263936731293823 outer loop 1th time, Inner loop 4th time, sort result: 1924263639731293823 outer loop 1th time, Inner loop 5th time, Sort result: 1924263673931293823 outer loop 1th time, Inner Loop 6th time, sort result: 1924263673139293823 outer loop 1th time, Inner Loop 7th time, sort result: 1924263673129393823 outer loop 1th times, Inner Loop 8th time, sort result: 1924263673129383923 outer loop 1th time, Inner Loop 9th time, sort result: 1924263673129382339 outer loop 2nd time, Inner loop 1th time, Sort result: 1924263673129382339 outer loop 2nd time, Inner Loop 2nd time, sort result: 1924263673129382339 outer loop 2nd time, Inner Loop 3rd time, sort result: 1924263673129382339 outer loop 2nd times, Inner Loop 4th time, sort result: 1924267363129382339 outer loop 2nd time, Inner Loop 5th time, sort result: 1924267313629382339 outer loop 2nd time, Inner Loop 6th time, Sort result: 1924267312936382339 outer loop 2nd time, Inner Loop 7th time, sort result: 1924267312936382339 outer loop 2nd time, Inner Loop 8th time, sort result: 1924267312936233839 outer loop 3rd times, Inner Loop 1th time, sort result: 1924267312936233839 outer loop 3rd time, Inner Loop 2nd time, sort result: 1924267312936233839 outer loop 3rd time, Inner loop 3rd time, Sort result: 1924726312936233839 outer loop 3rd time, Inner Loop 4th time, sort result: 1924726312936233839 outer loop 3rd time, Inner Loop 5th time, sort result: 1924726293136233839 outer loop 3rd times, Inner Loop 6th time, sort result: 1924726293136233839 outer loop 3rd time, Inner Loop 7th time, sort result: 1924726293123363839 Outer loop 4th time, Inner loop 1th time, Sort result: 1924726293123363839 Outer loop 4th time, Inner Loop 2nd time, sort result: 1972426293123363839 Outer Loop 4th time, Inner Loop 3rd time, sort result: 1972426293123363839 Outer loop 4th time, Inner Loop 4th time, sort result: 1972426293123363839 outside Loop 4th times, Inner Loop 5th times, Sort result: 1972426293123363839 Outer loop 4th time, Inner Loop 6th time, sort result: 1972426292331363839 outer loop 5th time, Inner loop 1th time, sort result: 7192426292331363839 outer loop 5th times, Inner Loop 2nd time, sort result: 7192426292331363839 outer loop 5th time, Inner Loop 3rd time, sort result: 7192426292331363839 outer loop 5th time, Inner Loop 4th time, Sort result: 7192426292331363839 outer loop 5th time, Inner Loop 5th time, sort result: 7192426232931363839 outer loop 6th time, Inner loop 1th time, sort result: 7192426232931363839 outer loop 6th times, Inner Loop 2nd time, sort result: 7192426232931363839 outer loop 6th time, Inner Loop 3rd time, sort result: 7192426232931363839 outer loop 6th time, Inner Loop 4th time, Sort result: 7192423262931363839 outer loop 7th time, Inner loop 1th time, sort result: 7192423262931363839 outer loop 7th time, Inner Loop 2nd time, sort result: 7192423262931363839 outer loop 7th times, Inner Loop 3rd time, sort result: 7192324262931363839 outer loop 8th time, Inner loop 1th time, sort result: 7192324262931363839 outer loop 8th time, Inner loop 2nd time, Sort result: 7192324262931363839 Outer loop 9th time, Inner loop 1th time, sorted results: 7192324262931363839 sorted by: 7192324262931363839`

Five, two-way bubble sort Algorithm principle:The principle of traditional bubbling algorithmThe bubbling Sort algorithm works as follows: (from back to forward)
principle of bidirectional bubbling algorithmthe two-way bubble sorting algorithm works as follows:
1. The traditional bubble sort of the two-way, first let the bubble sort from left to right, and then let the bubble sort from right to left, so complete a sort of action
2. Use left and right two flags to record the position of the ordered elements on both sides.
Algorithm Example:An example of a sort is as follows: Pre-order: 45 19 77 81 13 28 18 1977 11 Go Right Sort: 19 45 77 13 28 18 19 7711 [81] Sort left: [11] 19 45 77 13 28 1819 77 [81] sort right: [ 11] 19 45 13 28 18 19 [77 77 81] Sort left: [11 13] 19 45 18 28 19 [77 77 81] to the right: [11 13] 19 18 28 19 [45 77 77 81] Sort left: [11 13 18 ] 19 19 28 [45 77 77 81] to the right: [11 13 18] 19 19 [28 45 77 77 81] Left: [11 13 18 19 19] [28 45 77 77 81] as shown above, the parentheses indicate that the left and right sides are sorted , the sort is completed when the left >= is right.

VI. Implementation of the Code
`Package Cn.fuxi.ms.sort;public class Doublebubblesorttest {/** * bidirectional bubble sort * in each order, the forward bubbling sort will take the larger elements of all remaining elements to the far right of the remaining elements, The reverse bubbling sort takes the smaller elements of all remaining elements to the leftmost of the remaining elements * @param source * Array to sort */public static void Doublebubblesort (int[] source) {in  T length = source.length;for (int i = 0, J; i < LENGTH/2; i++) {//n number required N/2 trip for (j = i; j < length-1-I; Each trip needs to compare n-i times compared if (Source[j] > source[j + 1]) {swap (source, J, j + 1);} System.out.println ("First" + (i + 1) + "secondary forward bubbling, sort result:");p Rintarray (source);} System.out.println ();//Add a layer of loops, right to left, each time the For loop ends, the smaller number pops to the left for (--j; j > i; j--) {if (source[j-1] > Source[j]) {SW AP (source, J-1, j);} System.out.println ("First" + (i + 1) + "secondary reverse bubble, sort result:");p Rintarray (source);} System.out.println ("");}} /** * swap x, y subscript the value that points to * * @param source * Array to be swapped * @param x * array subscript x * @param y * array subscript x */p rivate static void Swap (int[] source, int x, int y) {int temp = source[x];source[x] = source[y];source[y] = temp;} /** * Iterate through the array and print * * @param SOURCE * Array to print */private static void PrintArray (int[] source) {for (int i = 0; i < source.length; i++) {Sy Stem.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 ("Pre-sort:");p Rintarray (source);d Oublebubblesort (source); System.out.println ("\ n-Sort:");p Rintarray (source);}}`

Vii. Printing Results

`Pre-sort: 2419263936731293823 1th forward bubbling, sort result: 1924263936731293823 1th forward bubbling, sorted result: 1924263936731293823 1th times forward bubbling, Sort result: 1924263936731293823 1th time forward bubbling, sorted results: 1924263639731293823 1th forward bubbling, sorted results: 1924263673931293823 1th times forward bubbling, Sort result: 1924263673139293823 1th time forward bubbling, sorted results: 1924263673129393823 1th forward bubbling, sorted results: 1924263673129383923 1th times forward bubbling, Sort result: 1924263673129382339 1th time reverse bubble, sort result: 1924263673129233839 1th reverse bubble, sort result: 1924263673123293839 1th times Reverse bubbling, Sort result: 1924263672331293839 1th time reverse bubble, sort result: 1924263672331293839 1th reverse bubble, sort result: 1924267362331293839 1th times Reverse bubbling, Sort result: 1924726362331293839 1th time reverse bubble, sort result: 1972426362331293839 1th reverse bubble, sort result: 7192426362331293839 2nd times forward bubbling, Sort result: 7192426362331293839 2nd time forward bubbling, sorted results: 7192426362331293839 2nd forward bubbling, sorted results: 7192426362331293839 2nd times forward bubbling, Sort result: 7192426233631293839 2nd time forward bubbling, sorted results: 7192426233136293839 2nd forward bubbling, sorted results: 7192426233129363839 2nd times forward bubbling, Sort result: 7192426233129363839 2nd time reverse bubble, sort result: 7192426233129363839 2nd reverse bubble, sort result: 7192426232931363839 2nd times Reverse bubbling, Sort result: 7192426232931363839 2nd time reverse bubble, sort result: 7192423262931363839 2nd reverse bubble, sort result: 7192324262931363839 2nd times Reverse bubbling, Sort Result: 7192324262931363839 3rd time forward bubbling, sorted results: 7192324262931363839 3rd forward bubbling, sorted results: 7192324262931363839 3rd forward bubbling, sorted results: 7192324262931363839 3rd times forward bubbling, Sort Result: 7192324262931363839 3rd time forward bubbling, sorted results: 7192324262931363839 3rd reverse bubbling, sorted results: 7192324262931363839 3rd times Reverse bubbling, Sort Result: 7192324262931363839 3rd time reverse bubble, sort result: 7192324262931363839 3rd reverse bubble, sort result: 7192324262931363839 4th times forward Bubbling, Sort Result: 7192324262931363839 4th time forward bubbling, sorted results: 7192324262931363839 4th forward bubbling, sorted results: 7192324262931363839 4th times Reverse bubbling, Sort Result: 7192324262931363839 4th time Reverse bubble, sort result: 7192324262931363839 the 5th time to bubble, Sort results: 7192324262931363839 after sorting: 7192324262931363839`

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

Ouyangpeng welcome reprint, sharing with people is the source of progress!

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

My Java Development learning journey------The bubble sort of >java classic sorting algorithm

Related Keywords:
Related Article

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.