Bubble sort of Java basics

Source: Internet
Author: User

1. Bubble sort

Bubble sort is a relatively simple sort algorithm. The principle of the algorithm is:

Repeatedly visited the sequence to sort, compare two adjacent elements at a time, compare them in the order they were ordered, and swap them out if they were in the wrong order. The work of the sequence of visits repeats until there are no more elements to be exchanged, and the sequencing of the sequence is complete.

Core code:

1 Private Static voidBubblesorttest (intarr[]) {2     inttemp = 0; 3      for(inti = 0; i < arr.length-1; i++) {4          for(intj = arr.length-1; J > i; j--) {5             if(Arr[j-1] >Arr[j]) {6temp = Arr[j-1];7ARR[J-1] =Arr[j];8ARR[J] =temp;9             }Ten         } One     } A}

The work done by the above code is to sort an array from small to large with bubbling.

In the outer loop from the back to the reading group, and then in the inner loop from backward to forward comparison, the number of adjacent to 22 comparison, if the previous number than it next to the next big, then exchange their position. Every time the outer loop moves the element i is small to the position of arr[i], the condition in the inner loop is j>i, because Arr[i] is lined up in front of the order. Each time you compare from backward to arr[i] you don't have to go on.

Of course, the core code above can also be written in the following form:

1Private Static voidBubbleSortTest2 (intarr[]) { 2inttemp = 0; 3 for(inti = arr.length-1; i > 0; i++) { 4 for(intj = 0; J < I; J + +) { 5if(Arr[j] < arr[j+1]) { 6 temp = arr[j+1]; 7 Arr[j+1] =Arr[j];8 Arr[j] =temp;9             }10         }11     }12}

Here the thought is the same as above, is a comparison of two of the adjacent elements . Just BubbleSortTest2 (int arr[]) is compared to the previous one and moves the largest number backwards.

The full code for the bubbling Sort Java implementation:

1  Public classBubblesorttest {2 3      Public Static voidMain (string[] args) {4         int[] intarr;5Intarr =New int[]{5,4,3,2,1};6System.out.println ("Before sorting:");7 printlist (intarr);8 bubblesorttest (intarr);9System.out.println ("After sorting:");Ten printlist (intarr); One     } A     /*Bubble Sort Core code*/ -     Private Static voidBubblesorttest (intarr[]) { -         inttemp = 0; the          for(inti = 0; i < arr.length-1; i++) { -              for(intj = arr.length-1; J > i; j--) { -                 if(Arr[j-1] >Arr[j]) { -temp = Arr[j-1]; +ARR[J-1] =Arr[j]; -ARR[J] =temp; +                 } A             } at         } -     } -      -      Public Static voidPrintlist (int[] list) { -          for(intvalue:list) { -System.out.print (Value + "")); in         } - System.out.println (); to     } +}

2. Optimized version of Bubble sort

A common optimization method for bubble sorting is to add a flag variable Changeflag, which is used to flag whether there is a data exchange during a trip.

If there is no data exchange at the time of a trip, then all elements are sorted so that they can jump directly out of the loop and end the sort.

Optimized code:

1  Public classBubblesorttest {2 3      Public Static voidMain (string[] args) {4         int[] intarr;5Intarr =New int[]{5,4,3,1,7,8,9};6System.out.println ("Before sorting:");7 printlist (intarr);8 bubblesorttest (intarr);9System.out.println ("After sorting:");Ten printlist (intarr); One     } A     /*Bubble Sort Core code*/ -     Private Static voidBubblesorttest (intarr[]) { -         inttemp = 0; the         BooleanChangeflag =false; -          -          for(inti = 0; i < arr.length-1; i++) { -Changeflag =false; +              for(intj = arr.length-1; J > i; j--) { -                 if(Arr[j-1] >Arr[j]) { +temp = Arr[j-1]; AARR[J-1] =Arr[j]; atARR[J] =temp; -Changeflag =true;//If the data has been exchanged, set the Changeflag to True -                 } -             } -             if(!Changeflag) { -                  Break;//No more swapping, jumping out of the loop in             } -System.out.print ("First" + (i+1) + "sub-order Result:"); to printlist (arr); +         } -     } the      *      Public Static voidPrintlist (int[] list) { $          for(intvalue:list) {Panax NotoginsengSystem.out.print (Value + "")); -         } the System.out.println (); +     } A}

Operation Result:

In addition, here is a code that is sorted by the console input array:

1 ImportJava.util.Scanner;2  Public classBubblesort {3 4      Public Static voidMain (string[] args) {5Scanner sc =NewScanner (system.in);6System.out.println ("Please enter an integer to bubble sort, separated by a space in the same line:");7String Intstr =sc.nextline ();8         9string[] Strarr = Intstr.split ("");Ten         int[] Intarr =New int[strarr.length]; One          A          for(inti = 0; i < strarr.length; i++) { -Intarr[i] =Integer.parseint (Strarr[i]); -         } the bubblesorttest (intarr); -System.out.println ("After sorting:"); - printlist (intarr); -     } +  -     Private Static voidBubblesorttest (intarr[]) { +         inttemp = 0; A         BooleanChangeflag =false; at          -          for(inti = 0; i < arr.length-1; i++) { -Changeflag =false; -              for(intj = arr.length-1; J > i; j--) { -                 if(Arr[j-1] >Arr[j]) { -temp = Arr[j-1]; inARR[J-1] =Arr[j]; -ARR[J] =temp; toChangeflag =true; +                 } -             }         the             if(!Changeflag) { *                  Break; $             }Panax NotoginsengSystem.out.print ("First" + (i+1) + "sub-order Result:"); - printlist (arr); the         } +     } A      the      Public Static voidPrintlist (int[] list) { +          for(intvalue:list) { -System.out.print (Value + "")); $         } $ System.out.println (); -     } -}

Bubble sort of Java basics

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.