Professional Documents
Culture Documents
CSCI1933 Lecture6
CSCI1933 Lecture6
• Possible metrics:
– Correctness!
– How much time does it take? (time complexity)
– How much memory does it require? (space
complexity)
(we can precisely quantify these)
• Big Oh
O(g(n))
- f(n) is of order at most g(n)
• Big Omega
Ω (g(n))
– f(n) is of order at least g(n)
(lower bound)
• Big Theta Θ (g(n))
– f(n) is of order g(n)
– cg(n) is both a lower and upper bound
(closest estimate of complexity)
Exercise: estimating time complexity
sum=0;
for (int i=1; i <= n; i++) {
for(int j=1; j <= n; j++) {
sum = sum+1;
}
}
Exercise: estimating time
complexity
return result;
}
Complexity of common sorting algorithms
Sorting algorithms
• Algorithm #1
• Find the minimum value in the list “Selection” sort
• Swap it with the value in the first position
• Repeat the steps above for the remainder of the list (starting at the second position and
advancing each time)
• Algorithm #2
• Iterate from 1st to last element of the array “Insertion” sort
• Compare the current element to its predecessor
• If curr. element smaller, compare it to elements before; move greater elements right one
position to make space for smaller element
• Algorithm #3
“Merge sort”
- Divide an array into halves
- Sort the two halves (recursively)
- Merge them into one sorted array
• Algorithm #3 “Quicksort”
– Pick an element from the list (pivot).
– Reorder the list so that all elements less than the pivot come before the pivot and so that all
elements greater than the pivot come after it (equal values can go either way).
– Repeat the process on the sub-list of lesser elements and the sub-list of greater elements.
Summary of various sorting
algorithms
Scan steps:
Swap steps: