N
N
netrox2017-03-04 22:57:48
Algorithms
netrox, 2017-03-04 22:57:48

How does the quicksort algorithm work?

Can you describe in detail how this algorithm works?

int partition (int[] array, int start, int end) 
   {
       int marker = start;
       for ( int i = start; i <= end; i++ ) 
       {
           if ( array[i] < array[end] ) 
           {
               int temp = array[marker]; // swap
               array[marker] = array[i];
               array[i] = temp;
               marker += 1;
           }
       }
       return marker - 1;
   }

   void quicksort (int[] array, int start, int end)
   {
       if ( start >= end ) 
       {
           return;
       }
       int pivot = partition (array, start, end);
       quicksort (array, start, pivot-1);
       quicksort (array, pivot+1, end);
   }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Zagaevsky, 2017-03-04
@zagayevskiy

Here's even a video for you https://youtu.be/ywWBy6J5gz8
And so read on the Internet.

V
Vladimir Olohtonov, 2017-03-05
@sgjurano

A random element is selected on the sorted part of the array, all elements less than it are transferred to the left side, the rest to the right. For each of the parts, a similar algorithm is called. The devil is in the details :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question