6

An array A of size n is known to be sorted except for the first k elements and last k elements where k is a constant. which of the following algorithms is best suited for sorting the array?

 A) Quicksort
 B) Bubble sort
 C) Selection Sort
 D) Insertion Sort

Given answer is D.

unable to understand how this works and also What would have been the answer if Merge sort is also given?

7
  • 4
    I suppose there is some assumption of k being significantly smaller than n? Otherwise you could make k = n/2 and the whole array would be unsorted... Also is K and k the same thing?
    – jdehesa
    Jan 7, 2019 at 11:30
  • yes K and k are the same thing here. corrected now.
    – y_159
    Jan 7, 2019 at 11:31
  • that's what makes it confusing ...i also have the same doubt.
    – y_159
    Jan 7, 2019 at 12:15
  • maybe there should be some restrictions for insertion sort to be the correct answer.
    – y_159
    Jan 7, 2019 at 12:17
  • 1
    About your "bonus" question: Yes, I think Merge Sort would be the fastest, with O(2klogk + n) (=O(n) for const. k) for merge-sorting the two unsorted segments and then merging the three parts.
    – tobias_k
    Jan 7, 2019 at 12:21

2 Answers 2

3

Let's have a look at the complexity of the algorithms:

A) Quicksort: would take worse case O(n²) average O(n log n)
B) Bubble Sort: would take O(n²)
C) Selection Sort: would take O(n²)
D) Insertion Sort: would take O(k* n) if k is constant = O(n)

So D has the best performance. (for each of the k elements: O(log n) to find the position to insert to + O(n) to insert)

But since Quicksort is known to have a small konstant faktor and is in average O(n log n), it is most likely faster for "bigger" k values.


Extra:

E) merge sort : would take 2 * O(k log k) + O(n)

  • sort the k elements at the front O(k log k)
  • sort the k elements at the end O(k log k)
  • merge the 3 lists O(n)

Over all that makes for constant k O(n) so based on complexity the same as Insertion Sort.

But if you look at it with k not constant:
merge sort: O(k log k) + O(n)
Insertion Sort: O(k* n)

So insertion sort would be faster.

Arguments against merge sort:
In general merge sort is not inplace (insertion sort is), so you would need extra space or a very clever implemenattion variant that manages to do it inplace without much overhead in complexity.

8
  • 1
    how the insertion sort will have O(k*logn) time complexity?
    – y_159
    Jan 7, 2019 at 12:13
  • @ashwani yadav: You are right I thought about that finding the insert position would only take O(log n). depending on your data-structure ins insert is costly,
    – MrSmith42
    Jan 7, 2019 at 12:18
  • 2
    your answer seems satisfying now.
    – y_159
    Jan 7, 2019 at 12:30
  • 1
    On my system, Intel 3770K 3.5 ghz, Windows 7 Pro 64 bit, Visual Studio Desktop Express 2015, quick sort is only 15% faster than standard merge sort. In 64 bit mode, with 16 registers, 8 of them used as pointers to current and end of runs, a 4 way bottom up merge sort takes about the same time as quick sort, but as mentioned, it needs O(n) space. If quicksort includes code to drop into heap sort if recursion gets too deep (intro sort), it's only 10 % faster than standard 2 way merge sort, (and slower than 4 way merge sort in 64 bit mode).
    – rcgldr
    Jan 7, 2019 at 20:42
  • 2
    There's a simpler in place merge sort than the block merge sort I linked to before, but it's not stable, it's slower, and it's a combination of recursion and iteration. The 1st quarter and last half are sorted (recursion), then merged into the second quarter, swapping elements to the first quarter for the merge, leaving the 1st quarter unsorted. Then the 1st 1/8 is sorted and merged with last 6/8 into 2nd 1/8, leaving 1st 1/8 unsorted. Repeat until 2 elements remain on the left, use insertion type sort to put those 2 elements into place. I haven't found an online example of this yet.
    – rcgldr
    Jan 7, 2019 at 20:49
1

Since the first K and last K elements are constant in numbers, so it really makes no sense in calculating their complexity as it will be constant.

Comparing all above given algos with their complexity:

A) Quicksort: Worst case O(n²) average O(n log n)

B) Bubble Sort: O(n²)

C) Selection Sort: O(n²)

D) Insertion Sort: O(k* n) if k=constant = O(n)

If the inversion count is O(n), then the time complexity of insertion sort is O(n). In worst case, there can be n(n-1)/2 inversions. The worst case occurs when the array is sorted in reverse order. So the worst case time complexity of insertion sort is O(n2).*

So, Quicksort is best in general but for small List Insertion sort has a Advantage :

Insertion sort is faster for small n because Quick Sort has extra overhead from the recursive function calls. Insertion sort is also more stable than Quick sort and requires less memory.

See Why is Insertion sort better than Quick sort for small list of elements?

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.