


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An introduction to algorithms, focusing on sorting techniques such as bubble sort and selection sort. It covers preconditions, loop invariants, postconditions, and running time analysis. Students will learn about the best and worst-case scenarios for these algorithms and gain insights into their implementation.
Typology: Study notes
1 / 4
This page cannot be seen from the preview
Don't miss anything!



1
^ Preconditions^
Things that should be true before executing an algorithm ^ Postconditions^
Things that should be true after executing an algorithm ^ Loop Invariant^
A true statement that we can make about each step of the loop. i.e. At step i, (somestatement) is true or
a condition that doesn’t change
^ Running Time of an Algorithm - Big O^
Running time in terms of a large input, n. For example: O(n) is n^2 ->Big O of n is n squared i.e. for a large input n, the running time is n squared ^ Divide and Conquer Algorithm
2
^ Divide
and Conquer Algorithm Solve a problem by dividing it in to pieces. ^ Key^
When searching, it’s the thing we’re looking for
3
^ Compare each element (except the last one) with its neighbor tothe right^
If they are out of order, swap them This puts the largest element at the very end The last element is now in the correct and final place The last element is now in the correct and final place ^ Compare each element (except the last
two ) with its neighbor to
the right^ ^ If they are out of order, swap them^ ^ This puts the second largest element next to last^ ^ The last two elements are now in their correct and final places Compare each element (except the last
three
) with its neighbor to
4
^ Compare each element (except the last
three
) with its neighbor to
the right^ ^ Continue as above until you have no unsorted elements on the left This causes larger values to "bubble" to the end of the list whilesmaller values "sink" towards the beginning of the list.
^
Preconditions: array may or may not be sorted ^
Algorithm:start = 0, end = (array length - 1)Loop:
Compare successive pairs of items and put the larger “on the right”This will “bubble” the largest one all the way to the right.start = start + 1If (start == end) we’re done. The array is sorted. ^
Loop Invariant: At step i, the i largest items have been placed.
5
^
Postcondition: Array is sorted ^
What is the best/worst running time given n items? Best and worst time:(n-1) + (n-2) + … 1 = (n+1)(n)/2 which is approximately n^2If the array is already sorted AND the bubblesort is optimized toquit if no switches are done, then the best time is n.
public static void bubbleSort(int[] a){for (int i=0; i < a.length - 1; i++) {
// if a pass is made with no swaps, we're done.boolean swappedElements = false;for (int j=0; j< a.length - 1 - i; j++){// compare neighbors. if the earlier one is bigger, swap.if (a[j] > a[j + 1]) {
int tmp = a[j];a[j] = a[j+1];a[j+1] = tmp;swappedElements = true;}
6
} }if (! swappedElements)break; }}
^
Preconditions: an unsorted (or sorted) array ^
Algorithm
Algorithm
start = 0, end = (array length - 1)Loop:If (start == end) we’re doneFind smallest item in series array[start] to array[end]Switch array[start] with the smallest item.start = start + 1 ^
Loop Invariant: After step i, the first i items in the array contain thesmallest i items in sorted order.
7
^
Postcondition: Array is sorted ^
Running time (average – best & worst) Selecting the lowest element requires scanning all
n^ elements (this takes
n
−^ 1 comparisons) and then swapping it into the first position. ^
Finding the next lowest element requires scanning the remaining
n^ −^1
elements and so on n + (n-1) + (n-2) + … 1 = (n+1)(n)/2 which is approximately n^
^
Preconditions: a
sorted
array and a “key”
^
Algorithm:Compare the key with the array’s
middle
item
Compare the key with the array s
middle
item
Loop:If they’re the same, we’re done (return the index)If the key is smaller, consider the left subarrayIf the key is larger, consider the right subarrayIf the subarray size is 0, we’re done (return failure)Otherwise, compare key with the subarray’s
middle
item
12
Otherwise, compare key with the subarray s
middle
item
^
Loop Invariant: After step i, the size of the dataset yet to be searched is(array length / 2^i) ^
Postcondition: the index of an occurrence of the key is returned, or
an error message is transmitted if the key is not found
^
Best case running time? Worst case?