Java Implementation Bubble Sort

Source: Internet
Author: User

Bubble Sort:

Is two elements that are contiguous by index, assuming greater than/less than (depending on whether an ascending or descending row is required). Then replace, or not change.

Such a round down. Compare n-1 times. n equals the number of elements; n-2, n-3 ... Until the last round. 1 more times.

So the comparison is decremented: from n-1 to 1

Then the total number of times is: 1+2+3+...+ (n-1), calculated in a linear formula: (1+n-1)/2* (n-1) ==> n/2* (n-1) ==> (n^2-n) * 0.5

Use large o to indicate the time complexity of the algorithm: 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-------Sort); for (int j = 0; J < Ary.length; J + +) {System.out.print (Ary[j] + "");} /* * Ascending. ASC1 and ASC2 optimize the comparison times for internal loops. Better than the total number of times: * 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);//descending, just need to infer the size of the replacement}static void Orderasc (int[] ary) {int count = 0;//the number of times int len = ary . length;for (int j = 0; J < Len; J + +) {//outer fixed loop count for (int k = 0; k < len-1; k++) {//inner fixed loop count if (Ary[k] > Ary[k + 1]) {Ary[k] = ary[k + 1] + (ary[k + 1] = ary[k]) * 0;//One-step interchange/* Swap two variable values * a=a+b * b=a-b * a=a-b * *} count++;} System.out.println ("\ n-----orderasc in ascending order------number of times:" + count); for (int j = 0; J < Len; J + +) {System.out.print (Ary[j] + " ");}} static void OrderAsc1 (int[] ary) {int count = 0;//number of times intLen = ary.length;for (int j = 0; J < Len; J + +) {//outer fixed loop count for (int k = len-1; k > J; k--) {//inner layer from multiple to less decrement ratio if (ary[k ] < Ary[k-1]) {ary[k] = Ary[k-1] + (ary[k-1] = ary[k]) * 0;//one-step Exchange} count++;}  System.out.println ("\ n-----orderAsc1 in ascending order------number of times:" + count); for (int j = 0; J < Len; J + +) {System.out.print (Ary[j] + " ");}} static void OrderAsc2 (int[] ary) {int count = 0;//the number of times int len = ary.length;for (int j = len-1; j > 0; j--) {//outer fixed loop secondary Number */* k < J;  Total comparison times = (n^2-n)/2 */for (int k = 0; k < J; k++) {//The inner layer is decremented from many to less if (Ary[k] > ary[k + 1]) {Ary[k] = ary[k + 1] + (ary[k + 1] = ary[k]) * 0;//One-step Exchange}count++;}}  System.out.println ("\ n-----orderAsc2 in ascending order------number of times:" + count); for (int j = 0; J < Len; J + +) {System.out.print (Ary[j] + " ");}}}
Print



Java Implementation Bubble Sort

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.