0% found this document useful (0 votes)
55 views3 pages

CSCE 210 Dr. Amr Goneid Exercises (7) Sorting Algorithms

The document discusses various sorting algorithms including comparison counting sort, selection sort, bubble sort, insertion sort, merge sort, quicksort, and median-of-three quicksort. It provides examples of applying each algorithm, analyzing their time complexities in best and worst cases, and determining whether they are stable algorithms or not. Questions are asked about analyzing steps, comparisons, complexity, and determining best/worst cases for the sorting algorithms.

Uploaded by

bla bla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views3 pages

CSCE 210 Dr. Amr Goneid Exercises (7) Sorting Algorithms

The document discusses various sorting algorithms including comparison counting sort, selection sort, bubble sort, insertion sort, merge sort, quicksort, and median-of-three quicksort. It provides examples of applying each algorithm, analyzing their time complexities in best and worst cases, and determining whether they are stable algorithms or not. Questions are asked about analyzing steps, comparisons, complexity, and determining best/worst cases for the sorting algorithms.

Uploaded by

bla bla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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 =1
i = n(n+1)/2 }

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 selection sort and by Bubble
sort.

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 Insertion Sort 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 =1
i = n(n+1)/2 }

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 Quicksort 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?
________________________________________________________________________

You might also like