Quick Sort Algorithm: A Fast, Recursive Approach to Sorting Arrays, Quizzes of Computer Science

An introduction to the quick sort algorithm, a fast and recursive sorting method. Quick sort differs from merge sort in the way it breaks down an array into smaller parts and sorts them. The process of partitioning an array, selecting a pivot, and implementing the quick sort algorithm. It also discusses the importance of selecting a good pivot value and the potential risks of poor performance.

Typology: Quizzes

Pre 2010

Uploaded on 08/30/2009

koofers-user-5bd-1
koofers-user-5bd-1 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Name:
Lesson: Quick Sort
1
Lesson: Quick Sort – A
Usually
Fast
Sorting Algorithm
In this lesson we introduce quick sort. Quick sort, as the name suggests, is
another fast sorting algorithm. Quick sort is also recursive, which will give us the
opportunity to once again use the recursive analysis techniques introduced in the
previous lesson. Quick sort has differing best and worst case execution times,
similar in this regard to insertion sort. Finally, quick sort presents an unusual
contrast to the merge sort algorithm.
The quick sort algorithm is in one sense similar to, and in another sense very
different from the merge sort algorithm. Both work by breaking an array into two
parts, recursively sorting the two pieces, and putting them back together to form
the final result.
Where they differ is in the
way that the array is
broken into pieces, and
hence the way that they
are put back together.
Merge sort devotes very
little effort to the breaking
apart phase, simply
selecting the first half of
the array for the first list,
and the second half for the
second. This means that relatively little can be said about the two sub-arrays,
and significant work must be performed to merge them back together.
Quick sort, on the other
hand, spends more time on
the task of breaking apart.
Quick sort selects one
element, which is called the
pivot
. It then divides the
array into two sections with
the following property:
every element in the first
half is smaller than or equal
to the pivot value, while
every element in the
second half is larger than or equal to the pivot. Notice that this property does not
guarantee sorting. Each of the smaller arrays must then be sorted (by recursively
calling quick sort). But once sorted, this property makes putting the two sections
pf3
pf4

Partial preview of the text

Download Quick Sort Algorithm: A Fast, Recursive Approach to Sorting Arrays and more Quizzes Computer Science in PDF only on Docsity!

Lesson: Quick Sort – A Usually Fast

Sorting Algorithm

In this lesson we introduce quick sort. Quick sort, as the name suggests, is another fast sorting algorithm. Quick sort is also recursive, which will give us the opportunity to once again use the recursive analysis techniques introduced in the previous lesson. Quick sort has differing best and worst case execution times, similar in this regard to insertion sort. Finally, quick sort presents an unusual contrast to the merge sort algorithm. The quick sort algorithm is in one sense similar to, and in another sense very different from the merge sort algorithm. Both work by breaking an array into two parts, recursively sorting the two pieces, and putting them back together to form the final result. Where they differ is in the way that the array is broken into pieces, and hence the way that they are put back together. Merge sort devotes very little effort to the breaking apart phase, simply selecting the first half of the array for the first list, and the second half for the second. This means that relatively little can be said about the two sub-arrays, and significant work must be performed to merge them back together. Quick sort, on the other hand, spends more time on the task of breaking apart. Quick sort selects one element, which is called the pivot. It then divides the array into two sections with the following property: every element in the first half is smaller than or equal to the pivot value, while every element in the second half is larger than or equal to the pivot. Notice that this property does not guarantee sorting. Each of the smaller arrays must then be sorted (by recursively calling quick sort). But once sorted, this property makes putting the two sections

back together much easier. No merge is necessary, since we know that elements in the first part of the array must all occur before the elements in the second part of the array. Because the two sections do not have to be moved after the recursive call the sorting can be performed in-place. That is, there is no need for any additional array as there is when using the merge sort algorithm. All that is necessary is to modify the description from “sorting an array” to “sorting the portion of an array that appears in locations i to j”, as follows: public void quickSort (double [ ] storage) { quickSort (storage, 0, storage.length-1); } private void quickSort (double [ ] storage, int low, int high) { if (low >= high) return; // base case int pivot = (low + high)/2; // one of many techniques pivot = partition(storage, low, high, pivot); quickSort (storage, low, pivot-1); quickSort (storage, pivot+1, high); } There is, however, a danger in this process. In merge sort it was easy to guarantee that the two halves were roughly equal size, yielding a fast O(n log n) process. In quick sort this is much more difficult to guarantee. If we are unlucky then in the worst case one partition contains no values, and the other is just one element smaller. This leads to poor O(n^2 ) performance.

Partitioning

The process of dividing a portion of an array into two sections is termed partitioning. The limits of the partition will be described by a pair of values: low and high. The first represents the lowest index in the section of interest, and the second the highest index. In addition there is a third element that is selected, termed the pivot. The first step is to swap the element at the pivot location and the first position. The variable i is set to the next position, and the variable j to high. The heart of the partition algorithm is a while loop. The invariant that is going to be preserved is that all the elements with index values smaller than i are

public class QuickSort { private void swap (double [] storage, int i, int j) { double temp = storage[i]; storage[i] = storage[j]; storage[j] = temp; } private int partition (double [ ] storage, int low, int high, int pivot) { } public void quickSort (double [ ] storage) { quickSort (storage, 0, storage.length-1); } private void quickSort (double [ ] storage, int low, int high) { if (low >= high) return; // base case int pivot = (low + high)/2; // one of many techniques pivot = partition(storage, low, high, pivot); quickSort (storage, low, pivot-1); quickSort (storage, pivot+1, high); } }