Question
Consider list = { 5, 4, 2, 9, 1, 7, 3, 8, 6 } and the quick sort algorithm. Assume we always choose the first
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 and listR.
To get you started, here is the format of what I am looking for in your solution:
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 9
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 = { ??? },
This is what I have but it I think I did it wrong.
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 9
leftIndex < rightIndex so swap list[0] and list[6]: list = { 1, 4, 2, 9, 5, 7, 3, 8, 6 }
While loop pass 2:
leftIndex ends up at 1, rightIndex ends up at 8
leftIndex < rightIndex so swap list[1] and list[2]: list = { 1, 2, 4, 9, 5, 7, 3, 8, 6 }
While loop pass 3:
leftIndex ends up at 2, rightIndex ends up at 7
leftIndex < rightIndex so swap list[2] and list[6]: list = { 1, 2, 3, 9, 5, 7, 4, 8, 6 }
While loop pass 4:
leftIndex ends up at 3, rightIndex ends up at 6
leftIndex < rightIndex so swap list[3] and list[6]: list = { 1, 2, 3, 4, 5, 7, 9, 8, 6 }
While loop pass 5:
leftIndex ends up at 5, rightIndex ends up at 4
leftIndex < rightIndex so swap list[5] and list[8]: list = { 1, 2, 3, 4, 5, 6, 9, 8, 7 }
While loop pass 6:
leftIndex ends up at 6, rightIndex ends up at 8
leftIndex < rightIndex so swap list[6] and list[8]: list = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }
While loop terminates because leftIndex = 8 >= rightIndex = 6
partition() returns ??? so listL = { 1, 2, 3, 4 }, listR = { 5, 6, 7, 8, 9},
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started