Question: so everything in my code is functional. it's just that it doesn't sort or satisfy some test cases. I hope you can help me make
so everything in my code is functional. it's just that it doesn't sort or satisfy some test cases. I hope you can help me make it work for all test cases.
The original array:
The original list:
The sorted list:
The original array:
The original list:
The sorted list:
The original array:
The original list:
The sorted list:
Here's the code. Try to focus on changing on the quicksort, helper, partition. Don't delete other codes and try to comment the lines like I did.
import java.util.Arrays;
import java.security.SecureRandom;
public class ListOnArraydUNGSethyPagna
private int data;
int size; Size of array
public ListOnArraydUNGSethyPagnaint n
size n ;
data new intsize;
data;
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;
public void insertFirstint x
if isFull err; return;
int i datasize ;
datasize datai ;
datai data;
data i;
datai x;
public void quicksort
handling empty cases. to only use not empty cases
if isEmpty
initialize the head index and tail
int first data;
int last first;
Find the last index of the list and move until end of list
while datalast
last datalast ;
call the helper function
quickSortHelperfirst last;
quicksort Helper
private void quickSortHelperint head, int tail
if the two nodes are not the same, then sort
if head tail && datagetNextIndexhead tail
Partition the list and get the pivot index
int pivotIndex partitionhead tail;
sort left and right
quickSortHelperhead pivotIndex;
quickSortHelperdatagetNextIndexpivotIndex tail;
Partition the list for quicksort
private int partitionint head, int tail
int pivotValue datahead;
int pre head;
int curr datagetNextIndexhead; Start from the next index after pivot
Iterate through the list and rearrange elements based on the pivot
while curr tail
If current element is smaller than pivot
if datacurr pivotValue
Move 'pre' to the next position
pre datagetNextIndexpre;
Swap the values of 'pre' and 'curr'
int temp datapre;
datapre datacurr;
datacurr temp;
else, move to the next index
curr datagetNextIndexcurr;
int temp datahead;
datahead datapre;
datapre temp;
return pre;
public int getNextIndexint index
return index ; Simply return the next index
public String toString
if isEmpty return "The list is empty.";
StringBuilder sb new StringBuilder;
int i data;
sbappenddatai; Append
while datai
i datai;
sbappendappenddatai;
return sbappendutoString;
Main method for testing
public static void mainString args
int testData
Empty list
Single element
Two elements sorted
Two elements unsorted
All negative
IntegerMAXVALUE, Integer.MINVALUE Edge values
All zeros
Duplicates
Random order
Decreasing order
IntegerMAXVALUE, Integer.MAXVALUE, Integer.MAXVALUE All max values
IntegerMINVALUE, Integer.MINVALUE, Integer.MINVALUE All min values
Mixed zeros
Multiple duplicates
Mixed positive and negative
;
for int a : testData
int n alength;
SecureRandom random new SecureRandom;
ListOnArraydUNGSethyPagna list new ListOnArraydUNGSethyPagnan 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;
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
