Quicksort In-Place

Sort by

recency

|

150 Discussions

|

  • + 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)))
    }
    
  • + 0 comments

    def partition(arr, first, last):

    i = (first - 1)         # last_index of smaller elements
    pivot = arr[last]     
    
    for j in range(first, last):
    
        # If current element is smaller than or equal to pivot
        if arr[j] <= pivot:
    
            # increment last_index of smaller elements
            i = i + 1
            # move the element to the last_index of smaller elements
            arr[i], arr[j] = arr[j], arr[i]
    
    arr[i + 1], arr[last] = arr[last], arr[i+1]
    print (' '.join(str(x) for x in arr))
    
    return (i + 1)
    

    def quickSort(arr, first, end):

    if len(arr) == 1:
        return arr
    if first < end:        
        pivot_index = partition(arr, first, end)        
        quickSort(arr, first, pivot_index-1)
        quickSort(arr, pivot_index+1, end)
    

    n = int(input().rstrip())

    arr = list(map(int,input().rstrip().split()))

    quickSort(arr, 0, n-1)