next up previous
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 = tex2html_wrap_inline62 (Happens when the input is sorted in reserve order)
- Best-Case = tex2html_wrap_inline64 (Happens when the input is already sorted)
- Average-Case = tex2html_wrap_inline62 (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.

displaymath56

The solution to this recurrence relation is tex2html_wrap46 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
tex2html_wrap_inline68
tex2html_wrap47 (Insert from the right. It happens when the array is already sorted)
tex2html_wrap_inline68
tex2html_wrap48
tex2html_wrap_inline68
tex2html_wrap49
- Binary Insertion (AVL-height balanced binary tree, etc.) (In this case, insertions "costs" tex2html_wrap50 steps.)
tex2html_wrap_inline68
Worst-case = tex2html_wrap_inline76
tex2html_wrap_inline68
Best-case = tex2html_wrap_inline76
tex2html_wrap_inline68
Average-case = tex2html_wrap_inline76
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 tex2html_wrap_inline90
X[k+1..n] - Each element is tex2html_wrap_inline92
(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.

displaymath57


(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 tex2html_wrap_inline100 , and tex2html_wrap_inline102 . This gives us the recurrence relation.

displaymath58


Other Sorts:




next up previous
Next: About this document

David W. Juedes
Wed Oct 9 00:10:10 EDT 1996