Top Banner
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 12. 150223. CARRANO CHAPTER 11
22

CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 12. 150223. CARRANO CHAPTER…

Jan 19, 2018

Download

Documents

Doris Walsh

HW4 Passing in command line arguments Iterative Merge Sort What to turn in Other questions, clarifications Due date postponed until Friday 2.27
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 12. 150223. CARRANO CHAPTER…

CSS 342DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS ILECTURE 12. 150223.CARRANO CHAPTER 11

Page 2: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 12. 150223. CARRANO CHAPTER…

Agenda• HW 4

• Review Big O notation.

• Sorts, MergeSort

• Hand Back Midterm

Page 3: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 12. 150223. CARRANO CHAPTER…

HW4• Passing in command line arguments

• Iterative Merge Sort

• What to turn in

• Other questions, clarifications

• Due date postponed until Friday 2.27

Page 4: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 12. 150223. CARRANO CHAPTER…

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

Page 5: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 12. 150223. CARRANO CHAPTER…

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

Page 6: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 12. 150223. CARRANO CHAPTER…

Review: Analysis and Big O Notation◦Algorithm A is order f ( n ), denoted O( f ( n ))

◦ If constants k and n0 exist ◦ Such that A requires no more than k f ( n ) time units to solve a

problem of size n ≥ n0

Page 7: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 12. 150223. CARRANO CHAPTER…

Big-O class problem(s)• What is the Big-O complexity of an algorithm which requires 7n + 5n3 + 9 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();}

}

http://courses.washington.edu/css342/dimpsey/ProgramExamples/BigOwAnswers.pdf

Page 8: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 12. 150223. CARRANO CHAPTER…

Sorts and sorting

Page 9: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 12. 150223. CARRANO CHAPTER…

Sorting the Sorts Selection Sort worst/average O(n2) Bubble Sort worst/average O(n2) Insertion Sort worst/average O(n2) Shell Sort worst O(n2)/average O(n3/2) Merge Sort worst/average O(n log n) Quick Sort worst O(n2)/average O(n log n) Radix Sort worst/average O(n)

Page 11: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 12. 150223. CARRANO CHAPTER…

Efficiency of Bubble Sort: O(n2)

29 10 14 1337

2910 14 1337

2910 14 1337

2910 14 1337

2910 14 13 37

2910 14 13 37

2910 14 13 37

2910 14 13 37

2910 14 13 37

ComparisonSwapping

N-1N-1

N-2N-2

11

……

Pass 1 Pass 2

Page 12: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 12. 150223. CARRANO CHAPTER…

Efficiency of Insertion Sort: O(n2)

29 10 14 13372910 14 13372910 14 13371410 29 13371410 29 13371410 29 13371410 29 13371310 14 3729

Sorted Unsorted Comp. Shift Insert Operations

1 1 1 2+1

2 1 1 3+1

3 1 1 4+1

2 3 1 5+1

Page 13: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 12. 150223. CARRANO CHAPTER…

CSS342: SORTING ALGORITHMS 13

MergeSort

1 4 8 13 14 20 25 2 3 5 7 11 23

Key: THE MERGE!Assuming that we have already had two sorted array,How can we merge them into one sorted array?

2 3 5 7 11 23 25201413841

Page 14: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 12. 150223. CARRANO CHAPTER…

14

first mid last

sorted sorted

first

1

last

1fir

st2

last

2

theArray

tempArray

< >=

inde

x

first midsorted sorted

first

1

last

1

first

2las

t2

theArray

tempArray

inde

x

Page 15: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 12. 150223. CARRANO CHAPTER…

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

Page 16: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 12. 150223. CARRANO CHAPTER…

void Merge(vector<int> &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++; }

Page 17: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 12. 150223. CARRANO CHAPTER…

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;}

Page 18: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 12. 150223. CARRANO CHAPTER…

Computer Scientist of the weekJohn 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

Page 19: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 12. 150223. CARRANO CHAPTER…

MergeSort: successive merges38 16 17123927 24 5

3816 27 39 12 17 245

16 3827 39 5 12 2417

5 12 16 17 24 38 3927

Use recursion to get here

Page 20: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 12. 150223. CARRANO CHAPTER…

MergeSort

38 16 17123927 24 5

38 16 17123927 24 5

firstmid=(fist + last)/2

last

theArray

38 16 3927 1712 24 5first last first last

mid=(fist + last)/2mid=(fist + last)/2

38 16 3927 1712 24 5first

first

last

last

first < last

Page 21: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 12. 150223. CARRANO CHAPTER…

MergeSort: Overview 38 16 17123927 24 5first

mid=(fist + last)/2last

theArray

38 16 3927 1712 24 5

38 16 3927 1712 24 5firstlast

38 16 17123927 24 5

3816 27 39 12 17 245

16 3827 39 5 12 2417

5 12 16 17 24 38 3927

void MergeSort(vector<int> &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);

}}

http://en.wikipedia.org/wiki/Merge_sort#mediaviewer/File:Merge-sort-example-300px.gif

Page 22: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 12. 150223. CARRANO CHAPTER…

MergeSort: (Efficiency Analysis)38 16 17123927 24 5

3816 27 39 12 17 245

16 3827 39 5 12 2417

5 12 16 17 24 38 3927

Level # sub-arrays #comparisons # copies per merge

1 4 12 * 2

2 2 34 * 2

3 1 78 * 2

X n/2x 2x-12x * 2 At level X, #nodes in each sub-array = 2x

At level X, # major operations = n/2x * (3 * 2x – 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)