QUICKSORT
Quicksort, or partition-exchange sort, is a sorting algorithm
developed by Tony Hoare that, on average, makes O(n log n)
comparisons to sort n items. In the worst case, it makes O(n2)
comparisons, though this behavior is rare. Quicksort is often
faster in practice than other O(n log n) algorithms.
Additionally, quicksort's sequential and localized memory
references work well with a cache. Quicksort is a comparison
sort and, in efficient implementations, is not a stable sort.
Quicksort can be implemented with an in-place partitioning
algorithm, so the entire sort can be done with only O(log n)
additional space used by the stack during the recursion. TONY HAORE
 Quick-sort is a randomized
sorting algorithm based on
the divide-and-conquer
paradigm:
 Divide: pick a random element
x (called pivot) and partition S
into
 L elements less than x
 E elements equal x
 G elements greater than x
 Recur: sort L and G
 Conquer: join L, E and G
x
x
L GE
x
 We partition an input
sequence as follows:
 We remove, in turn, each
element y from S and
 We insert y into L, E or G,
depending on the result of the
comparison with the pivot x
 Each insertion and removal is
at the beginning or at the end
of a sequence, and hence takes
O(1) time
 Thus, the partition step of
quick-sort takes O(n) time
Algorithm partition(S, p)
Input sequence S, position p of pivot
Output subsequences L, E, G of the
elements of S less than, equal to,
or greater than the pivot, resp.
L, E, G empty sequences
x S.remove(p)
while S.isEmpty()
y S.remove(S.first())
if y < x
L.insertLast(y)
else if y = x
E.insertLast(y)
else { y > x }
G.insertLast(y)
return L, E, G
To start with let us take an unsorted array which we
need to sort in ascending order
Now we need to choose a pivot element which can
be done in various ways. Here we choose the mid
element that is 7
9 71 4 10 6 3852
9 71 4 10 6 3852
Now elements smaller than the pivot are
brought to its left and the greater elements are
brought to is right. To check we start from the
1st element of array and likewise move forward
by placing the element as decribed
pivot
7 914 10625 83
7 914 10625 83
sorted In this array the pivot
element 7 is in sorted
position. Now the element at
the left of pivot which are
smaller than 7 is one part of
the array, the element at the
right of pivot which are
greater than 7 is another
part of the array. After this
we will perform the same
operation as we did in the
first pass recursively.
146253 9 10 8
pivot
2 3 5 6 41
pivot
1098
sortedsorted
3 5 6 41
pivot
534 6
sorted
34 6
pivot
3 4
sorted
7 914 10625 83
2 3 5 6 41
534 6
3 41 2 5 6 7
1098
98 10
This is the sorted
portioned array. Now
we need to merge
them all to get the
sorted array
3 4 5 6
3 4 5 621 98 107
3 4 5 621 98 107
This is the final
sorted array
Quicksort Algorithm..simply defined through animations..!!

Quicksort Algorithm..simply defined through animations..!!

  • 1.
  • 2.
    Quicksort, or partition-exchangesort, is a sorting algorithm developed by Tony Hoare that, on average, makes O(n log n) comparisons to sort n items. In the worst case, it makes O(n2) comparisons, though this behavior is rare. Quicksort is often faster in practice than other O(n log n) algorithms. Additionally, quicksort's sequential and localized memory references work well with a cache. Quicksort is a comparison sort and, in efficient implementations, is not a stable sort. Quicksort can be implemented with an in-place partitioning algorithm, so the entire sort can be done with only O(log n) additional space used by the stack during the recursion. TONY HAORE
  • 3.
     Quick-sort isa randomized sorting algorithm based on the divide-and-conquer paradigm:  Divide: pick a random element x (called pivot) and partition S into  L elements less than x  E elements equal x  G elements greater than x  Recur: sort L and G  Conquer: join L, E and G x x L GE x
  • 4.
     We partitionan input sequence as follows:  We remove, in turn, each element y from S and  We insert y into L, E or G, depending on the result of the comparison with the pivot x  Each insertion and removal is at the beginning or at the end of a sequence, and hence takes O(1) time  Thus, the partition step of quick-sort takes O(n) time Algorithm partition(S, p) Input sequence S, position p of pivot Output subsequences L, E, G of the elements of S less than, equal to, or greater than the pivot, resp. L, E, G empty sequences x S.remove(p) while S.isEmpty() y S.remove(S.first()) if y < x L.insertLast(y) else if y = x E.insertLast(y) else { y > x } G.insertLast(y) return L, E, G
  • 5.
    To start withlet us take an unsorted array which we need to sort in ascending order Now we need to choose a pivot element which can be done in various ways. Here we choose the mid element that is 7 9 71 4 10 6 3852
  • 6.
    9 71 410 6 3852 Now elements smaller than the pivot are brought to its left and the greater elements are brought to is right. To check we start from the 1st element of array and likewise move forward by placing the element as decribed pivot 7 914 10625 83
  • 7.
    7 914 1062583 sorted In this array the pivot element 7 is in sorted position. Now the element at the left of pivot which are smaller than 7 is one part of the array, the element at the right of pivot which are greater than 7 is another part of the array. After this we will perform the same operation as we did in the first pass recursively. 146253 9 10 8 pivot 2 3 5 6 41 pivot 1098 sortedsorted 3 5 6 41 pivot 534 6 sorted 34 6 pivot 3 4 sorted
  • 8.
    7 914 1062583 2 3 5 6 41 534 6 3 41 2 5 6 7 1098 98 10 This is the sorted portioned array. Now we need to merge them all to get the sorted array 3 4 5 6 3 4 5 621 98 107 3 4 5 621 98 107 This is the final sorted array