问题
Currently trying to understand how the QuickSort Algorithm operates before I implement it into code, I have the unsorted array:
{7,8,2,5,1,9,3,6}
In the case of this question, the right most element in the array has been picked as the pivot, 6. I have gone through the array comparing each element with 6(pivot) and depending if the array element is less than or bigger than six, done the appropriate action. As a result, now all values less than 6 are on the left and all values greater than 6 are on the right The array now looks like this
{2,5,1,3,6,7,9,8}.
As many tutorials have stated we now essentially have two smaller arrays
{2,5,1,3} and {7,9,8}
I am stuck here on what to do next, as every different tutorial picks a different pivot point making it hard to follow. Do i do the same thing again in my two smaller arrays?
If someone could show me how to sort the {2,5,3,1}
array and explain how you did it that would be great I will then do {7,9,8}
myself.
回答1:
You need to do the same thing on two segments once you got the position of your first pivot. Call method on left and right segments recursively.
Now partition on your two subsets {2,5,1,3}
and {7,9,8}
.
You will pick 3 as pivot on {2,5,1,3}
and get {2,1}
and {5}
for first segment and do similar for the other segment.
{2,5,1,3}
^
{2,1,3,5}
^
pivot (3) at proper position. do the same on left and right segments.
{2,1} and {5}
^
{1,2,3,5}
回答2:
The following animated representation explains how to find the pivot value in an array.
The pivot value divides the list into two parts. And recursively, we find the pivot for each sub-lists until all lists contains only one element.
Steps to achieve QuickSort algorithm:
Step 1 − Choose the highest index value as pivot (can be any index)
Step 2 − Take two variables to point left and right of the list excluding pivot
Step 3 − left points to the low index
Step 4 − right points to the high
Step 5 − while value at left is less than pivot move right
Step 6 − while value at right is greater than pivot move left
Step 7 − if both step 5 and step 6 does not match swap left and right
Step 8 − if left ≥ right, the point where they met is new pivot
The pseudocode for the above algorithm can be derived as −
function partitionFunc(left, right, pivot)
leftPointer = left -1
rightPointer = right
while True do
while A[++leftPointer] < pivot do
//do-nothing
end while
while rightPointer > 0 && A[--rightPointer] > pivot do
//do-nothing
end while
if leftPointer >= rightPointer
break
else
swap leftPointer,rightPointer
end if
end while
swap leftPointer,right
return leftPointer
end function
Quick Sort Algorithm
Using pivot algorithm recursively, we end up with smaller possible partitions. Each partition is then processed for quick sort. We define recursive algorithm for quicksort as follows −
Step 1 − Make the right-most index value pivot
Step 2 − partition the array using pivot value
Step 3 − quicksort left partition recursively
Step 4 − quicksort right partition recursively
Quick Sort Pseudocode
To get more into it, let's see the pseudocode for quick sort algorithm −
procedure quickSort(left, right)
if right-left <= 0
return
else
pivot = A[right]
partition = partitionFunc(left, right, pivot)
quickSort(left,partition-1)
quickSort(partition+1,right)
end if
end procedure
回答3:
The way QuickSort works is through a recursive procedure, which is the procedure you have described. Applying the same procedure on both resulting arrays and you will be fine.
Just to clarify, the base case of this algorithm is when the size of the input array is one, resulting in a sorted array.
回答4:
Quick answer to:
I am stuck here on what to do next, as every different tutorial picks a different pivot point making it hard to follow.
The pivot can be chosen on many different ways, even randomly. Average performance of quick sort is O(n*log n), but worst-case is O(n^2). Depending on which way the pivot is being chosen, the algorithm is more reliable when sorting some "end-case" arrays and thus, the number of cases where its performance is O(n^2) is smaller.
来源:https://stackoverflow.com/questions/42393496/quicksort-algorithm-what-is-next-step-once-first-pivot-is-located-its-final-posi