Quicksort In-Place

Sort by

recency

|

151 Discussions

|

  • + 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

  • + 0 comments

    Does anyone know why this code is gives me an "RangeError: Maximum call stack size exceeded"?

    I have tried the code on many other platforms and it works, but here I get that error...

    function processData(arr) {
      //Enter your code here
      const pivot = arr.length - 1;
      let i = -1;
      
      if(arr.length <= 1) return arr
    
      for (let j = 0; j < arr.length; j++) {
        
        if (arr[j] < arr[pivot]) {
          //we increment i
          i++;
    
          //we swamp elements
          const toSwamp = arr[i];
          arr[i] = arr[j];
          arr[j] = toSwamp;
         
        }
    
        if (j === pivot) {
          const toSwamp = arr[i + 1];
          arr[i + 1] = arr[pivot];
          arr[pivot] = toSwamp;
        
        }
      }
      
      console.log(arr)
      return processData(arr.slice(0, i+1)).concat(processData(arr.slice(i+1, pivot+1)))
    }