Loop Running Time: O(n) Not O(n2)

Click For Summary

Discussion Overview

The discussion revolves around the running time complexity of a specific sorting algorithm implemented in pseudo code. Participants explore whether the running time is O(n) or O(n²), examining the behavior of nested loops and the nature of the sorting process. The conversation includes theoretical considerations, examples, and comparisons to other sorting algorithms.

Discussion Character

  • Debate/contested
  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • One participant questions the running time, suggesting that the outer loop runs n times and the inner loop could run n-1 times, leading to a worst-case time of O(n²).
  • Another participant argues that the while loop condition is satisfied at most O(n) times, implying a linear time complexity.
  • Some participants provide examples to illustrate the algorithm's performance, noting that for certain inputs, the while loop executes significantly fewer times than the maximum.
  • There is a discussion about the nature of the sorting algorithm, with some participants asserting it is not a comparison-based sort and highlighting its specific applicability to permutations of numbers 0 to n-1.
  • Concerns are raised about the algorithm's effectiveness with arbitrary numbers, suggesting that it may not work as intended in those cases.
  • Participants propose alternative methods for sorting that could be more efficient, while also discussing the implications of the swap operations in maintaining order.
  • Some participants note that the algorithm can achieve sorting in fewer than O(n log n) operations due to its unique characteristics.

Areas of Agreement / Disagreement

Participants express differing views on the time complexity of the algorithm, with no consensus reached. There are competing interpretations of the algorithm's efficiency and its applicability to various types of input.

Contextual Notes

Participants highlight limitations regarding the algorithm's performance with duplicates and arbitrary numbers, indicating that assumptions about input types significantly affect the analysis.

zorro
Messages
1,378
Reaction score
0
Can someone explain why the running time of following piece of pseudo code is O(n) and not O(n2)?

for i := 0 to n - 1
while A[A] != A
swap(A, A[A])
end while
end for

The for loop executes at most n times and the inner while loop executes at most n-1 times, giving a worst case time of O(n2)
 
Technology news on Phys.org
This follows from the algorithm: it "sorts" A. The while condition is satisfied at most O(n) times. Take some simple example (like n=5), and check how it works.
 
I tried the output for 1 3 4 2 0 and found that the While loop executes 4 times only for i=0, it doesn't execute for other i's. The output was sorted. This of course took O(n) time.
Isn't the worst case complexity of comparison based sorting algorithms O(nlogn)?
 
Abdul Quadeer said:
Isn't the worst case complexity of comparison based sorting algorithms O(nlogn)?
This isn't a comparason based sort. The worst case requires (n-1) swaps. The simplest pattern to produce the worst case pattern is to rotate the numbers left 1 place, for example if there are 8 numbers, one of the worst case patterns is 1 2 3 4 5 6 7 0.
 
Abdul Quadeer said:
Isn't the worst case complexity of comparison based sorting algorithms O(nlogn)?

This is a special case, because it only sorts the numbers 0, 1, ... (n-1).

If the array A contained arbitrary numbers (say n = 3 and A[0] = 1000, A[1] = -2000, A[2] = 42) the algorithm does not work at all. But a general comparison based sort like Quicksort or Heapsort would work, of course.
 
AlephZero said:
This is a special case, because it only sorts the numbers 0, 1, ... (n-1).

If the array A contained arbitrary numbers (say n = 3 and A[0] = 1000, A[1] = -2000, A[2] = 42) the algorithm does not work at all. But a general comparison based sort like Quicksort or Heapsort would work, of course.

So this is a comparison based sort, right?
 
It sorts special A (permutations) only. And this can be done quicker:
Code:
for i := 0 to n - 1
  A[i] = i
end for
 
That does not work with duplicates.
 
mfb said:
It sorts special A (permutations) only. And this can be done quicker:


To be fair, the "swap" operation could maintain the order of other data related to the permutation, but your ultra-fast version doesn't do that.

Each "swap" operation puts at least one element of the array into the correct place, and the algorithm never does a "swap" that moves an element out of its correct place. So the total number of "swaps" is always <= n.
 
  • #10
Abdul Quadeer said:
That does not work with duplicates.
Hmm, right.

Anyway, it is a very special sorting algorithm, which allows it to be quicker than n log n. You can know the final position of elements in constant time.
 
  • #11
This "index" sort method could be used after a conventional sort that sorted an array of indexes to an array of structures, to swap the array of structures in place to sort them according to the array of sorted indexes.

AlephZero said:
Each "swap" operation puts at least one element of the array into the correct place.
Some examples:

7 6 5 4 0 3 2 1
1 6 5 4 0 3 2 7
6 1 5 4 0 3 2 7
2 1 5 4 0 3 6 7
5 1 2 4 0 3 6 7
3 1 2 4 0 5 6 7
4 1 2 3 0 5 6 7
0 1 2 3 4 5 6 7

2 0 1 5 3 4 7 6
1 0 2 5 3 4 7 6
0 1 2 5 3 4 7 6
0 1 2 4 3 5 7 6
0 1 2 3 4 5 7 6
0 1 2 3 4 5 6 7
 
Last edited:
  • #12
rcgldr said:
This "index" sort method could be used after a conventional sort that sorted an array of indexes to an array of structures, to swap the array of structures in place to sort them according to the array of sorted indexes.
Example pseudo-code for this:

Code:
//      generate indexes

    for(i = 0; i < n; i++)
        I[i] = i;

//      sort indexes based on A[]

    sort(I, ...);

//      swap data based on sorted indexes

    for(i = 0; i < n; i++){
        while(     I[i] !=  I[I[i]] ){
            swap(A[I[i]], A[I[I[i]]]);
            swap(  I[i],    I[I[i]] );
        }
    }

Example of how this works. Using the values swapped during the "swap sort" of the sorted indexes as indexes to swap A will duplicate the permutation process needed to sort A. I also did this with B, the original sorted indexes to show that this algorithm duplicates the permutation that produced the sorted indexes.

Code:
A 7 6 5 4 0 3 2 1   (array   original)
B 0 1 2 3 4 5 6 7   (indexes original)
I 4 7 6 5 3 2 1 0   (indexes sorted)

A 7 6 5 0 4 3 2 1   (swapped A[I[0] = 4] with A[I[I[0]] = 3])
B 0 1 2 4 3 5 6 7   (swapped B[I[0] = 4] with B[I[I[0]] = 3])
I 3 7 6 5 4 2 1 0   (swapped   I[0] = 4  with   I[I[0]] = 3 )

A 7 6 5 3 4 0 2 1   (swapped A[I[0] = 3] with A[I[I[0]] = 5])
B 0 1 2 5 3 4 6 7   (swapped B[I[0] = 3] with B[I[I[0]] = 5])
I 5 7 6 3 4 2 1 0   (swapped   I[0] = 3  with   I[I[0]] = 5 )

A 7 6 0 3 4 5 2 1   (swapped A[I[0] = 5] with A[I[I[0]] = 2])
B 0 1 4 5 3 2 6 7   (swapped B[I[0] = 5] with B[I[I[0]] = 2])
I 2 7 6 3 4 5 1 0   (swapped   I[0] = 5  with   I[I[0]] = 2 )

A 7 6 2 3 4 5 0 1   (swapped A[I[0] = 2] with A[I[I[0]] = 6])
B 0 1 6 5 3 2 4 7   (swapped B[I[0] = 2] with B[I[I[0]] = 6])
I 6 7 2 3 4 5 1 0   (swapped   I[0] = 2  with   I[I[0]] = 6 )

A 7 0 2 3 4 5 6 1   (swapped A[I[0] = 6] with A[I[I[0]] = 1])
B 0 4 6 5 3 2 1 7   (swapped B[I[0] = 6] with B[I[I[0]] = 1])
I 1 7 2 3 4 5 6 0   (swapped   I[0] = 6  with   I[I[0]] = 1 )

A 7 1 2 3 4 5 6 0   (swapped A[I[0] = 1] with A[I[I[0]] = 7])
B 0 7 6 5 3 2 1 4   (swapped B[I[0] = 1] with B[I[I[0]] = 7])
I 7 1 2 3 4 5 6 0   (swapped   I[0] = 1  with   I[I[0]] = 7 )

A 0 1 2 3 4 5 6 7   (swapped A[I[0] = 7] with A[I[I[0]] = 0]) A = sorted
B 4 7 6 5 3 2 1 0   (swapped B[I[0] = 7] with B[I[I[0]] = 0]) B = indexes sorted
I 0 1 2 3 4 5 6 7   (swapped   I[0] = 7  with   I[I[0]] = 0 ) I = sorted
 
Last edited:

Similar threads

  • · Replies 11 ·
Replies
11
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 16 ·
Replies
16
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K