Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

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

Material Type: Exam; Professor: Huggins; Class: Computing & Algorithms III; Subject: Computer Science; University: Kettering University; Term: Summer 2008;

Typology: Exams

Pre 2010

Uploaded on 08/07/2009

koofers-user-u68
koofers-user-u68 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

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 n1 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(n1) + 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.