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

Another question on Computers and Technology

question
Computers and Technology, 23.06.2019 09:30
Light travels at a speed of 186,000 miles a second. the distance light travels in a year is 5,865,690,000,000 miles/year 5,865,695,000,000 miles/year 58,656,950,000,000 miles/year 6,789,000,0000 miles/year
Answers: 1
question
Computers and Technology, 23.06.2019 18:30
This program should be a short piece of code that prints all of the positive integers from 1 to 100 as described more fully below. the program may contain multiple methods, and if using an oo language, should be contained within a single class or object. the program should be designed so that it begins execution when invoked through whichever mechanism is most common for the implementation language. â–ş print out all positive integers from 1 to 100, inclusive and in order. â–ş print messages to standard output, matching the sample output below. â–ş in the output, state whether the each integer is 'odd' or 'even' in the output. â–ş if the number is divisible by three, instead of stating that the number is odd or even, state that the number is 'divisible by three'. â–ş if the number is divisible by both two and three, instead of saying that the number is odd, even or divisible by three; state that the number is 'divisible by two and three'. â–ş design the logic of the loop to be as efficient as possible, using the minimal number of operations to perform the required logic. sample output the number '1' is odd. the number '2' is even. the number '3' is divisible by three. the number '6' is divisible by two and three.
Answers: 1
question
Computers and Technology, 24.06.2019 06:30
Me and category do i put them in because this is science
Answers: 1
question
Computers and Technology, 24.06.2019 07:30
Consider the folloeing website url: what does the "http: //" represent? a. protocal identifier. b. ftp. c. domain name d. resource name
Answers: 2
You know the right answer?
In the QuickSort algorithm, the partition method we developed in class chose the start position for...
Questions
question
Mathematics, 21.12.2019 03:31
question
Mathematics, 21.12.2019 03:31
question
Advanced Placement (AP), 21.12.2019 03:31
Questions on the website: 13722367