CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO CHAPTER 11
Agenda HW 4 Review Big O notation. Sorts, MergeSort Hand Back Midterm
HW4 Passing in command line arguments Iterative Merge Sort What to turn in Other questions, clarifications Due date postponed until Friday 2.27
Command lines arguments to a C++ program Program.exe param1 param2 param3 main(argc, *argv[]) { } argc: number of command line arguments argv[] Array of pointers to characters argv[0] being the program name Others each hold a command line argument Quick/simple programming example: write a program which outputs the command line arguments if there are 3 of them
int main(int argc, char* argv[]) { if (argc == 4) { for (int i = 1; i < argc; i++) { cout << argv[i] << endl; } return 0; } Simple command line arg program
Review: Analysis and Big O Notation ◦Algorithm A is order f ( n ), denoted O( f ( n )) ◦If constants k and n 0 exist ◦Such that A requires no more than k f ( n ) time units to solve a problem of size n ≥ n 0
Big-O class problem(s) What is the Big-O complexity of an algorithm which requires 7n + 5n operations for a data set of size n. Prove your answer. for (int i = 1; i <= n; i++) { for (int j = i; j < i * n; j++) { FuncX(); }
Sorts and sorting
Sorting the Sorts Selection Sortworst/average O(n 2 ) Bubble Sortworst/average O(n 2 ) Insertion Sortworst/average O(n 2 ) Shell Sortworst O(n 2 )/average O(n 3/2 ) Merge Sortworst/average O(n log n) Quick Sortworst O(n 2 )/average O(n log n) Radix Sortworst/average O(n)
Sorts previously covered Bubble Sort: example-300px.gif example-300px.gif let’s determine why it is O(n 2 ) Insertion Sort: example-300px.gif example-300px.gif let’s determine why it is O(n 2 )
Efficiency of Bubble Sort: O(n 2 ) Comparison Swapping N-1 N ………… Pass 1Pass 2
Efficiency of Insertion Sort: O(n 2 ) Sorted Unsorted Comp.ShiftInsert Operations
CSS342: SORTING ALGORITHMS13 MergeSort Key: THE MERGE! Assuming that we have already had two sorted array, How can we merge them into one sorted array?
14 firstmid last sorted first1 last1 first2 last2 theArray tempArray < >= index firstmid sorted first1 last1 first2 last2 theArray tempArray index
Computer Scientist of the week (very difficult…. possibly NP- Complete) Stephen Cook Forefather of computational complexity theory Theory of NP-Completeness Prof. at University of Toronto 1982: Turing award winner
void Merge(vector &itemVector, int first, int mid, int last) { int *tempArr; int size = last - first + 1; tempArr = new int[size]; int first1 = first; int last1 = mid; int first2 = mid + 1; int last2 = last; int index = 0; while ((first1 <= last1) && (first2 <= last2)) { if (itemVector[first1] < itemVector[first2]) { tempArr[index] = itemVector[first1]; first1++; } else { tempArr[index] = itemVector[first2]; first2++; } index++; }
while (first1 <= last1) { tempArr[index] = itemVector[first1]; first1++; index++; } while (first2 <= last2) { tempArr[index] = itemVector[first2]; first2++; index++; } for (index = 0; index < size; index++) { itemVector[first] = tempArr[index]; first++; } delete[] tempArr; }
Computer Scientist of the week John Von Neumann! Innovations in Set theory, Geometry, Quantum Mechanics, Economics, Statistics Founded Game Theory Monte Carlo Method EDVAC: data and program both in same address space ENIAC: First computer to use a stored program Von Neumann Architecture! Manhattan Project MAD: Mutually Assured Destruction Merge Sort Algorithm
MergeSort: successive merges Use recursion to get here
MergeSort first mid=(fist + last)/2 last theArray firstlastfirstlast mid=(fist + last)/ first last first < last
MergeSort: Overview first mid=(fist + last)/2 last theArray firstlast void MergeSort(vector &iVector, int first, int last) { if (first < last) { int mid = (first + last) / 2; MergeSort(iVector, first, mid); MergeSort(iVector, mid + 1, last); Merge(iVector, first, mid, last); } sort-example-300px.gif
MergeSort: (Efficiency Analysis) Level# sub-arrays#comparisons # copies per merge * * * 2 Xn/2 x 2 x -1 2 x * 2 At level X, #nodes in each sub-array = 2 x At level X, # major operations = n/2 x * (3 * 2 x – 1) = O(3n) #levels = log n, where n = # array elements ( if n is a power of 2 ) #levels = log n + 1 if n is not a power of 2 # operations = O(3n) * (log n + 1) = O(3 n log n) = O(n log n)