COMP2247 - Algorithms and Data Structures

Syllabus Software Installation Assignments Tests
Lectures and Labs
**Back to Full Menu**

Sorting and Searching Algorithms SelectionSort Program BinarySearch Program QuickSort Program HardwareStore Program HardwareStore Comparable Objects Program



QuickSort Program


Description

This program uses the quick sort algorithm to sort an array of random integers.

Associated Concepts:

Quick Sort

Algorithm:

main method:

Create an array with random integers.

Print the unsorted array elements.

Call the method quickSort() to sort the array, and pass the array, the left most index, the right most index as the arguments.

Print the sorted array elements.

quickSort method definition:

Call partition() method to get the index j which creates a partition at j, and pass the array, the left most index, and the right most index as the arguments.

Call quickSort() on the left partition and on the right partition recursively.

The process stops once there is only one element left in the sub-array.

At this point, the array is sorted.

partition method definition:

The Partition() method creates a partition at index j:

elements on the left <= x (pivot)

elements on the right >= x (pivot)

Source Code



Sample Run



Video