Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Fix the buggy version of quicksort package labs; public class lab6 { public static void main(String args[]) { int arr[] = {10, 7, 8, 9,

Fix the buggy version of quicksort

package labs;

public class lab6 {

public static void main(String args[]) {

int arr[] = {10, 7, 8, 9, 1, 5,6,7};

int arr2[] = {9, 8, 7, 6, 5, 4, 3, 2, 1};

int arr3[] = {1, 3, 5, 3, 2, 6, 20};

quicksort(arr,0,arr.length-1);

quicksort(arr2,0,arr2.length-1);

quicksort(arr3,0,arr3.length-1);

System.out.println(Arrays.toString(arr));

System.out.println(Arrays.toString(arr2));

System.out.println(Arrays.toString(arr3)); }

}

output should be:

>>>[1,5,6,7,7,8,9,10]

>>>[1,2,3,4,5,6,7,8,9]

>>>[1,2,3,3,5,6,20]

Here is unfinised code to fix:

private static int partition(int[] items,int low, int high) {

int i=0;

int j=items.length-1;

int k=items.length-1;

int pivot=items[low];

while(j < items.length)

{

if (items[j] > pivot)

{

swap(j,i,items);

j++;

i++;

}

else if(items[j] < pivot) {

j++; }

else {

swap(j,k-1,items);

k--;

}

}

return i;

}

private static void swap(int i,int j,int[] items) {

int temp=items[i];

items[i]=items[j];

items[j]=temp;

}

public static void quicksort(int[] arr) {

quicksort(arr,5,arr.length-1);

}

private static void quicksort(int[] arr, int low, int high) {

if (low < arr.length) {

int pi = partition(arr,low,high);

quicksort(arr, low, high);

quicksort(arr, low, high);

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeffrey A. Hoffer Fred R. McFadden

4th Edition

0805360476, 978-0805360479

More Books

Students also viewed these Databases questions