Hello, I'm having trouble executing QuickSort and I've followed the steps one by one but the result is not what i was expecting.
The outcome is supposed to be a list where everything after pivot is greater than it and everything before pivot is less than it. Im sure I am doing something wrong but ive spend so much time trying to solve it again and again and i always have the same output.
Help would be appreciated!
#Having trouble with QuickSort
70 messages · Page 1 of 1 (latest)
- Wait patiently for a helper to come along.
- Once someone helps you, say thank you and close the thread with:
+close
- Feel free to nominate the person for helper of the week in #helper-nominations
- Do not ping the mods, unless someone is breaking the rules.
- If you're happy with the help you got here, and the server overall, you can contribute financially as well:
what do you pivot around?
Pivot is the first element of the list, 7.
Index 0
I'm not sure what you've done here, we're not solving QuickSort with trees but with the following way:
Pivot is element 0
F is element 1
L is element N-1
F moves to the right till it finds a value greater than pivot.
L moves to the left till it finds a value less than pivot.
Swapping elements F and L.
This goes on while F<L.
Swapping pivot with F unless L is less than F.
The algorithm continues but at this point, everything before pivot should be less than it and everything after pivot greater than it.
have to be more precise than that
F moves to the right till it finds a value greater than pivot.
L moves to the left till it finds a value less than pivot.
in what order does this happen
You can't really swap 6 and 9. Can you? Because your F is bigger than L here.
according to the algorithm, if low > high, nothing should happen, the while loop would stop and
pivot should change with first/low
and it would be 7 6 9 8 -> 6 7 9 8
6 7 9 8
you have nested loops here
damn
yeah
wait no
Yes, so what you have shown in second row would not happen.
This is correct.
wait why would it be correct?low would've already changed
why won't you do sorting with binary trees? 
same, just recursive instead of nested loop nonsense
thats how it is teached in uni
we will learn abt trees and data structs in next semester
rip
yeah...
I am trying to say that that is the correct form your array should obtain after completion of algorithm for this pivot.
Btw, show rest of your code. This much seems fine. You might be making an error in next function calls.
this is not my code, its the code from uni so it shouldnt be wrong, im just tryna understand how quicksort works
my whole problem is why this specific algorithm doesnt work with 7 6 9 8
It is working fine.
Once the while loop ends, you have this instance:
first = 0
low = 2
high = 1
Now, it will run the if clause at the end and set list[first] = list[high] = 6.
Then, list[high] = pivot = 7
Your array is now
6 7 9 8.
first and low is the same thing, did you mean pivot?
wait
No. They are not the same.
Yes
More precisely, it's the starting index of subarray to be partitioned but in this case, it's zero.
the if statement won't run because high > low is false since high = 1 and low = 2
No. If statement after the outer while loop. Did you delete a picture?
I can swear it was there moments ago. Lol
There was a picture with an "if else clause" inside it. I'm talking about that one.
Wherein you are returning the index of pivot after partition is done.
Yes. This one.
if pivot > list[high]
if 0 > 1 (false)
Pivot is not 0.
why
Pivot is value at index 0 which is 7.
aint pivot the index itself
Look at your code.
Pivot = list[first].
No.
oh yes
hmm
wait so because low>high the first if statement didnt happen
and
because pivot > list[high]
which means 7 > 6,
7 and 6 swapped
so the list is now 6 7 9 8
extremely interesting
thanks a lot i shouldve looked at the code in detail
Happy to help.
+close