Java Bubble Sort

Source: Internet
Author: User

Bubble sort (Bubble sort) is a simple sort algorithm. It repeatedly visited the sequence to sort, comparing two elements at a time, and swapping them out if they were wrong in the order. The work of the sequence of visits is repeated until no more need to be exchanged, that is, the sequence is sorted. The algorithm is named because the smaller elements will slowly "float" through the switch to the top of the sequence.

The bubbling sorting algorithm works as follows:

    1. Compares the adjacent elements. If the first one is bigger than the second one, swap them both.
    2. Do the same for each pair of adjacent elements, starting with the last pair from the first pair to the end. At this point, the last element should be the maximum number.
    3. Repeat the above steps for all elements, except for the last one.
    4. Repeat the above steps each time for fewer elements, until there are no pairs of numbers to compare.

Process diagram for bubbling sort:

Code:

[Java]View PlainCopyprint?
  1. Public class bubblesort{
  2. public static void Main (string[] args) {
  3. int score[] = {n/A, * *, * *, 100};
  4. For (int i = 0; i < score.length-1; i++) { //up to n-1 sequencing
  5. for (int j = 0; J < score.length-i- 1; j + +) { //Sort the current unordered interval score[0......length-i-1] (the scope of J is critical, this The circumference is gradually shrinking)
  6. if (Score[j] < Score[j + 1]) { //swap the small value to the back
  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) + "Second order 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. }
Reference: Http://zh.wikipedia.org/wiki/%E5%86%92%E6%B3%A1%E6%8E%92%E5%BA%8F

The results of the implementation are as follows:

[HTML]View PlainCopyprint?
  1. 1th time Sorted Results: 69 75 87 89 90 99 100 67
  2. 2nd time sorted Results: 75 87 89 90 99 100 69 67
  3. 3rd time Sorted Results: 87 89 90 99 100 75 69 67
  4. 4th time Sorted Results: 89 90 99 100 87 75 69 67
  5. 5th time Sorted Results: 90 99 100 89 87 75 69 67
  6. 6th time Sorted Results: 99 100 90 89 87 75 69 67
  7. 7th time Sorted Results: 100 99 90 89 87 75 69 67
  8. Final sort Result: 100 99 90 89 87 75 69 67

Java 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.