Quicksort 1 - Partition

  • + 0 comments

    The quicksort algorithm in Ruby

    def quickSort(arr)
        # Write your code here
        return arr if arr.length <= 1
    
        pivot = arr.delete_at(arr.length / 2)  # Select the pivot element
        left, right = arr.partition { |x| x < pivot }  # Partition the array
    
        # Recursively sort and combine
        return quickSort(left) + [pivot] + quickSort(right)
    
    end