3.5 Consider list = { 5, 4, 2, 9, 1, 7, 3, 8, 6 } and the quick sort algorithm.
ID: 3873347 • Letter: 3
Question
3.5 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 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
Given list is: 5 4 2 9 1 7 3 8 6
Pivot is: 5
Left index: 1 Right index: 8
While loop pass: 1
left index ends up at 5 and left index ends up at 4
so swap list[5] and list[4]:
Swapped list is: 1 4 2 3 5 7 9 8 6
Partition returns: 4
left list is:
1 4 2 3
right list is:
7 9 8 6
Pivot is: 1
Left index: 1 Right index: 3
While loop pass: 1
left index ends up at 1 and left index ends up at 0
so swap list[1] and list[0]:
Swapped list is: 1 4 2 3 5 7 9 8 6
Partition returns: 0
left list is:
right list is:
4 2 3
Pivot is: 4
Left index: 2 Right index: 3
While loop pass: 1
left index ends up at 4 and left index ends up at 3
so swap list[4] and list[3]:
Swapped list is: 1 3 2 4 5 7 9 8 6
Partition returns: 3
left list is:
3 2
right list is:
Pivot is: 3
Left index: 2 Right index: 2
While loop pass: 1
left index ends up at 3 and left index ends up at 2
so swap list[3] and list[2]:
Swapped list is: 1 2 3 4 5 7 9 8 6
Partition returns: 2
left list is:
2
right list is:
Pivot is: 7
Left index: 6 Right index: 8
While loop pass: 1
left index ends up at 7 and left index ends up at 6
so swap list[7] and list[6]:
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 is: 8
Left index: 8 Right index: 8
While loop pass: 1
left index ends up at 8 and left index 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
...................................................
program in java:
public class quicksort{
static void swap(int[] a,int i,int j){
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
static int partition(int a[],int p,int q){
int key=a[p],i=p+1,j=q,k=1;
System.out.println("Pivot is: "+key);
System.out.println("Left index: "+i+" Right index: "+j);
System.out.println("While loop pass: "+k);
k++;
while(i<=j){
while(i<=q&&a[i]<=key)
i++;
while(j>=p&&a[j]>key)
j--;
if(i<j)
swap(a,i,j);
}
System.out.println("left index ends up at "+i+" and "+"left index ends up at "+j);
swap(a,j,p);
System.out.println("so swap list["+i+"] and list["+j+"]:");
System.out.print("Swapped list is: ");
for(int e=0;e<9;e++)
System.out.print(a[e]+" ");
System.out.println();
return j;
}
static void quick_sort(int a[],int p,int q){
if(p<q){
int j = partition(a,p,q);
System.out.println("Partition returns: "+j);
System.out.println("left list is:");
for(int e=p;e<j;e++)
System.out.print(a[e]+" ");
System.out.println();
System.out.println("right list is:");
for(int e=j+1;e<=q;e++)
System.out.print(a[e]+" ");
System.out.println();
quick_sort(a,p,j-1);
quick_sort(a,j+1,q);
}
}
public static void main(String args[]){
int[] a = {5,4,2,9,1,7,3,8,6};
System.out.print("Given list is: ");
for(int i=0;i<9;i++)
System.out.print(a[i]+" ");
System.out.println();
quick_sort(a,0,8);
for(int i=0;i<9;i++)
System.out.print(a[i]+" ");
System.out.println();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.