
Indian Institute of Information Technology Design and Manufacturing, Instructor Kancheepuram, Chennai 600 127, India N.Sadagopan An Autonomous Institute under MHRD, Govt of India Scribe: http://www.iiitdm.ac.in S.Dhanalakshmi COM 209T Design and Analysis of Algorithms -Lecture Notes Sorting Algorithms Objective: This module focuses on design and analysis of various sorting algorithms using paradigms such as Incremental Design and Divide and Conquer. Sorting a list of items is an arrangement of items in ascending (descending) order. We shall discuss six different sorting algorithms and we begin our discussion with Bubble sort. 1 Bubble Sort Bubble sort is a comparison based sorting algorithm wherein comparing adjacent elements is a primitive operation. In each pass, it compares the adjacent elements in the array and exchanges those that are not in order. Basically, each pass through the array places the next largest value in its proper place, hence the number of comparisons reduces by one at each pass. In other words, each item \bubbles"up to the location where it is supposed to be in the sorted sequence. This invariant is maintained by the algorithm in each pass and hence, bubble sort correctly outputs the sorted sequence. For an n-element array, the below pseudo code requires n − i comparisons for the ith iteration (Pass). Origin: Initially, Bubble sort was referred to as \Sorting by exchange" in [1, 2] and further, it is re- ferred to as \Exchange Sorting" in [3, 4]. The term \Bubble Sort " was first used by Iverson in 1962 [5]. Invariant: At the end of ith iteration, the last i elements contain i largest elements. i.e. a[n] contains the largest, a[n − 1] contains the second largest, and so on. At the end of nth iteration, the array is sorted as it contains n largest elements. Pseudo code: Bubble Sort(Array a[]) 1. begin 2. for i = 1 to n − 1 3. for j = 1 to n − i 4. if (a[j] > a[j + 1]) then 5. Swap (a[j]; a[j + 1]) 8. end Run-time Analysis We shall analyze the run-time by considering the best case input and the worst case input. Interestingly, for bubble sort, irrespective of the nature of input, the number of passes to be made is n − 1. Further, the number of comparisons during ith pass is n − i. By the end of every pass, at least one element is placed in its right position. In case of best case input, there is no swapping done and for every other input swapping may be required during each pass. Since the underlying model focuses on the number of comparisons (not on the number of swaps as it is less dominant operation), the number of comparisons is n − 1 + n − 2 + ::: + 2 + 1 = O(n2) for all inputs. Trace of Bubble Sort Algorithm Input: a[9] = f54; 26; 93; 17; 77; 31; 44; 55; 20g Pass 1: Pass 2: Pass 3: 2 Pass 4: Pass 5: Pass 6: 3 Pass 7: Pass 8: In this pass, no exchange for all 8 comparisons. Since, the input array size is 9, the number of passes is 8. The algorithm terminates and the input array contains the sorted sequence. 2 Insertion Sort This is a commonly used sorting algorithm with applications from arranging cards in a card game to ar- ranging examination answer sheets based on students' roll number. Insertion sort follows incremental design wherein we construct a sorted sequence of size two, followed by a sorted sequence of size three, and so on. In this sorting, during ith iteration, the first (i − 1) elements are sorted and ith card is inserted to the correct place by performing linear search on the first (i − 1) elements. This algorithm performs well on smaller inputs and on inputs that are already sorted. Origin: Insertion sort was mentioned by John Mauchly as early as 1946, in the first published discussion on computer sorting [6]. Pseudo code: Insertion Sort(a[]) Source: CLRS 1. begin 2. for j = 2 to n 3. key = a[j] 4. i = j + 1 5. while i > 0 and a[i] > key 6. a[i + 1] = a[i] 7. i = i − 1 8. a[i + 1] =key 9. end Run-time Analysis We shall analyze the run time of insertion sort by considering its worst case and best case behavior. In the below table, for each line of the pseudo code, the cost (the number of times the line is executed) incurred in best and worst case inputs is given. The table presents precise estimate using step count analysis. Alternatively, the cost can also be obtained using recurrence relation. Recurrence relation in worst case: T (n) = T (n − 1) + n − 1, T (2) = 1, solving this using substitution method/recurrence tree method yields T (n) = O(n2). Recurrence relation in best case: T (n) = T (n−1)+1, T (2) = 1, and the solution is T (n) = O(n). 4 Table 1: Step Count Analysis Pseudo code Worst Case Analysis Best Case Analysis I/P: Descending Order I/P: Ascending Order for j = 2 to n n n key = a[j] n − 1 n − 1 i = j + 1 n − 1 n − 1 Pn n(n+1) Pn while i > 0 and a[i] > key j=2(j) = 2 − 1 j=2(1) = n − 1 Pn n(n−1) a[i + 1] = a[i] j=2(j − 1) = 2 0 Pn n(n−1) i = i − 1 j=2(j − 1) = 2 0 a[i + 1] =key n − 1 n − 1 3n2 7n Total 2 + 2 − 4 5n − 4 = θ(n2) = θ(n) Trace of Insertion Sort Algorithm As mentioned, at the end of ith iteration, the first i elements are sorted. So, at the end of nth iteration and for this example, at the end of seventh iteration the given seven elements are sorted. Input: a[7] = {−1; 4; 7; 2; 3; 8; −5g Iteration 1: Iteration 2: Iteration 3: 5 Iteration 4: Iteration 5: Iteration 6: 6 3 Selection Sort It is a natural sorting algorithm [1] in which we find minimum, second minimum, third minimum and so on and arrange them in increasing order. Like bubble sort, irrespective of the input, during ith stage this algorithm incurs (n − i) comparisons. Further, the algorithm does linear search to find ith minimum. Pseudocode: Selection Sort(a[]) 1.begin 2. for j = 1 to n − 1 3. min = j 4. for i = j + 1 to n 5. if a[i] < a[min] 6. min = i 7. Swap(a[j]; a[min]) 8.end Run-time Analysis Note Line 5 of Selection Sort is executed for all inputs. During ith iteration, the statement is executed (n−i) times. Therefore, the total cost is n − 1 + n − 2 + ::: + 1, which is O(n2). Alternatively, the recurrence relation both in worst case and best case is T (n) = T (n − 1) + n − 1, T (2) = 1. Thus, T (n) = θ(n2). Trace of Selection Sort Algorithm Input: a[7] = {−1; 5; 3; 9; 12; 4; 8; 23; 15g 7 4 Merge Sort Merge Sort is based on the paradigm divide and conquer which has divide and conquer (combine) phases. As part of divide phase which is a top-down approach, the input array is split into half, recursively, until the array size reduces to one. That is, given a problem of size n, break it into two sub problems of size n=2, again break each of this sub problems into two sub problems of size n=4, and so on till the sub problem size reduces to n=2k = 1 for some integer k. (see Figure 1(a)). As part of conquer phase which is a bottom-up approach, we combine two sorted arrays of size one to get a sorted array of size two, and combine two sorted arrays of size two to get a sorted array of size 4, and in general, we combine two sorted arrays of size n=2 to get a sorted array of size n. Conquer phase happens when the recursion bottoms out and makes use of a black box which takes two sorted arrays and merges these two to produce one sorted array. In the example illustrated in Figure 1(b), at the last level , the black box combines the sorted array A = f1g and the sorted array B = f4g and results in a sorted array f1; 4g. Similarly, it combines the array {−1g and f8g and results in {−1; 8g. This process continues till the root which eventually contain the sorted output for the given input. Origin: Merge sort is one of the very first methods proposed for computer sorting (sorting numbers using computers) and it was suggested by John Von Neumann as early as 1945 [6]. A detailed discussion and analysis of merge sort was appeared in a report by Goldstine and Neumann as early as 1948 [7]. n Input: 1, 4, 2, 8, -1, 0 n/2 n/2 1, 4, 2, 8, -1, 0 1 4 8 -1 2-1, 8 0 n/4 n/4 n/4 1, 4 n/4 1, 4, 2 8, -1, 0 n/8 n/8 n/8 n/8 n/8 n/8 n/8 n/8 1, 2, 4 -1, 0, 8 1, 4 2 8, -1 0 -1, 0, 1, 2, 4, 8 1 4 8 -1 n/2k = 1 n/2k = 1 1 1 Split Phase Combine Phase (a) (b) Figure 1: (a) Split phase of merge sort (top-down) (b) Conquer phase of merge sort (bottom-up) 8 Pseudocode: Merge-Sort(A; p; r) Source: CLRS begin 1.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages21 Page
-
File Size-