jeudi 2 décembre 2021

trying to make my quick-sort program work with a while loop, ran into a hitch

public class qsort {
    public static void main(String[] args) {
        char[] arr = {'Q', 'U', 'I', 'C', 'K', 'S', 'O', 'R', 'T', 'E', 'X', 'A', 'M', 'P', 'L', 'E'};
        System.out.println(Arrays.toString(arr));
        shuffle(arr);
        System.out.println(Arrays.toString(arr));
        System.out.println("Pivot="+arr[0]);
        int i;
        while(arr[i]<arr[j]){
            for(i=1;i<arr.length;i++){
                if(arr[i]>arr[0]){
                    System.out.println("Left="+arr[i]);
                    break;
                }
            }
            for(int j=arr.length-1;j>i;j--){
                if(arr[j]<=arr[0]){
                    System.out.println("Right="+arr[j]);
                    exch(arr,i,j);
                    break;
                }
            }
        }
        System.out.println(Arrays.toString(arr));
        }
    public static void exch(char[] arr,int i, int j){
        char temp=arr[i];
        arr[i]=arr[j];
        arr[j]=temp;
    }
    public static void shuffle(char[] arr) {
        Random rand = new Random();
        for (int i = 0; i < arr.length; i++) {
            int r = rand.nextInt(arr.length);
            char temp = arr[i];
            arr[i] = arr[r];
            arr[r] = temp;
        }
    }
}



Aucun commentaire:

Enregistrer un commentaire