Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider list = { 5, 4, 2, 9, 1, 7, 3, 8, 6 } and the quick sort algorithm. Assu

ID: 3713087 • Letter: C

Question

Consider list = { 5, 4, 2, 9, 1, 7, 3, 8, 6 } and the quick sort algorithm. Assume we always choose the first list item in the list being partitioned as the pivot. Trace the partition method showing how list is partitioned into listL andlistR. To get you started, here is the format of what I am looking for in your solution (next page):

list = { 5, 4, 2, 9, 1, 7, 3, 8, 6 }, pivot = 5, leftIndex = -1, rightIndex = 9 While loop pass 1:

leftIndex ends up at 0, rightIndex ends up at 6

leftIndex < rightIndex so swap list[0] and list[6]: list = { 3, 4, 2, 9, 1, 7, 5, 8, 6 }

While loop pass 2:

...

While loop pass 3:

...
While loop terminates because leftIndex = ??? >= rightIndex = ???partition() returns ??? so listL = { ??? }, listR = { ??? },

Explanation / Answer

Solution:

The list is:

list= { 5, 4, 2, 9, 1, 7, 3, 8, 6 }

According to the instruction the pivot is selected as 5

pivot:=> 5
L: 1 R: 8 (please note that here L and R are indicating the left and right index of the given list)
While loop pass 1
L ends up at 5 and R ends up at 4
now,

swap list[5] and list[4]:
The swapped list is=> 1 4 2 3 5 7 9 8 6
Partition returns: 4

Now the left sublist is
1 4 2 3
the right sublist is:
7 9 8 6

taking left sublist
pivot:=> 1
L: 1 R: 3
While loop pass: 1
i ends up at 1 and j ends up at 0
now swap list[1] and list[0The swapped
ped list is: 1 4 2 3 5 7 9 8 6
Partition returns: 0
left list is:

right list is:
4 2 3
pivot:=> 4
L: 2 R: 3
While loop pass: 1
i ends up at 4 and j ends up at 3
now swap list[4] and list[3]:
The swapped list is: 1 3 2 4 5 7 9 8 6
Partition returns: 3
left list is:
3 2
right list is:

pivot:=> 3
L: 2 R: 2
While loop pass: 1
left index ends up at 3 and left index ends up at 2
now swap list[3] and list[2]:
The swapped list is: 1 2 3 4 5 7 9 8 6
Partition returns: 2
left list is:
2
right list is:

pivot:=> 7
L: 6 R: 8
While loop pass: 1
i ends up at 7 and j ends up at 6
now swap list[7] and list[6]:
The swapped list is: 1 2 3 4 5 6 7 8 9
Partition returns: 6
left list is:
6
right list is:
8 9
pivot:=> 8
L: 8 R: 8
While loop pass: 1
i ends up at 8 and j ends up at 7
so swap list[8] and list[7]:
Swapped list is: 1 2 3 4 5 6 7 8 9
Partition returns: 7
left list is:

right list is:
9
1 2 3 4 5 6 7 8 9

This is the final sorted array.

I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote