Bubble Sorting-java Implementation

Source: Internet
Author: User

Bubble Sorting-java Implementation
1. Bubble Sorting 1.1. algorithm principle

1) Compare 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 last one. 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 any number.

1.2. algorithm analysis 1.2.1. Time Complexity

If the initial state of the file is in positive order, a scan can complete the sorting. The number of key word comparisons C and the number of records M both reach the minimum value: C = n-1, M = 0.

Therefore, the best time complexity of Bubble Sorting is O (n ).

If the initial file is in reverse order, you need to sort the n-1n. For each sort, we need to compare the n-I keywords (1 ≤ I ≤ N-1), and each comparison must move the record three times to reach the position of the exchange record. In this case, the maximum number of comparisons and moves is reached:

C = O (n2) M = O (n2)

The worst time complexity of Bubble Sorting is O (n2 ).

In summary, the average time complexity of Bubble Sorting is O (n2 ).

1.2.2. algorithm Stability

Bubble Sorting is to call a small element forward or a large element backward. The comparison is an adjacent comparison between two elements, and the Exchange also occurs between these two elements. Therefore, if the two elements are equal, I think you will not be bored to exchange them. If the two equal elements are not adjacent, even if the two are adjacent through the previous two exchanges, at this time, the sequence of the same elements is not changed, so the Bubble Sorting is a stable sorting algorithm.

1.3. Algorithm Implementation

PackageConstructionDemo;

Public classBubbleSort {

Public static voidMain (String [] args ){

Int[] Sort =Sort(Newint[] {6, 5, 7, 9, 8, 3, 4, 1 });

For(IntI: sort ){

System.Out. Println (I );

}

}

Public static int[] Sort (Int[] Arrs ){

IntTemp = 0;

For(IntI = 0; I <arrs. length; I ++ ){

For(IntJ = 0; j <arrs. length-I-1; j ++ ){

If(Arrs [j]> arrs [j + 1]) {

Temp = arrs [j];

Arrs [j] = arrs [j + 1];

Arrs [j + 1] = temp;

}

}

}

ReturnArrs;

}

}

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.