You are on page 1of 4

Sorting The objective of the sorting algorithm is to rearrange the records so that their keys

are ordered according to some well-defined ordering rule.

Problem: Given an array of n real number A[1.. n]. Objective: Sort the elements of A in ascending order of their values.

Internal Sort
If the file to be sorted will fit into memory or equivalently if it will fit into an array, then the sorting method is called internal. In this method, any record can be accessed easily.

External Sort
• Sorting files from tape or disk. • In this method, an external sort algorithm must access records sequentially, or

at least in the block.

Memory Requirement
1. Sort in place and use no extra memory except perhaps for a small stack or

table. 2. Algorithm that use a linked-list representation and so use N extra words of memory for list pointers. 3. Algorithms that need enough extra memory space to hold another copy of the array to be sorted.

Most of the simple algorithm are stable. . comparisons of the form ai = aj are useless. . given two elements ai and aj we perform one of the tests. O(n2)-class includes bubble sort. Note that in a comparison sort. but most of the well-known sophisticated algorithms are not. Given all of the input elements are distinct (this is not a restriction since we are deriving a lower bound). . then this is the best we can do. ai = aj and ai ≥ aj to determine their relative order. ai ≥ aj and ai < aj are all equivalent. Therefore we assume that all comparisons have form ai ≥ aj. O(n2)-algorithms and O(n log n)-algorithms. There are two classes of sorting algorithms namely. merge sort and quick sort. selection sort and shell sort. an>. . . ai ≤ aj. we use only comparisons between elements to gain information about an input sequence <a1. insertion sort. a2. O(n log n)-class includes heap sort. ai < aj.Stability A sorting algorithm is called stable if it is preserves the relative order of equal keys in the file. We also note that the comparison ai ≤ aj . That is. O(n2) Sorting Algorithms O(n log n) Sorting Algorithms Now we show that comparison-based sorting algorithm has an Ω(n log n) worstcase lower bound on its running time operation in sorting. so no comparison of ai = aj are made.

so decision tree must have at least n! leaves. consider the decision tree for insertion sort operating on given elements a1. we can represent a comparison-based sorting algorithm with a decision tree T. the sorting algorithm may perform some calculation which we are not interested in and will eventually perform another comparison between two other elements of input sequence. There are are 3! = 6 possible permutations of the three input elements. so the decision tree must have at least 6 leaves. A Lower Bound for the Worst Case The length of the longest path from the root to any of its leaves represents the worstcase number of comparisons the sorting algorithm perform. Since there are . Proof Consider a decision tree of height h that sorts n elements. the worstcase number of comparisons corresponds to the height of its tree. Therefore. selection sort. A lower bound on the height of the tree is therefore a lower bound on the running time of any comparison sort algorithm. Based on the result of this comparison. Examples of comparison-based algorithms (in CLR) are insertion sort. Theorem The running time of any comparison-based algorithm for sorting an n-element sequence is Ω(n lg n) in the worst case. heapsort.The Decision Tree Model Each time a sorting algorithm compares two elements ai and aj . Consequently. there are n! possible permutations of the n input elements. As an example. there are two outcomes: "Yes" or "No". which again will have two outcomes. a2 and a3. quicksort. merge sort. In general. and treesort.

We have n! ≤ 2h Taking logarithms on both sides (lg(n!) ≤ h h ≥ lg(n!) Since the lg function is monotonically increasing. which is Ω(n lg n) . from Stirling's approximation we have n! > (n/e)n h ≥ (n/e)n where e = 2. .71828 . .n! permutation of n elements and the tree must have at least n! leaves.