Quick Sort (Also known as “partition-exchange sort”)

Quick Sort

(Also known as “partition-exchange sort”)

Definition:

Quick sort is a well –known sorting algorithm, based on divide & conquer approach. The

steps are:

1. Pick an element called pivot from the list

2. Reorder the list so that all elements which are less than the pivot come before the pivot and all elements greater than pivot come after it. After this partitioning, the pivot is in its final position. This is called the partition operation

3. Recursively sort the sub-list of lesser elements and sub-list of greater elements.

Features:

• Developed by C.A.R. Hoare

• Efficient algorithm

• NOT stable sort

• Significantly faster in practice, than other algorithms

Algorithm

ALGORITHM Quicksort (A[ l …r ])

//sorts by quick sort

//i/p: A sub-array A[l..r] of A[0..n-1],defined by its left and right indices l and r

//o/p: The sub-array A[l..r], sorted in ascending order

image

ALGORITHM Partition (A[l ..r])

//Partitions a sub-array by using its first element as a pivot

//i/p: A sub-array A[l..r] of A[0..n-1], defined by its left and right indices l and r (l < r)

//o/p: A partition of A[l..r], with the split position returned as this function’s value

image

Analysis:

Input size: Array size, n

Basic operation: key comparison

• Best, worst, average case exists:

Best case: when partition happens in the middle of the array each time.

Worst case: When input is already sorted. During key comparison, one half is empty, while remaining n-1 elements are on the other partition.

• Let C(n) denotes the number of times basic operation is executed in worst case: Then C(n) = C(n-1) + (n+1) for n > 1 (2 sub-problems of size 0 and n-1 respectively) C(1) = 1

Best case:

C(n) = 2C(n/2) + Θ(n)                 (2 sub-problems of size n/2 each)

• Solving the recurrence equation using backward substitution/ master theorem, we have:

C(n) = C(n-1) + (n+1) for n > 1; C(1) = 1

C(n) = Θ (n2)

C(n) = 2C(n/2) + Θ(n).

= Θ (n1log n)

= Θ (n log n)

NOTE:

The quick sort efficiency in average case is Θ( n log n) on random input.

Binary Search

Description:

Binary tree is a dichotomic divide and conquer search algorithm. Ti inspects the middle element of the sorted list. If equal to the sought value, then the position has been found. Otherwise, if the key is less than the middle element, do a binary search on the first half, else on the second half.

Algorithm:

Algorithm can be implemented as recursive or non-recursive algorithm.

ALGORITHM BinSrch ( A[0 … n-1], key)

//implements non-recursive binary search

//i/p: Array A in ascending order, key k

//o/p: Returns position of the key matched else –1

image

Analysis:

Input size: Array size, n

Basic operation: key comparison

• Depend on

Best – key matched with mid element

Worst – key not found or key sometimes in the list

• Let C(n) denotes the number of times basic operation is executed. Then Cworst(n) = Worst case efficiency. Since after each comparison the algorithm divides the problem into half the size, we have

image

• Solving the recurrence equation using master theorem, to give the number of times the search key is compared with an element in the array, we have:

C(n) = C(n/2) + 1

a = 1

image

Applications of binary search:

• Number guessing game

• Word lists/search dictionary etc

Advantages:

• Efficient on very big list

• Can be implemented iteratively/recursively

Limitations:

• Interacts poorly with the memory hierarchy

• Requires given list to be sorted

• Due to random access of list element, needs arrays instead of linked list.

Comments

Popular posts from this blog

Data Structure Visualization:Introduction and Value of Data Structure Rendering

Binary Space Partitioning Trees:BSP Tree as a Hierarchy of Regions.

Drawing Trees:HV-Layout