subject

In the QuickSort algorithm, the partition method we developed in class chose the start position for the pivot. We saw that this leads to worst case performance, O(n2), when the list is initially sorted. Try to improve your QuickSort by choosing the value at the middle, instead of the value at the start, for the pivot. Test your solution with the Driver you used for homework. Upload the output produced by the Driver, and your modified QuickSort source file.

ORIGINAL

public class QuickSort> implements Sorter

{

List list;

public void sort(List list)

{

this. list = list;

qSort(0, list. size() -1);

}

public void qSort(int start, int end)

{

if(start >= end)

return;

int p = partition(start, end);

qSort(start, p-1);

qSort(p+1,end);

}

public int partition(int start, int end)

{

int p = start;

E pivot = list. get(p);

for(int i = start+1; i <= end; i++)

if(pivot. compareTo(list. get(i)) > 0)

{

list. set(p, list. get(i));

p++;

list. set(i, list. get(p));

}

list. set(p, pivot);

return p;

}

}



Driver

public class DriverQuicksort

{ static final int MAX = 20;

public static void main(String[] args)

{

Random rand = new Random(); // random number generator

List numbers = new ArrayList ();

Sorter sorter;

sorter = new QuickSort ();

// Test QuickSort with random input

System. out. println ("Testing Quicksort");

for (int i=0; i
numbers. add (rand. nextInt(50)); // random int in [0..49]

System. out. println ("Before sorting:");

System. out. println (numbers);

sorter. sort (numbers );

System. out. println ("After sorting:");

System. out. println (numbers);

System. out. println ();

// Test QuickSort with ascending input

numbers. clear();

for (int i=0; i
numbers. add (i * 10); // initially in ascending order

System. out. println ("Before sorting:");

System. out. println (numbers);

sorter. sort ( numbers);

System. out. println ("After sorting:");

System. out. println (numbers);

System. out. println ();

// Test QuickSort with descendng input

numbers. clear();

for (int i=0; i
numbers. add (MAX-i); // initially in ascending order

System. out. println ("Before sorting:");

System. out. println (numbers);

sorter. sort ( numbers);

System. out. println ("After sorting:");

System. out. println (numbers);

System. out. println ();

numbers. clear();

numbers. add(75);

numbers. add(93);

numbers. add(35);

numbers. add(0);

numbers. add(75);

numbers. add(-2);

numbers. add(93);

numbers. add(4);

numbers. add(6);

numbers. add(76);

System. out. println ("Before sorting:");

System. out. println (numbers);

sorter. sort(numbers);

System. out. println ("After sorting:");

System. out. println (numbers);

System. out. println ();

}

}

ansver
Answers: 1

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 10:20, alcantar28eduin
Print "usernum1 is negative." if usernum1 is less than 0. end with newline. convert usernum2 to 0 if usernum2 is greater than 10. otherwise, print "usernum2 is less than or equal to 10.". end with newline
Answers: 3
image
Computers and Technology, 23.06.2019 19:30, bevanscory123
What are loans to a company or government for a set amount of time
Answers: 1
image
Computers and Technology, 24.06.2019 01:00, kayranicole1
What shows the web address of the page that is currently displayed in the workspace? status window toolbar location bar internet box
Answers: 1
image
Computers and Technology, 24.06.2019 01:30, tanya44737
Hazel has just finished adding pictures to her holiday newsletter. she decides to crop an image. what is cropping an image?
Answers: 1
You know the right answer?
In the QuickSort algorithm, the partition method we developed in class chose the start position for...

Questions in other subjects:

Konu
Physics, 31.08.2019 21:00