Next: About this document
Scribe Notes for
Design & Analysis of Algorithms
CS 404/504
Professor David Juedes
written by: Hong Lin
September 26, 1996
The complexity of BubbleSort.....
-
- - Worst-Case =
(Happens when the input is sorted in reserve order)
-
- - Best-Case =
(Happens when the input is already sorted)
-
- - Average-Case =
(See Knuth, Art of Computer Programming, Vol3, 1971)
We can think of the Bubble sort as a divide and conquer algorithm
that operates as follows:
Bubble_Sort (A array, n integer)
begin
sorted = TRUE;
for i=1 to n-1 do
begin
if A[i] > A[i+1] then
Exchange (A[i],A[i+1])
sorted FALSE;
end;
if sorted
return;
else
Bubble sort(A,n-1);
end;
This version of the Bubblesort admits the following recurrence relation.
The solution to this recurrence relation is
Other Sorting Algorithms:
Insertion_Sort (X:array, n: integer)
begin
for i=2 to n do
Insert X[i], into X[1..i-1];
end;
The complexity of the insertion sort depends upon the means of insertion.
-
- - Straight Insertion
-
-
(Insert from the right. It happens when the array is already sorted)
-
-
-
-
-
- - Binary Insertion (AVL-height balanced binary tree, etc.) (In this case, insertions "costs"
steps.)
-
- Worst-case =
-
- Best-case =
-
- Average-case =
Quick Sort:
This sort is an excellence example of a divide and conquer algorithm.
The QuickSort works as follows:
(1). Pick an element s to partition the array around.
Ex:
-
- s=X[1];
-
- s=Random element from X[1..n];
-
- s=median of X[1..n];
(2) Partition X about s
-
- X[1..k] - Each element is
-
- X[k+1..n] - Each element is
(3). Call
-
- QuickSort(X[1..k]);
-
- QuickSort(X[k+1..n]);
Analysis:
Let's look at a recurrence relation for the running time for
Quicksort in the best/worst cases. If we choose s to be X[1],
then we get the following recurrence relation for the worst-case running time.
(This happens when the array is sorted already)
If we choose s to be the median element
(and there are no elements in the array), then Quicksort is
called recursively on arrays of size
, and
.
This gives us the recurrence relation.
Other Sorts:
- Shellsort
- Heapsort =
in the best-, worst-, and average-case. - Selection Sort =
in the best-, worst-, and
average-case.
Next: About this document
David W. Juedes
Wed Oct 9 00:10:10 EDT 1996