1

I have looked all over the internet and cannot find such wc time-complexity sort algorithm.

And I believe it's not Bogo Sort since it's wc is not infinity

asked Dec 9, 2020 at 17:26
5
  • A possible sorting algorithm is to generate all permutations of the input, and to check is the result is ordered. Worst case complexity O(n!). Even the bubble sort is much better! This seems to be Bogo sort? Commented Dec 9, 2020 at 17:29
  • I just checked that Bogo sort consists is generating random permutations, so different than the enumeration sort of complexity O(n!) Commented Dec 9, 2020 at 17:39
  • 1
    Every algorithm has O(n!) complexity, except those that are worse than that. Commented Dec 9, 2020 at 18:37
  • 1
    @NateEldredge Wikipedia says O((n+1)!). Commented Dec 9, 2020 at 19:42
  • Bogo sort is unbound. Commented Dec 10, 2020 at 2:05

1 Answer 1

2

Such an algorithm could be Heap's algorithm, enriched with a verification after each swap that checks whether the array is sorted. Each modification to the array made by Heap's algorithm takes amortised O(1) time, so it has an overall time complexity of O(n!).

Add to that the verification to see that the array is sorted. If we do that naively, this would take a time complexity of O(n) each time it runs, making the overall time complexity O(n!n).

But we could first count the number of consecutive inversions (which could be a value between 0 and n-1). This step has a time complexity of O(n). In light of the greater time complexity of Heap's algorithm, this is irrelevant.

Then we could see how each swap in Heap's algorithm influences that count. There can be up to 4 consecutive pairs that are influenced by a swap, so it takes constant time to verify how the count is modified by the swap. If after the swap the count is zero, we know the array is sorted.

In the worst case Heap's algorithm has to go through all permutations before the count is found to be zero, so that represents a worst case time complexity of O(n!)

answered Dec 9, 2020 at 18:22

1 Comment

That routine needs to be added to the table at en.wikipedia.org/wiki/Sorting_algorithm.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.