1

I came across the following question while I was doing some exercise:

A sorting algorithm starts from start of the list, scan until two succeeding items that are in the wrong order are found. Swap those items and go back to the beginning. The algorithm ends when the end of the list is reached.

What is the worst-case running time for a list of size n?

I feel that it is similar with bubble sort, but probably worse that that because it doesn't finish the whole pass of scanning the list. But I can't figure out how to calculate its time complexity. I am not sure if the code I came up below for this algorithm is correct. Many thanks for your help!

for (int i=0, i<n , i++){//n is the size of the array
     if (array[i]>array[i+1]){
          swap (array[i], array[i+1]);
          i=0;
     }
}
8
  • That looks like insertion sort to me, look it up Commented Nov 8, 2014 at 11:38
  • 1
    I am pretty sure it's not insertion sort. Insertion sort is when you pick up an item and insert it to the right location. Commented Nov 8, 2014 at 11:40
  • That would be selection sort Commented Nov 8, 2014 at 11:40
  • 1
    It's just a question/exercise for time complexity Commented Nov 8, 2014 at 11:46
  • 1
    i < n - 1 otherwise array[i+1] will get out of bounds Commented Nov 8, 2014 at 11:56

6 Answers 6

2

The number of comparisons for the worst case (I wont prove the worst case is n, n-1, ... 1 but I assume it is) is a tetrahedral number.

Why?

imagine a sequence

n, n-1, .... 1

so for 1, will be swap when everything before is already on place. So the number of comparisons when is in the last place will be n-1 before is swapped to the second last place. From the second last place it will have to do n - 2 comparisons. Following this logic, and extending to the previous numbers we have for different n:

In other words, for position n i will need to move one number down (1), from position n - 1 I will need to move 2, (1 & 2), from n-2 (1, 2 & 3). Assuming 1,2,3... n is the valid order and they start as n,...3, 2,1.

n = 3 -> 2 + 1 * 2 = 4 
n = 4 -> 3 + 2 * 2 + 3= 10 
n = 5 -> 4 + 3 * 2 + 2 * 3 + 4 = 20 
n = 6 -> 5 + 4 * 2 + 3 * 3 * 2 * 4 + 5 = 35
n = 7 -> 6 + 5 * 2 + 4 * 3 + 3 * 4 + 2 * 5 + 6 = 56 
n = 8 -> 7 + 6 * 2 + 5 * 3 + 4 * 4 + 3 * 5 + 2 * 6 + 7= 84

And this sequence is the tetrahedral numbers.

The formula for the n-th tetrahedral number is represented by the 3rd rising factorial of n divided by the factorial of 3

And as a binomial coeficient:

binomial coeficient

So, it seems that it is O(n^3)

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you!Could you explain a bit more what does n = 4 -> 3 + 2 * 2 + 3= 10 stand for? 3 is the number of steps that n (when n = 4) needs to move to its correct position? But why 2*2 +3?
Positions p1, p2, p3 and p4, elements in starting position 4, 3, 2, 1. 1 will be swapped from position p4 to position p3 when everything before it are ordered. So this causes 3 comparisons, 2 and 1 will be at one point at position p3 (2 at the beginning and 1 after one swap), that means that to move 2 and 1 from position p3 to position p2 we have 2 * 2 comparisons. 3, 2 and 1 will be moved from p2 to p1 that is one comparison each, 3 * 1. So 3 + 2 * 2 + 3 * 1
1

This is a bad form of insertion sort. In insertion sort, the complexity of insertion is linear in the length of the array, here it is quadratic. Hence the worst case time complexity of this algorithm is IMO O(n^3).

If the initial array was 4, 3, 2, 1, then the steps of the algorithm would be:

4 3 2 1
3 4 2 1
3 2 4 1
2 3 4 1
2 3 1 4
2 1 3 4
1 2 3 4

Note that each step in this sequence has a linear time complexity because to get the position of the earliest 'inversion' you scan the entire array from the beginning.

2 Comments

Could you elaborate on why it's quadratic?
@stillAFanOfTheSimpsons If you look at the sequence, to insert 1 in its correct position, we require 3 steps (seemingly linear). However each step requires us to scan the entire array from the beginning to first find the swapping position. Hence the total complexity of insertion is quadratic here.
1

An array may contain O(n^2) inversions. Every run corrects only one inversion and executes O(n) steps (comparisons). So overall time complexity is O(n^3).
The worst case - backward-sorted array with N*(N-1)/2 inversions, exact runtime analysis has already been done by Raul Guiu.

Comments

0

What you descibe is a bubble sort: http://en.wikipedia.org/wiki/Bubble_sort Bubble sort has worst-case and average complexity both О(n2)

3 Comments

I don't think so. Bubble is to compare every two succeeding items in a pass, but this one goes back to the beginning once it finishes one swap.
Yes it is, but the given code is incorrect: it misses an outer iteration, hence О(n2)
This is not bubble sort.
0

First, this algorithm looks like bubble-sort but slower than that.

Second, this algorithm is doing some non-sense operations, i.e Swap those items and go back to the beginning. For example your initial array is as follows,

10 11 12 13 14 15 16 17 18 19 9

You will iterate till the end, when you see 9, swap it with 18 then go to the beginning of the array and start comparing if(arr[0] < arr[1]) but you already know that it is smaller.

The worst case running time of this is O(n^3). The pseudo code will be something like this:

for i = 0:n-1
    if(arr[i] > arr[i+1]){
        swap(arr[i], arr[i+1])
        i = 0;
    }

2 Comments

Thanks. I understand the logic how the algorithm works, but I still don't get why the complexity is O(n^3)... Could you explain it a bit more?
As you see in my code, to put the last element to first positions, I iterate n^2 times. Assume, this array is reverse order, for each element it was going to do O(n^2) comparison, so it is O(n^3) in the worst case. Number of operation should be n^3/4 or n^3/6, i didnt think of it deeply
-1

Now Compare both your Code and Bubble sort because it seems to be..

Your code It will give Max O(n^3)

Bubble sort

void bubbleSort(int arr[])
{
    int n = arr.length;
    for (int i = 0; i < n-1; i++)
        for (int j = 0; j < n-i-1; j++)
            if (arr[j] > arr[j+1])
            {
                // swap temp and arr[i]
                int temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
}

It will give in worst case O(n^2) and even if array is sorted.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.