In: Computer Science
Given the unordered array:
[0] |
[1] |
[2] |
[3] |
[4] |
[5] |
[6] |
[7] |
[8] |
P |
E |
R |
Y |
I |
H |
J |
L |
S |
Suppose this array were being sorted using the quick sort algorithm from the course content,
into ASCENDING order, with the left-most item as the pivot value.
List the letters in the resulting array, in order AFTER the FIRST PARTITIONING.
[0] |
[1] |
[2] |
[3] |
[4] |
[5] |
[6] |
[7] |
[8] |
Code
used:
private static int partition(int start, int end)
{
char pivot = a[start];
int p1 = start + 1;
for (int i = start + 1; i <=
end; i++) {
if (a[i] <
pivot) {
swap(i, p1);
p1++;
}
}
a[start] = a[p1 - 1];
a[p1 - 1] = pivot;
return p1 - 1;
}
original array: [P, E, R, Y, I, H, J, L, S]
Swapping E and E
[P, E, R, Y, I, H, J, L, S]
Swapping I and R
[P, E, I, Y, R, H, J, L, S]
Swapping H and Y
[P, E, I, H, R, Y, J, L, S]
Swapping J and R
[P, E, I, H, J, Y, R, L, S]
Swapping L and Y
[P, E, I, H, J, L, R, Y, S]
Final array:
[0]L |
[1]E |
[2]I |
[3]H |
[4]J |
[5]P |
[6]R |
[7]Y |
[8]S |