Simple Quick Sort


Collapse Content

In the previous challenge, you wrote a 'partition' method to split an array into smaller and greater elements. This means you 'sorted' half the array with respect to the other half. Can you repeatedly use 'partition' to sort an entire array?

Guideline: In Insertion sort, you simply went through each element in order and inserted it into a sorted sub-array. In this challenge, you cannot focus on one element at a time, but instead must deal with whole sub-arrays. Each time you call partition, you are sorting two parts of the array with respect to each other. Notice if you called partition on two elements, that sub-array would be fully sorted.
Can you repeatedly call partition on each sub-array so that the entire array ends up sorted?

Click for More

Challenge

Can you code a Quicksort algorithm?

Please sign in or sign up to submit answers.

Alternatively, you can try out Learneroo before signing up.

Comments

  • static void doStuff(int[] ar){
        int pivot = ar[0];
        int pivotLocation = 0;
        for(int i = 1; i < ar.length; i++){
            int curr = ar[i];
            if(curr < pivot){
    
    cont...
  • static void doStuff(int[] ar){
    partition(ar, 0, ar.length - 1);
    }

    static void partition(int[] ar, int min, int max){
        int pivot = ar[min];
    
    cont...
Contact Us
Sign in or email us at [email protected]