Analysis of complexity of bubble sort time

Source: Internet
Author: User

Analysis of complexity of bubble sort time

Bubble Sorting repeatedly visits the series to be sorted and compares two elements at a time. If their order is wrong, they will be exchanged. The work of visiting a sequence is repeated until there is no need for exchange, that is, the sequence has been sorted.

The operation of the Bubble Sorting Algorithm is as follows: (from the back to the front) 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 the end. 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. [

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.

Regarding the complexity of the Bubble Sorting time, we all know why the worst case is O (n ^ 2) and why O (n) is the best case. Many people have doubts, the following is an analysis:

First, let's take a look at the following two Bubble sorting methods:

Method 1:

// Bubble sort

Template

Void Bubble (T a [], int n)

{

// Bubble the largest element in array a [0: n-1] to the right.

For (int I = n-1; I> 0; -- I)

For (int j = 0; j

If (a [j]> a [j + 1])

Swap (a [j], a [j + 1]);

}

Method 2:

// Bubble Sorting that is terminated in time

Template

Void BubbleSort (T a [], int n)

{

// Bubble Sorting that is terminated in time

Bool swapped = FALSE;

For (int I = n-1; I> 0 &&! Swapped; -- I)

{

Swapped = true;

For (int j = 0; j

If (a [j]> a [j + 1])

{

Swap (a [j], a [j + 1]);

Swapped = false;

}

}

}

First of all, using templates is a good habit. There is no doubt that this is the same for interviews!

For method 1, we can see that two for loops are executed in either the best case (that is, they are arranged in order) or the worst case until the condition is invalid, therefore, in both cases, the time complexity is O (n ^ 2 ).

Method 2 is different. if it is the best case (that is, it is arranged in order), the if Condition Statement will never be executed, so that the ending symbol swapped will not be changed to false, therefore, the outer for loop is executed only once, and the for loop is fully executed n times, and the total time complexity is changed to O (n ).

Here, the space complexity (secondary storage) is O (1), because a local variable is used in Swap, as shown below:

Template

Void Swap (T & a, T & B)

{

T temp =;

A = B;

B = temp;

}

What should I do if I cannot use additional storage space? What if I cannot add a local variable? We can only use the exclusive or operation as follows:

Template

Void Swap (T & a, T & B)

{

A = a ^ B;

B = a ^ B;

A = a ^ B;

}



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.