function quickPartition(array, start, end) {if (!array || start == end) {return null;}const pivot = array[end - 1];let i = start - 1;for (let j = start; j <= end-1; j++) {if (array[j] < pivot) {i++;array[i] = array[j] + array[i] - (array[j] = array[i]);/* swap ith and jth element */}}i++;// swap ith and ni1th elementarray[i] = array[end - 1] + array[i] - (array[end - 1] = array[i]);return i;}function quickSort(array, start, end) {if(start < end) {const partionedElement = quickPartition(array, start, end);if (partionedElement == null) {return}quickSort(start, partionedElement-1);quickSort(partionedElement+1, end);}}var array = [1, 2, 9, 10, 15];quickSort(array, 0, array.length);Doubts
When pivot is first node, then consider left as beg+1, and right as last index. Why do we start traversing right to left first, why not right to left.
ANSWER: Because leftmost node is pivot node. Left me element aaega jo chota hoga pivot se, and chota element hamesha right wala hi dega, left wala to bada element right me fekta h. Also the logic, pivot se bade utha kr pivot ke baad feko, chote utha kar pivot se pele feko. Left aaega to bada element uthega and pivot se replace krne pr bada element pivot se pele aa jaega.
Eg: 20 22 15 10, considering 20 as pivot node
Left se start kre to 22 > 20, so left se to chote hone chahie pivot se, so replace
22 20 15 10: Ye to ulta ho gya. That’s why, start loop from the right to left first.
When pivot is left most, to pivot ki initial value kya hoti h?
Ek algo or tha, jisme for loop and j++ krte the.
Pivot node kese select kre? Left, middle or right?
ANS: Teeno ka median
Chosing pivot randomly:
To avoid this, you can pick random pivot element too. It won't make any difference in the algorithm, as all you need to do is, pick a random element from the array, swap it with element at the last index, make it the pivot and carry on with quick sort.
Complexities:
Worst Case: When the array is already sorted. O(n^2)
We can eliminate this case by choosing pivot node as median of first, last and the medium node, and replace that with the last node to keep using the same algorithm.
Best case: When the pivot node always come out to be the medium node in each case O(nLogn).
Avg case: (nLogn).To do average case analysis, we need to consider all possible permutation of array and calculate time taken by every permutation which doesn’t look easy.
We can get an idea of average case by considering the case when partition puts O(n/9) elements in one set and O(9n/10) elements in other set. Following is recurrence for this case.
T(n) = T(n/9) + T(9n/10) + \theta(n)
Solution of above recurrence is also O(nLogn)
Space complexity: Space Complexity: O(n*log n)
AFTER HAVING THE WORST CASE COMPLEXITY OF O(n^2), WHY DO WE USE QUICK SORT???
Although the worst case time complexity of QuickSort is O(n2) which is more than many other sorting algorithms like Merge Sort and Heap Sort, QuickSort is faster in practice, because its inner loop can be efficiently implemented on most architectures, and in most real-world data. QuickSort can be implemented in different ways by changing the choice of pivot, so that the worst case rarely occurs for a given type of data. However, merge sort is generally considered better when data is huge and stored in external storage.
For arrays, merge sort loses due to the use of extra O(N) storage space.
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 MergeSort is preferred over QuickSort for Linked Lists?
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. 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.
What is 3-Way QuickSort?
In simple QuickSort algorithm, we select an element as pivot, partition the array around pivot and recur for subarrays on left and right of pivot.
Consider an array which has many redundant elements. For example, {1, 4, 2, 4, 2, 4, 1, 2, 4, 1, 2, 2, 2, 2, 4, 1, 4, 4, 4}. If 4 is picked as pivot in Simple QuickSort, we fix only one 4 and recursively process remaining occurrences. In 3 Way QuickSort, an array arr[l..r] is divided in 3 parts:
a) arr[l..i] elements less than pivot.
b) arr[i+1..j-1] elements equal to pivot.
c) arr[j..r] elements greater than pivot.
See this for implementation.
TODOS:
Pivot as middle
Src: https://www.geeksforgeeks.org/quick-sort/
I am a web developer and software engineer currently living in Delhi, India. My interests range from technology to entrepreneurship. I am also interested in running, fitness, and programming.
Sunday, 17 May 2020
Quick Sort Implementation in JS | Learn Data Structure with Shubham part 1
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment