Question: Note: Please check my quicksort functions. I think the error is because I didn't consider insertFirst function, but it could be because of other errors.
Note: Please check my quicksort functions. I think the error is because I didn't consider insertFirst function, but it could be because of other errors.
What I want is for you to make this code work. When running, the code must sort all the lists.
IMPORTANT!! However, please mind that you ONLY change quicksort.
Do NOT remove any lines from other functions and main. ONLY quicksorts and partition.
package compa;
import java.util.Arrays;
import java.security.SecureRandom;
Simulating a linked list with an array.
public class ListOnArraydGigiYimMingHay
private int data;
int size;
The constructor is slightly different to Lab
But the difference is irrevalent to the task.
public ListOnArraydGigiYimMingHayint n
size n ;
data new intsize;
data; may be omitted in Java.
for int i ; i size ; i
datai i ;
datasize ;
datasize ;
public boolean isEmpty
return data;
public boolean isFull
return datasize ;
public void err
System.out.printlnOops;
Insert a new element as the new head of the list.
public void insertFirstint x
if isFull err; return;
int i datasize ;
datasize datai ;
datai data;
data i;
datai x;
The inplace quicksort algorithm.
VERY IMPORTANT.
I've sought help from the following Internet resources and books:
Running time: O
public void quicksort
if isEmpty
quicksortdata data datasize ;
public void quicksortint array, int start, int end
if end start return; base case
int pivot partitionarray start, end;
quicksortarray start, end;
quicksortarray pivot end;
public static int partitionint array, int start, int end
int pivot array end;
int i start ;
for int j start; j end ; j
ifarrayj pivot
i;
int temp array j;
arrayi arrayj;
arrayj temp;
else if arrayj pivot
return j;
i;
int temp arrayi;
arrayi arrayend;
arrayend temp;
return i;
Output the elements stored in the list in order.
public String toString
if isEmpty return "The list is empty.";
StringBuilder sb new StringBuilder;
int i data;
sbappenddatai;
while datai
i datai;
sbappendappenddatai;
return sbappendutoString;
Todo: add at least ten more test cases to test your code.
Todo: modify the list removing and adding elements and sort it again.
The use of data structures from Java libraries is allowed here and only here.
public static void mainString args
int testData try different inputs.
;
for int a : testData
int n alength;
SecureRandom random new SecureRandom;
ListOnArraydGigiYimMingHay list new ListOnArraydGigiYimMingHayn random.nextIntn ; you don't need to understand this line.
System.out.printlnThe original array: Arrays.toStringa;
for int i n ; i ; i list.insertFirstai;
System.out.printlnThe original list: list;
You can uncomment the following line to print out how the elements are stored
System.out.printlnThe internal storage: Arrays.toStringlistdata;
list.quicksort;
System.out.printlnThe sorted list: list;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
