NOTE:
Because class was cancelled last week:
Due date for the following assignments has been extended to Feb 12th:
2: WriteBackwards
3: Palindrome
4: Towers
The following assignments are now EXTRA CREDIT:
5: SelectionSort
6: BubbleSort
7: InsertionSort
Reminders About Homework:
MUST be stapled on RIGHT side of code
NO handwritten work
MUST have the following in the comment area at top of code:
STUDENT NAME
PROGRAM NAME
PROGRAM DATE
PROGRAM DESCRIPTION
ASSIGNMENT NBR (from syllabus)
Source Code MUST compile and run
Assignments MUST be handed in ELECTRONICALLY and PRINTED
ANY Assignment not meeting the above criteria will be returned ungraded.
Corrected version WILL BE subject to late penalties as outlined in the syllabus.
Also About the Homework:
Returned papers and disks are alphabetized - please keep them that way
If there is no red mark on a line on the Grade Sheet, that means that
item does not apply to the assignment - your grade is not based
on those items.
Please use Courier or some other fixed-width font when printing.
A cover page is NOT necessary
Algorithm Efficiency
Execution Time
Count the number of operations required to complete the solution
Time to execute is dependent upon the computer hardware and operating system
Algorithm Growth Rates
Essentailly, how fast does the amount of time required to execute an algorithm
increase with the size of the problem?
Order of Magnitude and Big O Notation
O(f(N))
Given a function of N, what is the order of magnitude for its growth rate?
Simple Sorting Techniques
Bubble Sort
1. Move up an array (by index value), compare each adjacent pair of elements.
2. If the lower element has a larger value than the upper element, swap them.
3. After the first pass through the array, the largest value has "bubbled" to the top.
Recursive process:
Repeat steps 1-3 above, ignoring the last element in the array.
What would be the number of operations required if the array contained 5 elements?
What is the equation for the growth rate?
What would be the number of operations required if the array contained 10 elements?
What is the growth rate?
void BubbleSort(string A[], int N) {
bool Sorted = false;
for (int Pass=1; (Pass 0 ) {
Swap(A[Index], A[NextIndex]);
Sorted = false;
}
}
}
}
Selection Sort
1. Store the index of the first element in a variable (0).
2. Move up an array (by index value), compare each element to the stored element.
3. If the element is larger than the stored element, store its index in the variable.
4. After the first pass through the array, the variable stores the index of the largest value.
the largest value has been "selected" from the array
5. Swap the element at that position with the last position in the array.
Recursive process:
Repeat steps 1-5 above, ignoring the last element in the array.
What would be the number of operations required if the array contained 5 elements?
What is the equation for the growth rate?
What would be the number of operations required if the array contained 10 elements?
What is the growth rate?
void SelectionSort(string A[], int N) {
for ( int Last=N-1; Last >=1; --Last) {
int L = IndexOfLargest(A, Last+1);
Swap(A[L], A[Last]);
}
}
Insertion Sort
1. Create a second array of the same type and size as the first.
2. Copy the first element of the first array into the first position of the second array
3. Compare the next element in the first array with each element in the second array.
4. If an element in the second array is larger than the element from the first,
move it up one position in the second array and "insert" the element
from the first array
Recursive process:
Repeat steps 3-4 above.
What would be the number of operations required if the array contained 5 elements?
What is the equation for the growth rate?
What would be the number of operations required if the array contained 10 elements?
What is the growth rate?
void InsertionSort(string A[], int N) {
for (int Unsorted=1; Unsorted0) && (strcmp(A[Loc-1], NextItem)>0); --Loc) {
strcpy(A[Loc], A[Loc-1]);
shiftCount++;
}
strcpy(A[Loc], NextItem);
}
}