


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



CSC517: Data Structures and Algorithms - Spring 2005 Dr. Christian A. Duncan [email protected]
In class we covered the following topics:
Look over pseudocode for the following algorithm Sort(A, n) used to sort an array A of n numbers (via SelectionSort):
Algorithm Sort(A,n): Input: Array A of n>=1 integers Output: The Array A in sorted (increasing) order Begin for i := 1 to n-1 do for j := i+1 to n do if (A[i] > A[j]) then swap(A[i], A[j]) return A End
Why is this algorithm correct? How long will it take? We shall count it step by step. Why is such a method of counting going to be very ineffective and difficult for harder algorithms?
The above code and analysis was for an iterative approach. Another means of coding is via recursion. Recursive algorithms often are easier to analyze (once we learn some new techniques). Here is a (somewhat) recursive solution to the above sorting algorithm:
Algorithm Sort(A,n): Input: Array A of n>=1 integers (index starts at 1) Output: The Array A in sorted (increasing) order Begin if (n <= 1) return A for j := n-1 downto 1 do if (A[n] < A[j]) then swap(A[n], A[j]) Sort(A,n-1) return A End
Section 1.3 and Appendix A give basic math material that should be familiar or at least easily understood by everyone in class. We shall look over each theorem quickly to make sure of this.
∑n i=0 a
i (^) = 1 −an+ 1 −a.
∑n i=1 i^ =^
n(n+1)