Java-based Bubble Sorting

Source: Internet
Author: User

Java-based Bubble Sorting

Bubble Sorting:

It is to compare two adjacent elements by index. If the value is greater than/less than (depending on whether the order needs to be ascending or descending), it will be replaced. Otherwise, it will not be changed.

This round down, compared n-1 times, n is equal to the number of elements; N-2, n-3... until the last round, compared to 1

Therefore, the number of comparisons is decreasing from n-1 to 1.

The total number of comparisons is: 1 + 2 + 3 +... + (n-1), calculated using an equal deviation formula: (1 + N-1)/2 * (n-1) => n/2 * (n-1) => (n ^ 2-n) * 0.5

The time complexity of the algorithm is represented by large O: O (n ^ 2), ignoring the coefficients 0.5 and constant-n

Public class BubbleSort {public static void main (String [] args) {int len = 10; int [] ary = new int [len]; Random random = new Random (); for (int j = 0; j <len; j ++) {ary [j] = random. nextInt (1000);} System. out. println ("------- before sorting ------"); for (int j = 0; j <ary. length; j ++) {System. out. print (ary [j] + "");}/** in ascending order, Asc1 and Asc2 optimize the number of internal cycles, better * Total number of comparisons: * Asc1, Asc2 :( 1 + N-1)/2 * (n-1) => n/2 * (n-1) => n * (n-1) /2 => (n ^ 2-n)/2 * Asc: n ^ 2-n * // orderAsc (ary); // orderAsc2 (ary); orderAsc1 (ary ); // in descending order, you only need to replace the judgment size with} static void orderAsc (int [] ary) {int count = 0; // number of comparisons int len = ary. length; for (int j = 0; j <len; j ++) {// number of fixed outer loops for (int k = 0; k <len-1; k ++) {// Number of inner fixed loops if (ary [k]> ary [k + 1]) {ary [k] = ary [k + 1] + (ary [k + 1] = ary [k]) * 0; // One-Step swap/* swap two variable values * a = a + B * B = a-B * a = a-B */} count ++;} System. out. println ("\ n ----- after orderAsc Ascending Order ------ times:" + count); for (int j = 0; j <len; j ++) {System. out. print (ary [j] + "") ;}} static void orderAsc1 (int [] ary) {int count = 0; // number of comparisons int len = ary. length; for (int j = 0; j <len; j ++) {// number of fixed outer loops for (int k = len-1; k> j; k --) {// decrease the number of times the inner layer is compared from large to small if (ary [k] <ary [k-1]) {ary [k] = ary [k-1] + (ary [k-1] = ary [k]) * 0; // One-Step switching} count ++ ;}} system. out. println ("\ n ----- orderAsc1 in ascending order ------ times:" + count); for (int j = 0; j <len; j ++) {System. out. print (ary [j] + "") ;}} static void orderAsc2 (int [] ary) {int count = 0; // number of comparisons int len = ary. length; for (int j = len-1; j> 0; j --) {// number of fixed Outer Loops/** k <j; total number of comparisons = (n ^ 2-n)/2 */for (int k = 0; k <j; k ++) {// decrease the number of times the inner layer is compared from large to small if (ary [k]> ary [k + 1]) {ary [k] = ary [k + 1] + (ary [k + 1] = ary [k]) * 0; // One-Step switching} count ++ ;}} system. out. println ("\ n ----- orderAsc2 in ascending order ------ times:" + count); for (int j = 0; j <len; j ++) {System. out. print (ary [j] + "");}}}
Print

------- Before sorting ------ 898 7 862 286 879 660 433 724 316 ----- orderAsc1 after ascending sorting ------ times: 737 457 286 316 433 660 724 737 862 879 898


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.