Bubble sort-java Implementation

Source: Internet
Author: User

1. Bubble sort 1.1. The principle of the algorithm

1) compare adjacent elements. If the first one is bigger than the second one, swap them both.

2) for each pair of adjacent elements to do the same work, from the beginning of the first pair to the end of the last pair. At this point, the last element should be the maximum number.

3) Repeat the above steps for all elements except the last one.

4) Repeat the above steps each time for less and fewer elements until there is no pair of numbers to compare

1.2. Analysis of the algorithm 1.2.1. Complexity of Time

if the initial state of the file is positive, a single scan will complete the sequencing. The required number of keyword comparisons C and the number of records moved M have reached the minimum:c=n-1,m=0.

so the bubble sort the best Complexity of Time to be O (n) .

If the initial file is reversed, it needs to be n-1n sort of a trip. Each order is compared to the n-i keyword (1≤i≤n-1), and each comparison must move the record three times to reach the Exchange record location. In this case, the comparison and the number of moves have reached the maximum value:

c=o (n2) m=o (n2)

the worst time complexity for bubbling sorting is O (n2) .

The overall average time complexity of the bubble sort is O (n2) .

1.2.2. Algorithm stability

The bubble sort is to move the small element forward or the large element back. The comparison is an adjacent two element comparison, and the interchange also occurs between these two elements. So, if the two elements are equal, I think you will not be bored to exchange them again, if the two equal elements are not adjacent, then even through the preceding 22 exchange two adjacent together, this time will not be exchanged, so the same elements of the order has not changed, so bubble sort is a stable sorting algorithm.

1.3. Implementation of the algorithm

Package Constructiondemo;

Public class Bubblesort {

Public static void main (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) {

int temp = 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;

}

}

}

return Arrs;

}

}

Bubble sort-java Implementation

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.