Computer Science Engineering (CSE) Exam  >  Computer Science Engineering (CSE) Notes  >  Algorithms  >  Quick Sort & Merge Sort Comparison

Quick Sort & Merge Sort Comparison | Algorithms - Computer Science Engineering (CSE) PDF Download

When does the worst case of Quicksort occur?

The answer depends on strategy for choosing pivot. In early versions of Quick Sort where leftmost (or rightmost) element is chosen as pivot, the worst occurs in following cases.

  1. Array is already sorted in same order.
  2. Array is already sorted in reverse order.
  3. All elements are same (special case of case 1 and 2)

Since these cases are very common use cases, the problem was easily solved by choosing either a random index for the pivot, choosing the middle index of the partition or (especially for longer partitions) choosing the median of the first, middle and last element of the partition for the pivot. With these modifications, the worst case of Quick sort has less chances to occur, but worst case can still occur if the input array is such that the maximum (or minimum) element is always chosen as pivot.

Why Quick Sort preferred for Arrays and Merge Sort for Linked Lists?

Below are recursive and iterative implementations of Quick Sort and Merge Sort for arrays.

(i) Recursive Quick Sort for array.
(ii) Iterative Quick Sort for arrays.
(iii) Recursive Merge Sort for arrays
(iv) Iterative Merge Sort for arrays

  • Quick Sort in its general form is an in-place sort (i.e. it doesn’t require any extra storage) whereas merge sort requires O(N) extra storage, N denoting the array size which may be quite expensive. Allocating and de-allocating the extra space used for merge sort increases the running time of the algorithm.
  • Comparing average complexity we find that both type of sorts have O(NlogN) average complexity but the constants differ. For arrays, merge sort loses due to the use of extra O(N) storage space.
  • Most practical implementations of Quick Sort use randomized version. The randomized version has expected time complexity of O(nLogn). The worst case is possible in randomized version also, but worst case doesn’t occur for a particular pattern (like sorted array) and randomized Quick Sort works well in practice.
  • Quick Sort is also a cache friendly sorting algorithm as it has good locality of reference when used for arrays.
  • Quick Sort is also tail recursive, therefore tail call optimizations is done.

Why is Merge Sort preferred for Linked Lists?

Below are implementations of Quicksort and Mergesort for singly and doubly linked lists.
(i) Quick Sort for Doubly Linked List
(ii) Quick Sort for Singly Linked List
(iii) Merge Sort for Singly Linked List
(iv) Merge Sort for Doubly Linked List

  • In case of linked lists the case is different mainly due to difference in memory allocation of arrays and linked lists. Unlike arrays, linked list nodes may not be adjacent in memory.
  • Unlike array, in linked list, we can insert items in the middle in O(1) extra space and O(1) time if we are given reference/pointer to the previous node. Therefore merge operation of merge sort can be implemented without extra space for linked lists.
  • In arrays, we can do random access as elements are continuous in memory. Let us say we have an integer (4-byte) array A and let the address of A[0] be x then to access A[i], we can directly access the memory at (x + i * 4). Unlike arrays, we can not do random access in linked list.
  • Quick Sort requires a lot of this kind of access. In linked list to access i’th index, we have to travel each and every node from the head to i’th node as we don’t have continuous block of memory. Therefore, the overhead increases for quick sort. Merge sort accesses data sequentially and the need of random access is low.

The document Quick Sort & Merge Sort Comparison | Algorithms - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Algorithms.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
81 videos|80 docs|33 tests

Top Courses for Computer Science Engineering (CSE)

81 videos|80 docs|33 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Sample Paper

,

Quick Sort & Merge Sort Comparison | Algorithms - Computer Science Engineering (CSE)

,

Extra Questions

,

past year papers

,

study material

,

Quick Sort & Merge Sort Comparison | Algorithms - Computer Science Engineering (CSE)

,

Important questions

,

Summary

,

Semester Notes

,

video lectures

,

Objective type Questions

,

practice quizzes

,

Quick Sort & Merge Sort Comparison | Algorithms - Computer Science Engineering (CSE)

,

MCQs

,

Previous Year Questions with Solutions

,

ppt

,

Free

,

pdf

,

Viva Questions

,

shortcuts and tricks

,

Exam

,

mock tests for examination

;