CSCE 210 Dr. Amr Goneid Exercises (7) Sorting Algorithms ______1. Consider the algorithm for the sorting problem that sorts an array by counting, for each of its elements, the number of smaller elements and then uses this information to put the element in its appropriate position in the sorted array:

ComparisonCountingSort(A[0..n − 1], S[0..n-1]) //Sorts an array by comparison counting //Input: Array A[0..n − 1] of orderable values //Output: Array S[0..n − 1] of A’s elements sorted in nondecreasing order for i ←0 to n − 1 do Count[i]←0 for i ←0 to n − 2 do for j ←i + 1 to n − 1 do if (A[i] < A[j ]) Count[j ]←Count[j ]+ 1 else Count[i]←Count[i]+ 1 for i ←0 to n − 1 do S[Count[i]]←A[i]

• Trace the algorithm on the array A = {4 , 3, 8, 5} • Find the exact number of array element comparisons done by this algorithm in the best case and in the worst case. • What is the complexity (Big-O) of the algorithm? n {A useful summation: ∑ i = n(n+1)/2 } i = 1 Answer: A tracing of the algorithm will produce:

0 1 2 3 A 4 3 8 5 Count 1 0 3 2 S 3 4 5 8

The array element comparisons will be inside the nested (i) (j) loops. The number of such comparisons will always be (n-1) + (n-2) +...... +1 = n(n-1)/2 so that the complexity is O(n2) ______2. Sort the list E, X, A, M, P, L, E in alphabetical order by and by .

Solution: Sorting by Selection Sort will proceed as follows:

E X A M P L E A X E M P L E A E X M P L E A E E M P L X A E E L P M X A E E L M P X A E E L M P X

Do a similar tracing for sorting by Bubble Sort ______3. The following code segment performs on an array a[ ] of size (n) with the elements located in a[0], a[2], …., a[n-1]: for (i =1; i < n; i++) { v = a[i]; j = i; while(j > 0 && a[j-1] > v) { a[j] = a[j-1]; j--; } a[j] = v; }

• Find the exact number of array element comparisons done by this algorithm in the best case and in the worst case. • Find the exact number of array element moves done by this algorithm in the best case and in the worst case. • What is the complexity (Big-O) of the algorithm? n {A useful summation: ∑ i = n(n+1)/2 } i = 1 Solution: The analysis of Insert Sort is given in details in the slides of Part 8a ______4. The array of integers: (4 , 4 , 8 , 5 , 3 , 6 , 1) is input to the Mergesort algorithm. • Trace the algorithm to sort the array. • What is the complexity of the algorithm when sorting an array of N equal elements? • Is the algorithm stable? Solution: • The array sorting will proceed as follows:

4 4 8 5 3 6 1 4 4 8 5 3 6 1 4 4 8 5 3 6 1 4 4 8 5 3 6 1 4 4 5 8 3 6 1 4 4 5 8 1 3 6 1 3 4 4 5 6 8

• When sorting N equal elements, the complexity is still O(N log N) • Mergesort is a stable algorithm ______

5. Show the results of running the on the input : (14 , 11 ,15 , 19 , 17 , 16 , 17 , 18 , 12 ) using first element as pivot. What is the complexity of the algorithms if the input is already sorted? ______6. Estimate how many times faster quicksort will sort an array of one million random numbers than insertion sort.

Answer: For random input, Insertion sort costs O(n2) and Quicksort costs O(n log n). Hence, Quicksort is faster by a factor of n/ log n = 50,000 ______7. For quicksort with the median-of-three pivot selection, are strictly increasing arrays the worst-case input, the best-case input, or neither? Answer the same question for strictly decreasing arrays. ______8. The array of integers: (4 , 4 , 8 , 5 , 3 , 6 , 1) is input to the Quicksort algorithm that uses the Median-of-Three as pivot. • Display the array step by step as it is sorted by the algorithm. • What is the complexity of Quicksort when sorting an array of N equal elements? ______