ITEC 2620A Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: 2620a.htm Office: TEL 3049.

Slides:



Advertisements
Similar presentations
Order of complexity. Consider four algorithms 1.The naïve way of adding the numbers up to n 2.The smart way of adding the numbers up to n 3.A binary search.
Advertisements

Growth-rate Functions
MATH 224 – Discrete Mathematics
Chapter 3: The Efficiency of Algorithms Invitation to Computer Science, Java Version, Third Edition.
Analysis of Algorithms
HST 952 Computing for Biomedical Scientists Lecture 10.
Fundamentals of Python: From First Programs Through Data Structures
Visual C++ Programming: Concepts and Projects
Searching and Sorting Topics  Sequential Search on an Unordered File  Sequential Search on an Ordered File  Binary Search  Bubble Sort  Insertion.
Simple Sorting Algorithms
Chapter 3: The Efficiency of Algorithms Invitation to Computer Science, C++ Version, Third Edition Additions by Shannon Steinfadt SP’05.
Chapter 3: The Efficiency of Algorithms Invitation to Computer Science, C++ Version, Fourth Edition.
Chapter 3: The Efficiency of Algorithms
Complexity (Running Time)
Algorithm Efficiency and Sorting
Analysis of Algorithms CS 477/677
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Analysis of Algorithm.
Sorting 2 An array a is sorted (ascending order) if: for all i a[i]  a[j] Probably the most well-studied algorithmic problem in Computer Science There.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
Simple Sorting Algorithms. 2 Bubble sort Compare each element (except the last one) with its neighbor to the right If they are out of order, swap them.
CSE 1301 J Lecture 13 Sorting Richard Gesick. CSE 1301 J 2 of 30 Sorting an Array When an array's elements are in random order, our Sequential Search.
Simple Sorting Algorithms. 2 Outline We are going to look at three simple sorting techniques: Bubble Sort, Selection Sort, and Insertion Sort We are going.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
{ CS203 Lecture 7 John Hurley Cal State LA. 2 Execution Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
ITEC 2620A Introduction to Data Structures
Reynolds 2006 Complexity1 Complexity Analysis Algorithm: –A sequence of computations that operates on some set of inputs and produces a result in a finite.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Chapter 19: Searching and Sorting Algorithms
© 2011 Pearson Addison-Wesley. All rights reserved 10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
 Pearson Education, Inc. All rights reserved Searching and Sorting.
Lecture 6 Sorting Algorithms: Bubble, Selection, and Insertion.
ITEC 2620A Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: 2620a.htm Office: TEL 3049.
SortingBigOh Sorting and "Big Oh" Adapted for ASFA from a presentation by: Barb Ericson Georgia Tech Aug 2007 ASFA AP Computer Science.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
SortingBigOh ASFA AP Computer Science A. Big-O refers to the order of an algorithm runtime growth in relation to the number of items I. O(l) - constant.
1 Today’s Material Iterative Sorting Algorithms –Sorting - Definitions –Bubble Sort –Selection Sort –Insertion Sort.
Computer Science 101 Introduction to Sorting. Sorting One of the most common activities of a computer is sorting data Arrange data into numerical or alphabetical.
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.
Fundamentals of Algorithms MCS - 2 Lecture # 15. Bubble Sort.
ITEC 2620M Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: ec2620m.htm Office: TEL 3049.
ITEC 2620M Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: ec2620m.htm Office: Tel 3049.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
3 – SIMPLE SORTING ALGORITHMS
Lecture #9: Sorting Algorithms خوارزميات الترتيب Dr. Hmood Al-Dossari King Saud University Department of Computer Science 22 April 2012.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Lecture 4 1 Advance Analysis of Algorithms. Selection Sort 2 Summary of Steps Find the smallest element in the array Exchange it with the element in the.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Searching Topics Sequential Search Binary Search.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.
ITEC 2620M Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: ec2620m.htm Office: TEL 3049.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
ITEC 2620M Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: 20m.htm Office: TEL 3049.
Introduction to complexity
ITEC 2620M Introduction to Data Structures
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Algorithm design and Analysis
Intro to Computer Science CS1510 Dr. Sarah Diesburg
ITEC 2620M Introduction to Data Structures
Chapter 3: The Efficiency of Algorithms
Lecture 6 Efficiency of Algorithms (2) (S&G, ch.3)
MSIS 655 Advanced Business Applications Programming
Chapter 3: The Efficiency of Algorithms
ITEC 2620M Introduction to Data Structures
Presentation transcript:

ITEC 2620A Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: a.htm Office: TEL 3049

Sorting

3 Key Points Non-recursive sorting algorithms –Selection Sort –Insertion Sort –Best, Average, and Worst cases

4 Sorting Why is sorting important? –Easier to search sorted data sets –Searching and sorting are primary problems of computer science Sorting in general –Arrange a set of elements by their “keys” in increasing/decreasing order. Example: How would we sort a deck of cards?

5 Selection Sort Find the smallest unsorted value, and move it into position. What do you do with what was previously there? –“swap” it to where the smallest value was –sorting can work on the original array Example

6 Pseudocode Loop through all elements (have to put n elements into place) –for loop Loop through all remaining elements and find smallest –initialization, for loop, and branch Swap smallest element into correct place –single method

7 Insertion Sort Get the next value, and push it over until it is “semi” sorted. –elements in selection sort are in their final position –elements in insertion sort can still move which do you use to organize a pile of paper? Example

8 Pseudocode Loop through all elements (have to put n elements into place) –for loop Loop through all sorted elements, and swap until slot is found –while loop –swap method

9 Bubble Sort Slow and unintuitive…useless Relay race –pair-wise swaps until smaller value is found –smaller value is then swapped up

10 Cost of Sorting Selection Sort –Is there a best, worst, and average case? two for loops  always the same –n elements in outer loop –n-1, n-2, n-3, …, 2, 1 elements in inner loop average n/2 elements for each pass of the outer loop –n * n/2 compares

11 Cost of Sorting (Cont’d) Insertion Sort –Worst – same as selection sort, next element swaps till end n * n/2 compares –Best – next element is already sorted, no swaps n * 1 compares –Average – linear search, 50% of values  n/4 n * n/4 compares

12 Value of Sorting Current cost of sorting is roughly n 2 compares Toronto phone book –2 million records –4 trillion compares to sort Linear search –1 million compares Binary search –20 compares

13 Trade-Offs Write a method, or re-implement each time? Buy a parking pass, or pay cash each time? Sort in advance, or do linear search each time? Trade-offs are an important part of program design –which component should you optimize? –is the cost of optimization worth the savings?

Complexity Analysis

15 Key Points Analysis of non-recursive algorithms –Estimation –Complexity Analysis –Big-Oh Notation

16 Factors in Cost Estimation Does the program’s execution depend on the input? –Math.max(a, b); always processes two numbers  constant time –maxValue(anArray); processes n numbers  varies with array size

17 Value of Cost Estimation Constant time programs –run once, always the same… –estimation not really required Variable time programs –run once –future runs depend on relative size of input based on what function?

18 Cost Analysis Consider the following code: sum = 0; for (i=1; i<=n; i++) for (j=1; j<=n; j++) sum++; It takes longer when n is larger.

19 Asymptotic Analysis “What is the ultimate growth rate of an algorithm as a function of its input size?” “If the problem size doubles, approximately how much longer will it take?” Quadratic Linear (linear search) Logarithmic (binary search) Exponential

20 Big-Oh Notation Big-Oh represents the “order of” the cost function –ignoring all constants, find the largest function of n in the cost function Selection Sort –n * n/2 compares + n swaps O(n 2 ) Linear Search –n compares + n increments + 1 initialization O(n)

21 Simplifying Conventions Only focus on the largest function of n Ignore smaller terms Ignore constants

22 Examples Example 1: –Matrix multiplication A nm * B mn = C nn Example 2: selectionSort(a); for (int i = 0; i < n; i++) binarySearch(i,a);

23 Trade-Offs and Limitations “What is the dominant term in the cost function?” –What if the constant term is larger than n? What happens if both algorithms have the same complexity? –Selection sort and Insertion sort are both O(n 2 ) Constants can matter –Same complexity (obvious) and different complexity (problem size)