Java re-learning-the bubble ordering of algorithms

Source: Internet
Author: User

Java Sort, said to have eight big sort, small series this blog may not speak all, I will understand the comparison in place a few sort, and everyone share it. Today to say bubble sort, actually bubble sort belongs to exchange sort of one, bubble sort is the most classical exchange sort, its algorithm idea is: (suppose data is stored in array a[n])

1. Compare a[0] and a[1], if A[0]>A[1], then Exchange a[0],a[1], then compare the new a[1] (probably the original a[0]) and a[2], if A[1]>A[2], then Exchange a[1],a[2], and so on, until A[n-2 ] and a[n-1], so that the maximum number in a is "sink", i.e. a[n-1] is the maximum value in array a.
2. Proceed from the beginning of the 1th step, the difference is that this comparison to A[n-2] can be ended, so that the second large value in array A is exchanged to the position of a[n-2].
3. To compare n-1 times, array A is sorted in order from small to large. (It is also possible to determine if there is an exchange occurring, if the interchange does not occur, then the array A is already sorted).

Theory is always very virtual, directly on the code bar, because in the code we can find their own ideas, the code I also carried out a detailed analysis:

Package Cn.tgb.sort;import java.util.arrays;//Bubble Sort public class Bubblesort {public static void main (string[] args) {//Generate with Number of machines int[] values = new int[] {(int) (Math.random () * +), (int) (Math.random () *), (int) (Math.random () * +), (int) (M Ath.random () * +), (int) (Math.random () * +), (int) (Math.random () * +), (int) (Math.random () * 100)};//print out random number system . out.println (values);//Call sort logic sort (values);//print out the sorted random number System.out.println (arrays.tostring ( values));} Sort logic private static void sort (int[] values) {//define intermediate variable int temp;//Total two sets of loops, outer loops and inner loops. From the first array to the last/outer loop the first is 0-6, the second 1-6for (int i = 0; i < values.length; i++) {//within the loop, the number of comparisons is gradually reduced by 1, because the front row is not allowed to continue the platoon. For the following values.length-i-1, I understand that in the internal loop//in J and J+1 (that is, the last two bits) to compare, J to add 1, if not minus one, then the following will write J-1 and jfor (int j = 0; J < values. Length-i-1; J + +) {//Compare adjacent elements if front is greater than back. if (Values[j] > values[j + 1]) {//Swap two element position temp = Values[j];values[j] = values[j + 1];values[j + 1] = temp;}} Is there a situation in which the order or disorder occurs after the end of the cycle? Print out the results of each loop output. System.out.prIntln ("First" + (i + 1) + "order"), for (int k = 0; k < values.length; k++) {//with no newline output, easy to view System.out.print (Values[k] + "");} System.out.println ("");}}
The output results are as follows:


Small series in the consideration of such a problem, in the third cycle, in fact, is already ordered, but also to carry out the useless four order, wasting a lot of resources. In this way, we can set a flag bit, the default is False, when a swap occurs, it becomes true, if the fourth loop is not exchanged, that is, the flag bit is still false, then we jump out of the loop, see the following code:

Package Cn.tgb.sort;import java.util.arrays;//Bubble Sort public class Bubblesort {public static void main (string[] args) {//Generate with Number of machines int[] values = new int[] {(int) (Math.random () * +), (int) (Math.random () *), (int) (Math.random () * +), (int) (M Ath.random () * +), (int) (Math.random () * +), (int) (Math.random () * +), (int) (Math.random () * 100)};//print out random number system . out.println (values);//Call sort logic sort (values);//print out the sorted random number System.out.println (arrays.tostring ( values));} Sort logic private static void sort (int[] values) {//define intermediate variable int temp;//Mark bit, true if this trip has occurred, false otherwise. Obviously if there was a trip without an exchange, the description sort has been completed. Boolean flag = false;//Total two sets of loops, outer loops and inner loops. From the first array to the last/outer loop the first is 0-6, the second 1-6for (int i = 0; i < values.length; i++) {//within the loop, the number of comparisons is gradually reduced by 1, because the front row is not allowed to continue the platoon. For the following values.length-i-1, I understand that in the internal loop//in J and J+1 (that is, the last two bits) to compare, J to add 1, if not minus one, then the following will write J-1 and jfor (int j = 0; J < values. Length-i-1; J + +) {//Compare adjacent elements if front is greater than back. if (Values[j] > values[j + 1]) {//Swap two element position temp = Values[j];values[j] = values[j + 1];vaLues[j + 1] = temp;//occurs, the flag is set to Trueflag = True;}}            No swap occurs, which indicates that it is already ordered, jumping out of the loop if (Flag==false) {break; }//is there a situation in which the order or disorder occurs after the end of the cycle? Print out the results of each loop output. System.out.println ("First" + (i + 1) + "order"), for (int k = 0; k < values.length; k++) {//with non-newline output for easy viewing of System.out.print (value S[k] + "");} System.out.println ("");}}
Small series personal prefer bubble sort, think algorithm stable, better understanding. The next small series will be sent to the explanation of the insertion sort


Java re-learning-the bubble ordering of algorithms

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.