Quicksort In-Place

Sort by

recency

|

152 Discussions

|

  • + 0 comments

    def partition(arr.low.high): i=(low-7) pivot=arr[high] for j in range(low.high): if arr[j]

    def quick_Sort(arr.low.high): if len(arr)==1: return arr if low

  • + 0 comments

    anyone can explain why this code is wrong import java.io.; import java.util.;

    public class Solution {

    public void swap(int[] a, int i, int j) {
        int tmp = a[i];
        a[i] = a[j];
        a[j] = tmp;
    }
    
    public void print(int[] a) {
        for (int i : a) {
            System.out.print(i + " ");
        }
        System.out.print('\n');
    }
    
      public int partition(int[] arr, int left, int right) {
        int pivot = right;
        while (left < right) {
            while (left < right && arr[left] <= arr[pivot]) {
                left++;
            }
            while (left < right && arr[right] >= arr[pivot]) {
                right--;
            }
            swap(arr, left, right);
        }
        swap(arr, pivot, left);
        print(arr);
        return left;
    }
    
    public void quickSort(int[] a, int left, int right) {
        if (left >= right) {
            return;
        }
        int pivot = partition(a, left, right);
        quickSort(a, left, pivot - 1);
        quickSort(a, pivot + 1, right);
    }
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        sc.close();
        new Solution().quickSort(a, 0, a.length - 1);
    }
    

    }

  • + 0 comments

    I received no points for my submission. Instead, I got this message, "We couldn't process your submission. Please submit your code again."

  • + 0 comments
    import java.util.Scanner;
    
    public class QuickSortInPlace {
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int n = scanner.nextInt();
            int[] arr = new int[n];
            for (int i = 0; i < n; i++) {
                arr[i] = scanner.nextInt();
            }
    
            quickSort(arr, 0, n - 1);
    
            scanner.close();
        }
    
        public static void quickSort(int[] arr, int low, int high) {
            if (low < high) {
                int pivotIndex = partition(arr, low, high);
                printArray(arr);
    
                quickSort(arr, low, pivotIndex - 1);
                quickSort(arr, pivotIndex + 1, high);
            }
        }
    
        public static int partition(int[] arr, int low, int high) {
            int pivot = arr[high];
            int i = low - 1;
    
            for (int j = low; j < high; j++) {
                if (arr[j] < pivot) {
                    i++;
                    swap(arr, i, j);
                }
            }
    
            swap(arr, i + 1, high);
            return i + 1;
        }
    
        public static void swap(int[] arr, int i, int j) {
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    
        public static void printArray(int[] arr) {
            for (int num : arr) {
                System.out.print(num + " ");
            }
            System.out.println();
        }
    }
    
  • + 0 comments

    why i didn't got points for this problem