Java Bubble sorting, java bubble

Source: Internet
Author: User

Java Bubble sorting, java bubble

Bubble sorting principle. The following uses ascending order as an example.

  1. Compares adjacent elements. If the first is bigger than the second, exchange the two of them.
  2. Perform the same operation on each adjacent element, starting from the first pair to the end of the last pair. At this time, the last element is the largest.
  3. Repeat the preceding steps for all elements except the last one.
  4. Continue to repeat the above steps for fewer and fewer elements until there is no need to compare them.
    1 public class BubbleSort {2 public static void main (String [] args) {3 int score [] = {67, 69, 75, 87, 89, 90, 99,100 }; 4 for (int I = 0; I <score. length-1; I ++) {// do a maximum of N-1 sequential sorting 5 for (int j = 0; j <score. length-I-1; j ++) {// score the current unordered range [0 ...... length-i-1] sorting (j range is critical, this range is gradually reduced) 6 if (score [j] <score [j + 1]) {// swap a small value to the next 7 int temp = score [j]; 8 score [j] = score [j + 1]; 9 score [j + 1] = temp; 10} 11} 12 System. out. print ("no." + (I + 1) + "secondary sorting result:"); 13 for (int a = 0; a <score. length; a ++) {14 System. out. print (score [a] + "\ t"); 15} 16 System. out. println (""); 17} 18 System. out. print ("final sorting result:"); 19 for (int a = 0; a <score. length; a ++) {20 System. out. print (score [a] + "\ t"); 21} 22} 23}

Java Bubble Sorting

Public class MySort {
Public static void main (String [] args ){

MySort sort = new MySort ();
Int [] arr = new int [] {, 22, 5 };
Sort. sort (arr );
For (int I: arr ){
System. out. print (I + ",");
}
}

Public void sort (int [] targetArr) {// small to large sorting

Int temp = 0;
For (int I = 0; I <targetArr. length; I ++ ){
For (int j = I; j <targetArr. length; j ++ ){

If (targetArr [I]> targetArr [j]) {

/* // Method 1:
Temp = targetArr [I];
TargetArr [I] = targetArr [j];
TargetArr [j] = temp;

// Method 2:
TargetArr [I] = targetArr [I] + targetArr [j];
TargetArr [j] = targetArr [I]-targetArr [j];
TargetArr [I] = targetArr [I]-targetArr [j]; */
}

}
}
}
}

Java Bubble Sorting

Public class MySort {
Public static void main (String [] args ){

MySort sort = new MySort ();
Int [] arr = new int [] {, 22, 5 };
Sort. sort (arr );
For (int I: arr ){
System. out. print (I + ",");
}
}

Public void sort (int [] targetArr) {// small to large sorting

Int temp = 0;
For (int I = 0; I <targetArr. length; I ++ ){
For (int j = I; j <targetArr. length; j ++ ){

If (targetArr [I]> targetArr [j]) {

/* // Method 1:
Temp = targetArr [I];
TargetArr [I] = targetArr [j];
TargetArr [j] = temp;

// Method 2:
TargetArr [I] = targetArr [I] + targetArr [j];
TargetArr [j] = targetArr [I]-targetArr [j];
TargetArr [I] = targetArr [I]-targetArr [j]; */
}

}
}
}
}

Related Article

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.