Presentation is loading. Please wait.

Presentation is loading. Please wait.

Quick Sort & Merge Sort Technique

Similar presentations


Presentation on theme: "Quick Sort & Merge Sort Technique"— Presentation transcript:

1 Quick Sort & Merge Sort Technique
Syed Shahab

2 Sorting The Process of rearranging the elements
so that they are in ascending order or descending order is called sorting. Example: arranging of numbers, student records. very essential for searching the dictionaries, telephone directories.

3 Properties The two Main properties of sorting techniques are:
Stable: If the sorting algorithm preserves the relative order of any two equal elements,then the sorting algorithm is stable. In Place: If an algorithm does not require an extra memory space except for few memory units,then the algorithm is said to be in place.

4 Sorting Algorithms Various sorting algorithms are Quick Sort Merge Sort Bubble Sort Selection Sort Insertion Sort

5 Quick sort In quick sort, partition the array into two parts such that elements towards left of key element are less than key element and elements towards right of key element are greater than key element. Sort the left part of the array recursively. Sort the right part of the array recursively.

6 Execution Example Quick

7 Time Complexity(Quick sort)
The time complexity of Quick sort is: Worst case: Average case: Best case: T(n) = O(n log n) T(n) = θ(nlog2n) T(n) = O(n log n)

8 Advantage In place algorithm Applied to files of any size
Since I/O is largely sequential tape can be used It used for all type of sorting

9 Disadvantage Time complexity is greater than merge sort
It gives different time complexity in all cases.

10 Merge sort In merge sort, a given array of elements is divided into two parts. The left part of the array as well as the right part of the array is sorted recursively. Later, the sorted left part and the sorted right part are finally merged into a single sorted vector. The process of merging of two sorted vectors into a single sorted vector is called simple merge.

11 Execution Example Merge 7 2 9 4  3 8 6 1  1 2 3 4 6 7 8 9
  7 2  9 4  7  2  2 7 9 4  4 9 3 8  3 8 6 1  1 6 7  7 2  2 9  9 4  4 3  3 8  8 6  6 1  1 Merge Sort

12 Time complexity(Merge sort)
The time complexity of Merge sort in all three cases (best,average&worst) is: T(n) = θ(nlog2n)

13 Advantage Stable Algorithm Applied to files of any size
Used for internal sorting of arrays in main memory Most popular algorithm for all type of sorting

14 Disadvantage It uses more memory on the stack because of the recursion
Use extra space proportional to N. so the algorithm is not in place

15 Conclusion By comparing all the sorting techniques Merge sort time complexity is less in all three cases (time consuming is less). So, Merge sort is the most efficient technique when compare to all other sorting techniques.

16 Bibliography Anany Levitin Introduction to The Design & Analysis of Algorithms, Second Edition,Pearson education Website:- www. wikipedia.org/sorting


Download ppt "Quick Sort & Merge Sort Technique"

Similar presentations


Ads by Google