Tutorial Week 07
Exercise 1
- Explain what stability means in the context of sorting
- Suppose you have an implementation of a sorting algorithm that sorts strings and is case
insensitive (for example 'a' and 'A' are considered to be equal).
Explain what is wrong with the following argument:
I ran the following input through the program
AAAAA zzzzz abcde aaaaa
and the output of the program wasAAAAA aaaaa abcde zzzzz
This means my sorting program is stable
Exercise 2
Naive Bubble Sort
1 void bubbleSort(int items[], int n) { 2 int i, j; 3 4 for(i = n - 1; i > 0 ; i--) { 5 for(j = 1; j <= i; j++) { 6 if (items[j] < items[j - 1]) { 7 swap(j,j-1,items); 8 } 9 } 10 } 11}
Random order: | 3,2,4,8,1 |
Sorted order: | 1,2,3,4,5 |
Reverse order: | 5,4,3,2,1 |
- Show how each of these arrays above change as they are sorted by the program above.
- How many swaps are performed on the random, sorted, reverse ordered data sets shown above
- How many comparisons are peformed on the random, sorted and reverse ordered data sets shown above. By comparison we mean comparing two data elements from the array - we are not including the loop counter comparisons.
- With each line of code associate a cost and a formula expressing the number of times the C statement on that line will be executed when sorting n items in the worst case.
- What is the asymptotic worst case time complexity of the algorithm implemented by this program.
- What is the time complexity of the algorithm in the best case?
- Modify the program to implement bubble sort with early exit. What is the asymptotic worst case time complexity now? What is the time complexity now in the best case?
Exercise 3
Sorting Linked Lists
Implement selection sort, given the following definition of a linked list
typedef struct node * link; struct node{ Item item; link next; };
Exercise 4
Quick Sort
The following is the implementation of quicksort and the partition function as discussed in the lectures.
void quickSort (int a[], int l, int r){ int i; if (r <= l) { return; } i = partition (a, l, r); quickSort (a, l, i-1); quickSort (a, i+1, r); } int partition (int a[], int l, int r) { int i = l-1; int j = r; int pivot = a[r]; //rightmost is pivot for(;;) { while ( a[++i] < pivot) ; while ( pivot < a[--j] && j != l); if (i >= j) { break; } swap(i,j,a); } //put pivot into place swap(i,r,a); return i; //Index of the pivot }
Trace the execution of the partition function on sorting the following input sequence of numbers: 4 7 1 1 4 6 7 2 5 6.
Trace the execution of the partition function on the following sequence of numbers:
1 2 3 4 5 6 7 8 9 10
Exercise 5
Quicksort with median of three partitioning
One improvement to the Quicksort algorithm that we discussed was the use of Median-of-Three partitioning. In
this version of Quicksort, three items in the array are sampled and the median of the three values is used to partition the array. Given the code below,
trace the call of medianOfThreePivot and then partition on the sequence 1 2 3 4 5 6 7 8 9 10
// Quick sort with Median of Three Partitioning void quicksortMT (Item a[], int l, int r) { int i; if (r <= l) return; if(r-l > 1){ medianOfThreePivot(a,l,r); i = partition (a, l+1, r-1); } else { i = partition (a, l, r); } quicksortMT (a, l, i-1); quicksortMT (a, i+1, r); } void medianOfThreePivot(int items[], int low, int high){ // Swap median (value mid-way between l and r) with a[r - 1] // Compare a[l], a[r - 1] and a[r] // Rearrange values: // lowest value to be stored in a[l] // highest value to be stored in a[r] // median value to be stored in a[r-1] // You are required to provide an implementation for this in lab 07 } int partition (int a[], int l, int r) { int i = l-1; int j = r; int pivot = a[r]; //rightmost is pivot for(;;) { while ( a[++i] < pivot) ; while ( pivot < a[--j] && j != l); if (i >= j) { break; } swap(i,j,a); } //put pivot into place swap(i,r,a); return i; //Index of the pivot } void swap(int index1, int index2, int items[]){ int tmp; tmp = items[index1]; items[index1] = items[index2]; items[index2] = tmp; }
Exercise 6
Mergesort
- Briefly explain the top down recursive Mergesort algorithm.
Trace it's execution on the input: 4 7 1 1 4 6 7 2 5 6.
- Explain the good and bad points of the Mergesort algorithm.
Among the good points of Mergesort:
- O(n log(n)) worst case complexity
- Sort is stable provided merge operation is stable
- O(n log(n)) complexity on all cases (even easy ones like sorting ordered data)
- additional space proportional to n required for merge operation (although we could consider trying to implement an in-place merge) and space on the stack for the recursive calls