I have a question about quick sort ..
Does anyone have an idea on how to chose the Pivot element so that quick sort needs n choose 2 comparisons ?
And Thank you.
Related
I hate to just post a question regarding homework, but I am having a lot of trouble as to what they are trying to ask from me. I am not asking you to solve my homework problem, only to guide me as to the first steps I should take because I don't know where to begin on this. I read the part of the chapter about quick sort and sort of grasp it and watched a video on it too.
Here's the homework problem:
Sort an array of 10,000 elements using the quick sort algorithm as follows:
a. Sort the array using pivot as the middle element of the array.
b. Sort the array using pivot as the median of the first, last, and middle elements of the array.
c. Sort the array using pivot as the middle element of the array. How- ever, when the size of any sublist reduces to less than 20, sort the sublist using an insertion sort.
d. Sort the array using pivot as the median of the first, last, and middle elements of the array. When the size of any sublist reduces to less than 20, sort the sublist using an insertion sort.
e. Calculate and print the CPU time for each of the preceding four steps.
I'm having trouble understanding - is this going to be one whole function that contains these four steps, or four different functions each doing each step? I get what it means by step a in using pivot as middle element of array (Let's say you have 10 array elements and the middle would be element #4) but I don't get what it means by "sort the array using pivot as the median of the first, last, and middle elements of the array"
I need insight into the inner workings of quick sort and what this book is asking from me.
It has been discussed in many places many times I believe, but I have been searching for 3 days now and did not realize how this is happening. My question is:
Where is the second pointer, if we take the first or the last element as a pivot?
What I literally mean:
Case 1
Central array element as a pivot
This is all clear as 5 is in the center, we go ahead and find the elements that comply the conditions:
1.) element < pivot for the left side if found we stop the pointer
2.) element > pivot for the right side if found we stop the pointer
3.) swap the elements where we stopped the pointers during steps 1 and 2
Case 2: (unclear one)
first or last element as a pivot
Here is unclear where should I put both pointers to start the finding of an elements, and which direction regarding pivot should I move? Should this be two pointers as well, and how should they be moved?
Still should be exectly two pointers?
If two pointers are being used, it's some variation of Hoare partition scheme, where the pointers start off at the first and last element of the array, and advance towards each other while scanning for "out of place" elements. If the first or last element is chosen as the pivot, the first swap will involve the pivot.
Note that Hoare algorithm normally includes the pivot in one of the partitions. This creates an issue if using the first or last element as a pivot, as data patterns like already sorted data would result in stack overflow as the split will always be 0 elements and n elements. To resolve this issue, the algorithm would need to exclude the pivot in the recursive calls.
10,2,5,15,20,9,17,8,25,30,4
what would be the result of 1st round of the quick sort? the first element is the pivot which is 10 in this example
here is my answer:
10,4,8,9,20,15,17,5,25,30,2
I'm confuse. I googled and watch some videos about using quick sort algorithm but it seems there are different ways on how to do this algorithm . can someone give me advice?
The answer would be :
4,8,9,5,2,10,20,15,17,25,30
Quicksort takes the pivot and put it in the right place. The it start again with the two subarrays that have been created.
For the first round, it means that any element that is smaller than the pivot will be before the pivot, and any element bigger, will be after.
I am learning quick sort from Introduction to Algorithms.I got stuck on question 7.1-2. of Chapter 7 Quicksort-
"What value of q does PARTITION return when all elements in the array A[p…r] have the same value? Modify PARTITION so that q=⌊(p+r)/2⌋ when > all elements in the array A[p…r] have the same value."
The first part is easy and the answer is definitely r.But I can't even figure out what the second part is asking.I mean what is the reason for setting the pivot to (p+r)/2.Further I can't understand the solutions I found on searching on Google.
Please help me in understanding what is the advantage of this modification in case all elements are equal and if possible please provide the algorithm to do so.
By setting the pivot to the middle of p and r, we divide the array of size n into two sub-problems of equal size n/2. If you draw the recursion tree for the following recurrence, you would see its height as O(lgn)
T(n) = 2T(n/2)+O(n)
Imagine if the position of the pivot returned from the partition is always the last element in the array. Then the recurrence for the run time would be
T(n) = T(n-1)+O(n)
Do you see now why is it inefficient if the recursion tree is like a linked list? Try drawing the tree and adding the costs at each node in both the cases.
Also modifying the partition method to return (p+r)/2 is easy.
Hint: One easy way is split <= in the if condition of the partition.
I'm learning quicksort from Sedgwick's book. One of his exercise problems is
Write a program that sorts an array that is known to contain just two distinct key values. View Solution
I think I understand the solution. It runs in O(n). I don't however understand the relation between this and quicksort? I fail to see any sort of partitioning like in quick sort. All that is done here is deposit lesser and greater elements beyond respective boundaries pointed to by lt and gt.
Solution is just like quicksort whit single pass.
Quick sort pseudo code:
1. Check stopping condition
2. Pick one number called pivot
3. Move smaller or equal elements than pivot on left part of array
4. Move greater elements than pivot on right part of array
5. Quicksort left part
6. Quicksort right part
Where soultion you looking for is same as quicsort exept 1,5,6
1. Pick one number called pivot
2. Move smaller or equal elements than pivot on left part of array
3. Move greater elements than pivot on right part of array