## Difference Between Quick Sort and Merge SortA ## Quick SortQuick sort is a comparison based sorting algorithm that follows the divide and conquer technique to sort the arrays. In quick sort, we usually use a ## Algorithm of Quick Sort
- X <- A[r]
- I <- p-1
- For j <- p to r -1
- Do if A[j] <= x
- Then I <- I + 1
- Exchange A[i] <-> A[j]
- Exchange A[I + 1] <--> A[r]
- Return I + 1
- While (p < r)
- Do q <- Partition (A, p, r)
- R <- q-1
- While (p < r)
- Do q <- Partition (A, p, r)
- P <- q + 1
Suppose, we have an array X having the elements X[1], X[2], X[3],…., X[n] that are to be sort. Let's follow the below steps to sort an array using the quick sort.
If X[key] is less than X[right] or if X[key] < X[Right], - Continuously decreases the right end pointer variable until it becomes equal to the key.
- If X[key] > X[right], interchange the position of the key element to the X[right] element.
- Set, key = right and increment the left index by 1.
- Continuously compare the left element with the X[key] and increment the left index by 1 until key becomes equal to the left.
- If X[key] < X[left], interchange the position of the X[key] with X[left] and go to step 2.
Let's see an example of quick sort.
arr[] = {50, 20, 60, 30, 40, 56} In the above array, 50 is in its right place. So, we divided the elements that are less than pivot in one sub array and the elements that are larger than the pivot element in another sub array. Hence, we get the sorted array. Let's implement the above logic in C program.
Sorted Array is: Array = 50 20 60 30 40 56 ## Merge sortMerge sort is a most important sorting techniques that work on the divide and conquer strategies. It is the most popular sorting techniques used to sort data that is externally available in a file. The merge sort algorithm divides the given array into two halves (N/2). And then, it recursively divides the set of two halves array elements into the single or individual elements or we can say that until no more division can take place. After that, it compares the corresponding element to sort the element and finally, all sub elements are combined to form the final sorted elements. ## Steps to sort an array using the Merge sort algorithm- Suppose we have a given array, then first we need to divide the array into sub array. Each sub array can store 5 elements.
- Here we gave the first sub array name as A1 and divide into next two subarray as B1 and B2.
- Similarly, the right sub array name as A2 and divide it into next two sub array as B3 and B4.
- This process is repeated continuously until the sub array is divided into a single element and no more partitions may be possible.
- After that, compare each element with the corresponding one and then start the process of merging to arrange each element in such a way that they are placed in ascending order.
- The merging process continues until all the elements are merged in ascending order.
Let's see an example of merge sort.
arr[] = {70, 80, 40, 50, 60, 11, 35, 85, 2} Hence, we get the sorted array using the merge sort. Let's implement the above logic in a C program.
Predefined Array is 70 80 40 50 60 11 35 85 2 Sorted array using the Merge Sort algorithm 2 11 35 40 50 60 70 80 85 ## Quick Sort vs. Merge Sort
Next TopicBFS vs DFS |