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: [2,1]
The original list: 2->1
The sorted list: 2->1
The original array: [2147483647,-2147483648]
The original list: 2147483647->-2147483648
The sorted list: 2147483647->-2147483648
The original array: [0,0,0]
The original list: 0->0->0
The sorted list: 0->0->0
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 ListOnArray_23100448d_UNGSethyPagna {
private int[] data;
int size; // Size of array
public ListOnArray_23100448d_UNGSethyPagna(int n){
size =(n +1)<<1;
data = new int[size];
data[0]=0;
for (int i =2; i < size -2; i +=2)
data[i]= i +1;
data[size -1]=1;
data[size -2]=0;
}
public boolean isEmpty(){
return data[0]==0;
}
public boolean isFull(){
return data[size -1]==0;
}
public void err(){
System.out.println("Oops...");
}
public void insertFirst(int x){
if (isFull()){ err(); return; }
int i = data[size -1];
data[size -1]= data[i +1];
data[i +1]= data[0];
data[0]= i;
data[i]= x;
}
public void quicksort(){
// handling empty cases. to only use not empty cases
if (!isEmpty()){
// initialize the head index and tail
int first = data[0];
int last = first;
// Find the last index of the list and move until end of list
while (data[last +1]!=0){
last = data[last +1];
}
//call the helper function
quickSortHelper(first, last);
}
}
// quicksort Helper
private void quickSortHelper(int head, int tail){
//if the two nodes are not the same, then sort
if (head != tail && data[getNextIndex(head)]!= tail){
// Partition the list and get the pivot index
int pivotIndex = partition(head, tail);
//sort left and right
quickSortHelper(head, pivotIndex);
quickSortHelper(data[getNextIndex(pivotIndex)], tail);
}
}
// Partition the list for quicksort
private int partition(int head, int tail){
int pivotValue = data[head];
int pre = head;
//
int curr = data[getNextIndex(head)]; // 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 (data[curr]< pivotValue){
// Move 'pre' to the next position
pre = data[getNextIndex(pre)];
// Swap the values of 'pre' and 'curr'
int temp = data[pre];
data[pre]= data[curr];
data[curr]= temp;
}
// else, move to the next index
curr = data[getNextIndex(curr)];
}
int temp = data[head];
data[head]= data[pre];
data[pre]= temp;
return pre;
}
public int getNextIndex(int index){
return index +1; // Simply return the next index
}
public String toString(){
if (isEmpty()) return "The list is empty.";
StringBuilder sb = new StringBuilder();
int i = data[0];
sb.append(data[i++]); // Append
while (data[i]!=0){
i = data[i];
sb.append("->").append(data[i++]);
}
return sb.append('\u2193').toString();
}
// Main method for testing
public static void main(String[] args){
int[][] testData ={
{},// Empty list
{5},// Single element
{1,2},// Two elements sorted
{2,1},// Two elements unsorted
{-1,-2,-3,-4},// All negative
{Integer.MAX_VALUE, Integer.MIN_VALUE},// Edge values
{0,0,0},// All zeros
{1,2,2,3,3,3},// Duplicates
{10,50,30,20,40},// Random order
{100,90,80,70,60},// Decreasing order
{Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE},// All max values
{Integer.MIN_VALUE, Integer.MIN_VALUE, Integer.MIN_VALUE},// All min values
{1,2,3,4,5,0,0,0},// Mixed zeros
{7,3,5,3,5,3,5},// Multiple duplicates
{100,50,75,25,0,-25,-50,-75},// Mixed positive and negative
{1,1,1,1,1,1,1}
};
for (int[] a : testData){
int n = a.length;
SecureRandom random = new SecureRandom();
ListOnArray_23100448d_UNGSethyPagna list = new ListOnArray_23100448d_UNGSethyPagna(n + random.nextInt(1+(n <<2))); // you don't need to understand this line.
System.out.println("The original array: "+ Arrays.toString(a));
for (int i = n -1; i >=0; i--) list.insertFirst(a[i]);
System.out.println("The original list: "+ list);
list.quicksort();
System.out.println("The sorted list: "+ list);
}
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!