230 likes | 531 Views
Quick Sort And Merge Sort Technique. 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
E N D
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.
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.
Sorting Algorithms Various sorting algorithms are Quick Sort Merge Sort Bubble Sort Selection Sort Insertion Sort
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.
Execution Example • Quick
Time Complexity(Quick sort) T(n) = O(n log n) T(n) = θ(nlog2n) T(n) = O(n log n) • The time complexity of Quick sort is: Worst case: Average case: Best case:
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
Disadvantage Time complexity is greater than merge sort It gives different time complexity in all cases.
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.
Execution Example • Merge 7 2 9 4 3 8 6 11 2 3 4 6 7 8 9 7 2 9 42 4 7 9 3 8 6 1 1 3 6 8 7 22 7 9 4 4 9 3 8 3 8 6 1 1 6 77 22 9 9 4 4 33 88 66 11 Merge Sort
Time complexity(Merge sort) T(n) = θ(nlog2n) • The time complexity of Merge sort in all three cases (best,average&worst) is:
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
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
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.
Bibliography Anany LevitinIntroduction to The Design & Analysis of Algorithms, Second Edition,Pearson education Website:- www.academics.smcvt.edu www. wikipedia.org/sorting