Docsity

2 Solved Questions on Sorting Algorithm - Midterm Exam | CS 203, Exams of Algorithms and Programming

Toggle sidebar
bg1
On the Analysis of GoofySort
James K. Huggins
26 August 2008
The Setup
On the midterm exam for CS-203, Summer 2008, I assigned the following problem:
2. Consider the following sorting algorithm:
public static void goofySort (int[] array, int start, int stop) {
if (start>=stop) return;
goofySort(array, start, stop-1);
if (array[stop-1] > array[stop]) {
int swap = array[stop-1];
array[stop-1] = array[stop];
array[stop] = swap;
goofySort(array, start, stop-1);
}
}
a. 5 points. Explain informally why this algorithm correctly sorts the input array.
b. 10 points. Compute the exact number of comparisons performed by this sort in
terms of n, the size of the array, in the worst case. (Hint: what is the recurrence
relation for this problem?)
c. 10 points. Compute the exact number of comparisons performed by this sort in
terms of n, the size of the array, in the best case. (Hint: what is the recurrence
relation for this problem?)
It is easy to see that, in the best case, GoofySort makes exactly nโˆ’1 comparisons.
In the best case, the comparison in the โ€œifโ€ statement always fails, leading to exactly one
comparison and one recursive call. Thus, we have the following recurrence relation:
T(n) = T(nโˆ’1) + 1; T(1) = 0
1
pf3
pf4

Partial preview of the text

Download 2 Solved Questions on Sorting Algorithm - Midterm Exam | CS 203 and more Exams Algorithms and Programming in PDF only on Docsity!

On the Analysis of GoofySort

James K. Huggins

26 August 2008

The Setup

On the midterm exam for CS-203, Summer 2008, I assigned the following problem:

  1. Consider the following sorting algorithm:

public static void goofySort (int[] array, int start, int stop) { if (start>=stop) return; goofySort(array, start, stop-1); if (array[stop-1] > array[stop]) { int swap = array[stop-1]; array[stop-1] = array[stop]; array[stop] = swap; goofySort(array, start, stop-1); } }

a. 5 points. Explain informally why this algorithm correctly sorts the input array. b. 10 points. Compute the exact number of comparisons performed by this sort in terms of n, the size of the array, in the worst case. (Hint: what is the recurrence relation for this problem?) c. 10 points. Compute the exact number of comparisons performed by this sort in terms of n, the size of the array, in the best case. (Hint: what is the recurrence relation for this problem?)

It is easy to see that, in the best case, GoofySort makes exactly n โˆ’ 1 comparisons. In the best case, the comparison in the โ€œifโ€ statement always fails, leading to exactly one comparison and one recursive call. Thus, we have the following recurrence relation:

T (n) = T (n โˆ’ 1) + 1; T^ (1) = 0

The worst case analysis, however, is much more interesting. A naive analysis would observe that, in the worst case, the comparison in the โ€œifโ€ state- ment always succeeds, leading to the nested recursive call occuring each time. This leads to the following recurrence relation:

T (n) = 2T (n โˆ’ 1) + 1; T (1) = 0

A simple analysis of this recurrence relation leads to a solution of T (n) = 2nโˆ’^1 โˆ’ 1. However, after the administration of the exam, several students noted that GoofySort appeared to have a polynomial runtime, as observed by an empirical analysis. A detailed empirical analysis lead to to the discovery of the following tight bound for the number of comparisons performed:

T (n) =

n(n โˆ’ 1)(n โˆ’ 2) 6

  • n โˆ’ 1

In the following, we explore the behavior of this deceptively simple sorting algorithm and formally derive the equation above.

Preliminary Work

Our final analysis will rely upon two properties of the positive integers, which, for complete- ness, we formally prove by induction.

Lemma 1

โˆ‘n i=1 i^ =^

n(n+1)

Proof. By induction on n.

Base Case. For n = 1, we have 1 = 1(2) 2.

Inductive Case. We have: (^) โˆ‘ n i=1 i^ =^ (

โˆ‘nโˆ’ 1 i=1 i) +^ n = (nโˆ’ 2 1) n+ 22 n = (n+1) 2 n ยง.

Lemma 2

โˆ‘n i=1 i(i^ + 1) =^

n(n+1)(n+2)

Proof. By induction on n.

Base Case. For n = 1, we have 1(2) = 1(2)(3) 3.

Inductive Case. We have: โˆ‘n i=1 i(i^ + 1)^ =^ (

โˆ‘nโˆ’ 1 i=1 i(i^ + 1)) +^ n(n^ + 1) = (nโˆ’1)n 3 ( n+1)+ 3(n)( 3 n+1) = (n+2)n 3 ( n+1) ยง.

T (n) = T (n โˆ’ 1) + 1 +

(n โˆ’ 1)(n โˆ’ 2) 2

; T (1) = 0

Solving this recurrence relation yields the following equation:

T (n) = n โˆ’ 1 +

โˆ‘^ nโˆ’^2

i=

i(i + 1)

By Lemma 2, we have:

T (n) = n โˆ’ 1 + (

(n โˆ’ 2)(n โˆ’ 1)n 3

= n โˆ’ 1 +

n(n โˆ’ 1)(n โˆ’ 2) 6

Final Remarks

An empirical analysis was conducted, sorting all possible arrays of length โ‰ค 9 and calculating the largest number of comparisons performed. For each n โ‰ฅ 9, the maximum number of comparisons performed matches the formula derived above. This worst case occurs when the array begins in descending order. I am reminded of the famous remarks of Don Knuth, who once said โ€œBeware of bugs in the above code; I have only proved it correct, not tried it.โ€ I suppose the same can be same of run-time analyses.