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

Slides:



Advertisements
Similar presentations
Back to Sorting – More efficient sorting algorithms.
Advertisements

Introduction to Computer Science Theory
Garfield AP Computer Science
CSE Lecture 3 – Algorithms I
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Math 130 Introduction to Computing Sorting Lecture # 17 10/11/04 B Smith: Save until Week 15? B Smith: Save until Week 15? B Smith: Skipped Spring 2005?
Stephen P. Carl - CS 2421 Recursive Sorting Algorithms Reading: Chapter 5.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
Sorting Algorithms and Average Case Time Complexity
CS203 Programming with Data Structures Sorting California State University, Los Angeles.
CS Data Structures I Chapter 10 Algorithm Efficiency & Sorting III.
Faster Sorting Methods Chapter 9. 2 Chapter Contents Merge Sort Merging Arrays Recursive Merge Sort The Efficiency of Merge Sort Merge Sort in the Java.
1 Sorting/Searching CS308 Data Structures. 2 Sorting means... l Sorting rearranges the elements into either ascending or descending order within the array.
1 C++ Plus Data Structures Nell Dale Chapter 10 Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
E.G.M. Petrakissorting1 Sorting  Put data in order based on primary key  Many methods  Internal sorting:  data in arrays in main memory  External.
C++ Plus Data Structures
Cmpt-225 Sorting. Fundamental problem in computing science  putting a collection of items in order Often used as part of another algorithm  e.g. sort.
Lecture 4 Sept 4 Goals: chapter 1 (completion) 1-d array examples Selection sorting Insertion sorting Max subsequence sum Algorithm analysis (Chapter 2)
CS2420: Lecture 9 Vladimir Kulyukin Computer Science Department Utah State University.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
Sorting Algorithms Insertion and Radix Sort. Insertion Sort One by one, each as yet unsorted array element is inserted into its proper place with respect.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
ALGORITHM ANALYSIS AND DESIGN INTRODUCTION TO ALGORITHMS CS 413 Divide and Conquer Algortihms: Binary search, merge sort.
Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.
Recursion, Complexity, and Searching and Sorting By Andrew Zeng.
Chapter 16: Searching, Sorting, and the vector Type.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
Recursion, Complexity, and Sorting By Andrew Zeng.
Chapter 12 Recursion, Complexity, and Searching and Sorting
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Heapsort. Heapsort is a comparison-based sorting algorithm, and is part of the selection sort family. Although somewhat slower in practice on most machines.
1 Arrays: Matrix Renamed Instructor: Mainak Chaudhuri
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO CHAPT 12.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 12 – Data Structures – Trees Sorting Algorithms.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use by MSU Dept. of Computer Science.
Sorting Algorithms Discrete Mathematics Keyang He.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 13 October 13, 2009.
1 C++ Plus Data Structures Nell Dale Chapter 10 Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO CH 2, APPENDIX E.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Recursion.
Sorting. Sorting Sorting is important! Things that would be much more difficult without sorting: –finding a telephone number –looking up a word in the.
Selection Sort main( ) { int a[ ] = { 17, 6, 13,12, 2 } ; int i, j, t ; for ( i = 0 ; i
Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University.
Reading from a file, Sorting, and a little Searching Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics,
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Week 13 - Wednesday.  What did we talk about last time?  NP-completeness.
Review Quick Sort Quick Sort Algorithm Time Complexity Examples
Merge Sort Presentation By: Justin Corpron. In the Beginning… John von Neumann ( ) Stored program Developed merge sort for EDVAC in 1945.
1 Arrays: Matrix Renamed Instructor: Mainak Chaudhuri
Sorting and Runtime Complexity CS255. Sorting Different ways to sort: –Bubble –Exchange –Insertion –Merge –Quick –more…
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 23 Sorting.
Command Line Arguments
COMP 53 – Week Seven Big O Sorting.
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Algorithm Efficiency and Sorting
CSE 373 Data Structures and Algorithms
CMPT 225 Lecture 10 – Merge Sort.
Presentation transcript:

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)