Hi I have written this code but it will print these stack traces in the console please help me thanks! (Aslo "p" and "q" are the first and last index of our array ,respectively)
public class JavaQuickSort {
public static void QuickSort(int A[], int p, int q) {
int i, last = 0;
Random rand = new Random();
if (q < 1) {
return;
}
**swap(A, p, rand.nextInt() % (q+1));**
for (i = p + 1; i <= q; i++) {
if (A[i] < A[p]) {
swap(A, ++last, i);
}
}
swap(A, p, last);
QuickSort(A, p, last - 1);
QuickSort(A, last + 1, q);
}
private static void swap(int[] A, int i, int j) {
int temp;
temp = A[i];
**A[i] = A[j];**
A[j] = temp;
}
public static void main(String[] args){
int[] A = {2,5,7,3,9,0,1,6,8};
**QuickSort(A, 0,8 );**
System.out.println(Arrays.toString(A));
}
}
the Stack traces :
run:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -3
at JavaQuickSort.swap(JavaQuickSort.java:38)
at JavaQuickSort.QuickSort(JavaQuickSort.java:22)
at JavaQuickSort.main(JavaQuickSort.java:45)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)
I also bold those statements that cause these stack traces. like ==> ** ...**
EDITED:
public class JavaQuickSort {
public static void QuickSort(int arr[],int lo,int hi) {
int n = arr.length;
if(n<=1)
return;
**int r = partition(arr);**
**QuickSort(arr,lo , r-1);**
QuickSort(arr, r+1, hi);
}
private static void swap(int[] A, int i, int j) {
int temp;
temp = A[i];
**A[i] = A[j];**
A[j] = temp;
}
public static int partition(int arr[]){
int i, last = 0;
Random rand = new Random();
int n = arr.length;
if (n <= 1)
return arr[0];
**swap(arr, 0, rand.nextInt(n));**
for (i =1; i <n; i++) {
if (arr[i] < arr[0]) {
swap(arr, ++last, i);
}
}
swap(arr, 0, last);
return last;
}
public static void main(String[] args){
int[] A = {2,5,7,3,9,0,1,6,8};
**QuickSort(A, 0,8 );**
System.out.println(Arrays.toString(A));
}
}
I have edited my post for being more understandable also it will print these stack traces and I bold the lines that cause these stack traces!!!
the stack traces :
run:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at JavaQuickSort.swap(JavaQuickSort.java:27)
at JavaQuickSort.partition(JavaQuickSort.java:39)
at JavaQuickSort.QuickSort(JavaQuickSort.java:19)
at JavaQuickSort.QuickSort(JavaQuickSort.java:20)
at JavaQuickSort.QuickSort(JavaQuickSort.java:20)
at JavaQuickSort.QuickSort(JavaQuickSort.java:20)
at JavaQuickSort.QuickSort(JavaQuickSort.java:20)
at JavaQuickSort.main(JavaQuickSort.java:52)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)
please help me thanks
EDITED :
I have written this code with paying attention to the forst answer of this post but it wont sort my array!!!
public class JavaQuickSort {
public static void QuickSort(int arr[], int lo, int hi) {
if (hi > lo) {
Random rand = new Random();
int pivotIndex = lo + rand.nextInt(hi-lo+1);
int pivotnewIndex = partition(arr, lo, hi,pivotIndex);
QuickSort(arr, lo, pivotnewIndex - 1);
QuickSort(arr, pivotnewIndex + 1, hi);
}
}
private static void swap(int[] A, int i, int j) {
int temp;
temp = A[i];
A[i] = A[j];
A[j] = temp;
}
public static int partition(int arr[],int lo,int hi,int pivotIndex)
{
int pivotValue = arr[pivotIndex];
swap(arr, hi, pivotIndex);
int storeIndex = lo;
for(int i = lo;i<hi;i++){
if (arr[i]<=pivotValue)
swap(arr, storeIndex, i);
storeIndex = storeIndex ++;
}
swap(arr, storeIndex, hi);
return storeIndex;
}
public static void main(String[] args) {
int[] A = {2, 5, 7, 3, 9, 0, 1, 6, 8};
QuickSort(A, 0, 8);
System.out.println(Arrays.toString(A));
}
}
the output:
run:
[2, 9, 3, 8, 0, 6, 7, 1, 5]
BUILD SUCCESSFUL (total time: 2 seconds)
I need your help really I confused !!!