Bubble Sorting Algorithm and bubble Algorithm

Source: Internet
Author: User
Tags rounds

Bubble Sorting Algorithm and bubble Algorithm
The Bubble Sorting Algorithm operates as follows:

  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, from the first to the last. At this point, the final element should be the largest number.
  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.

Java Implementation of Bubble Sorting:

1 package com. mianshi. easy; 2 public class Bubble {3 4 public static void main (String [] args) {5 6 int [] a = {3, 1, 2, 5, 4, 6, 9, 8, 7 }; 7 8 bubbleSort (a); 9 10 for (int I = 0; I <. length; I ++) {11 System. out. print (a [I] + ""); 12} 13 14} 15 16 // The Bubble sorting algorithm implements 17 public static void bubbleSort (int [] arr) {18 19 for (int I = 0; I <arr. length-1; I ++) 20 {21 for (int j = 0; j <arr. length-i-1; j ++) 22 {23 if (arr [j]> arr [j + 1]) 24 {25 int temp = arr [j]; 26 arr [j] = arr [j + 1]; 27 arr [j + 1] = temp; 28} 29} 30} 31} 32} 33 34 results: 35 1 2 3 4 5 6 7 8 9View Code

The outer loop controls the number of rounds. The maximum (or minimum) elements of each round are placed at the end of the array, and the nine elements are compared eight times to complete sorting.
The inner loop controls the number of comparisons per round, removing the elements behind the previous rounds.
The first round is compared 8 times, and the second round removes the final elements in the sorting order, it needs to be compared 7 times ......

Algorithm stability:Stable sorting algorithm.

Time Complexity:Average time complexity is PS: the stability of the Sorting Algorithm

Assume that there are multiple records with the same keywords in the record sequence to be sorted. If the records are sorted, the relative sequence of these records remains unchanged, that is, in the original sequence, ri = rj, and ri before rj, while in the sorted sequence, ri is still before rj, it is called this sort algorithm is stable; otherwise it is called unstable.

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.