0
Partition of an array
Somebody can help me with this exercise: I have to make a partition of an array of integers. The code I wrote works for a half, that's because in the end there could be some element not correctly partitioned. Here's my code: https://code.sololearn.com/cku2om5sa5xb/?ref=app
6 ответов
+ 1
Can you explain a bit more?
I think , its better to use for loop instead of while. There you missing some iterations...
0
Jaya krishna The thing is this:
The program start searching left elements untill arrives at middle position, which I suppose to be the array initial position. If the left element is bigger than the pivot it start searching from the right, when it finds an element minor than the pivot then an exchange is made between array(left) and array (right). It works, almost works at least. The problem is that if I can't find bigger element on the left or minor element on the right the program doesn't work properly, I don't know if I've been clearer this time
0
In total code, you are performing operations for first pivot only. After that you are not changing pivot value. You are missing this.. After first iteration you should update pivot value. And repeat the process until left>right..
So repeat steps in for loop by updating pivot value...
0
Indeed I want the pivot to remain the same for all the program. Ex:
I have this array (N=8):
3 7 10 5 9 1 2 16
The pivot is 5 (pivot=array[N/2])
So it begins, is 3<=5? Yes, so go on.
Is 7<=5? No, then go to the right, is 16>=5 yes, than go on. Is 2>=5? No, exchange them:
3 2 10 5 9 1 7 16
Now repeat from left+1 untill N/2. Pivot should remains 5.
0
Gandalf the Grey
Pivot = array(N/2) is here 9 not 5.
N/2=4
Array[4] =9
0
Gandalf the Grey
In the 2nd while loop, it should be > not <. Change this. And check again... like
while(right>=middle || a[right]>a[light])
and final output for the above example you will get
3 2 1 5 9 10 7 16
is this what you want output?