Having a trouble with QuickSort - sorting

I have an array.
7,5,4,6,9,8
Pivot is the first element which is 7. So, the left cursor is 5 and right cursor is 8. After comparison between pivot and (left and right cursor), both cursor met at 6. So, the left cursor is at 6 and the right cursor is at 6. I am not sure what should I do after this step. Should I swap 6 with pivot,7 or should I swap 4 with pivot?
Thanks.

You should swap 6 with the pivot 7.
The algorithm states that once the right cursor is at the same position or farther left than the left cursor (the left cursor passed the right cursor), it should stop iterating. In this case, they both point to 6 as you correctly showed. Once the cursors stop iterating, you are supposed to swap the pivot with the element that the right cursor is pointing to, which is 6 in this case.
So, 7 is now the partition element in the array, and the next step is to recursively call quicksort on the elements to the right of 7, and the elements to the left of 7. As you can see, all the elements to the right are >7, and all the elements to the left are <7. Continue the same process you laid out above and the array will be sorted.

Related

I get a different quicksort results from the solution

The problem is to quicksort the word ELECTRONIC. I am working through every line manually, and the solution given is stepped. After the first step I have a different state from the solution and don't understand why.
I choose the pivot from the median of ETC (positions 0,4 and 9) and select E. This pivot is swapped with the last position, 9, and gives:
0123456789
CLECTRONIE
I increment i from C on the left and decrement j from I on the right and eventually swap positions 1(L) and 4(C) giving
0123456789
CCELTRONIE
Continuing to increment and decrement i and j respectively, the eventually cross with i at position 3, L, and this is swapped with the pivot at position 9 giving:
0123456789
CCEETRONIL
Now with the pivot at position 3 I thought the partition would be
CCE |E| TRONIL
but the solution I have states:
Quicksort ELECTRONIC
choose pivot: median(E,T,C)=E
partition using E: ECC|E|LTRONI
...
I understand the letters in Sl and Sr are the same, but I think the order is important. Can anyone identify where I have gone wrong, or how the solution gets this state please? Anything is appreciated.
Your partition achieves the main goal - left part contains lesser (or equal) elements, right part contains greater elements. Partition is finished. Task
(for current stage) is completed successfully.
Order of elements in these parts depends on partition implementation (there are different schemes) and does not influence on sorting correctness and speed.

My quicksort desk test doesn't seem to be correct

I'm currently practicing quicksort and it worked fine till now. But I could find an example where I fail (because it seems to be a Special case and I didn't read about it yet, that's why I do it wrong.. cannot solve it). The Problem is I think because I choose the smallest element as pivotelement:
9 1 4 2 7 *0* (star mark means pivotelement)
Now I set i,j where i will go through Array (move to right) till it finds an element which is greater than the pivotelement. And j will go through Array (move left) till it finds an element that is lower than the pivotelement. Found, we Switch the elements where i and j Show at. We do this till i and j crossed (aka "j is before i"). In this case we Switch the element i Shows to and index i with pivotelement... I don't want describe the entire algorithm now or it will be Long question..
9 1 4 2 7 *0*
i j but now we cannot find a j that is lower than Pivotelement. What we do?
I would continue by switching i with pivotelement:
*0* 1 4 2 7 9
j i But now is the Problem that i and j are in other positions
(j is before i). I have no idea.. Please clarify and help..
What you have is fine, you have successfully pivoted.
The pivot is in the correct position and both sides of the pivot satisfy the condition that all elements to the left are less and all elements to the right are greater. The i index gets to a bad position on the last move of the pivot, but you dont care that you have broken that because you are finished with it anyway. Now you recurse and quick sort the left and right sides of the pivot -- except there is no left side so in this corner case you only recurse to the right.
QuickSort works on the concept of partitioning. In every iteration it picks up a pivot element and tries to place it in the position where it would finally be placed.
In the beginning i pointer is always kept at index -1(which obviously isn't possible so we just assign i the value -1 and only reference an array element after we have incremented it).
So in your scenario after the first iteration you get,
0 1 4 2 7 9
which is the correct position for the chosen pivot element.
Explanation : Since i is initially at -1, you decrement j pointer till you get an element which is less than your current pivot element(0). This brings j all the way to index 0(you need to add those conditions to your code to make sure no illegal memory space is accessed).
Finally i+1(where i is still -1) is replaced with the pivot element, so effectively 9 is replaced with 0 and viola!
Refer to this link for a more comprehensive explanation.

Quick sorting algorithm states using middle element as pivot

I need help understanding exactly how the quick sort algorithm works. I've been watching teaching videos and still fail to really grasp it completely.
I have an unsorted list: 1, 2, 9, 5, 6, 4, 7, 8, 3
And I have to quick sort it using 6 as the pivot.
I need to see the state of the list after each partition procedure.
My main problem is understanding what the order of the elements are before and after the pivot. So in this case if we made 6 the pivot, I know the numbers 1 - 5 will be before 6 and 7 - 9 will go after that. But what will the order of the numbers 1 - 5 be and 7 - 9 be in the first partition given my list above?
Here is the partition algorithm that I want to use (bear in my I'm using the middle element as my initial pivot):
Determine the pivot, and swap the pivot with the first element of the list.
Suppose that the index smallIndex points to the last element smaller than the pivot. The index smallIndex is initialized to the first element of the list.
For the remaining elements in the list (starting at the second element)
If the current element is smaller than the pivot
a. Increment smallIndex
b. Swap the current element with the array element pointed to by smallIndex.
Swap the first element, that is the pivot, with the array element pointed to by smallIndex.
It would be amazing if anyone could show the list after each single little change that occurs to the list in the algorithm.
It doesn't matter.
All that matters - all that the partitioning process asserts - is that, after it has been run, there are no values on the left-hand side of the center point that emerges that are greater than the pivot and that there are no values on the right-hand side that are less than the pivot value.
The internal order of the two partitions is then handled in the subsequent recursive calls for each half.

quickest way to find out if an element is in the left or right subtree

If a binary tree is constructed as follows
the root is 1
the left child of an element n is 2*n
the right child of an element n is (2*n)+1
if I get a number n, what's the quickest way to find out if it's in the left or right subtree of the root? Is there some easy-to-determine mathematical property of the left subtree?
note: this is NOT a homework question, though it is part of a bigger algorithmic problem I'm trying to solve.
Consider the numbers in binary. Each child will be the parent number with a 0 or 1 appended to it depending on whether it is left or right.
This means that everything to the left of the root will start 10 in binary and anything to the right will start 11 in binary.
This means that you should be able to work out which side it is on just using some shift operations and then some comparisons.
I should note that I don't know if this is the most efficient method but it is a easy to determine mathematical property of the left subtree.
As noted by others the consequence of the 0 or 1 appendation means that each digit encodes the path through the subtree. The first 1 represents the root of the tree and from then on a 0 will mean taking the left branch at that point and a 1 will mean taking the right branch.
Thus binary 1001101 would mean left, left, right, right, left, right.
An obvious consequence of this is that the number of binary digits will determine exactly how deep in the tree that number is. so 1 is the top (1st) level. 10 would be at the second level (one choice made). my example 1001101 would be at the 7th level (six choices made). I should note that I'm unfamiliar with Binary tree terminology so not sure if the root would usually be considered the first or zeroth level which is why I am being explicit about number of choices made too.
One last observation in case it hasn't already been observed is that the numbers will also be assigned counting from top to bottom, left to right. So the first level is 1. The next is 2 on the left, 3 on the right. The level below that will go 4, 5, 6, 7 and then the row below that 8, 9, 10, 11, 12, 13, 14, 15 and so on. This isn't any more useful mathematically but if you are trying to visualise it may well help.
Following from Chris' observation, there's a very simple rule: Let x be the node you are looking for. Let S be the binary representation of x. Then the digits in S after the first from most significant tell you the path from the root: 0 means go left, 1 means go right.
Example: x = 2710 = 110112, so we need to go right, left, right, right to get there (the leading 1 is ignored).
The reason why this is true is that if you go right, you multiply by 2 (binary left shift by 1) and add a 1, so you are essentially appending a 1. Conversely, if you go left, you append a 0.

How to quicksort this data (step by step) with my own chosen pivot

I have data (3 is the pivot):
281374
i move two pointers from left and right
first i have 2 and 4 so i swap nothing
then i have 8 and 7 so i swap and have:
273184
now i have pinter in 3 and 1 so I swap:
271384
now left pointer = rightpointer - 1 so i should quick sort these:
271 | 384
separately right?
but if i do i will get something like this:
127 | 348
and this is NOT SORTED data!
what I got wrong?
The problem is:
then i have 8 and 7 so i swap and have:
273184
you do not swap these values. Everything from the left greater than the pivot got to be placed on the right, everything on the right lower than the pivot has to be placed on the left. But 7 and 8 are both GREATER than the pivot element.
see people dancing the quicksort algorithm:
http://www.youtube.com/watch?v=ywWBy6J5gz8
HTH
QuickSort doesn't work that way
if 3 is your pivot:
swap your pivot (3), with the end
281374 -> 281473
281473
-
is 2 less than 3? yes, but its already on the left so leave it alone
281473
-
is 8 less than 3? no, so we need to figure out the next correct place to put it.
Walk down the list and the first item <= the pivot (it can be the pivot itself) we swap.
In this case, its a 1 so....
281473 -> 218473
now we're at
218473
-
repeat til sorted.
You wouldn't swap corresponding numbers if the left is greater than the right, you would swap them depending on how they compare to the pivot value. 7 is greater than the pivot value already, so it's already on the correct (right) side of the pivot.
However, the standard algorithm for in-place partitioning is fairly different from what you seem to be attempting. Take a look at the Wikipedia article for in-place Quicksort.
3 is pivot then check 2:2 is less than 3 so do nothing
8 is greater than pivot so it moves to end of array
1 is less than 3 :do nothing
and other elements are greater than 3 and in their correct place
now we have:213748
now we take this array apart to two sub arrays and get first elements of them pivot and sort them with quick sort
123748
123478
hip hip hurray
we sort it

Resources