Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Sort, Search, and Running Time.

Slides:



Advertisements
Similar presentations
Algorithms Algorithm: what is it ?. Algorithms Algorithm: what is it ? Some representative problems : - Interval Scheduling.
Advertisements

Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
Back to Sorting – More efficient sorting algorithms.
CSE Lecture 3 – Algorithms I
Chapter 6: Arrays Java Software Solutions for AP* Computer Science
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
ADSA: Sorting/ Advanced Data Structures and Algorithms Objective –examine popular sorting algorithms, with an emphasis on divide and conquer.
Ver. 1.0 Session 5 Data Structures and Algorithms Objectives In this session, you will learn to: Sort data by using quick sort Sort data by using merge.
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Standard Template Library Ming.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 14 Ming Li Department of.
Binary Search Visualization i j.
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Introduction of Concepts Ming Li.
1 ICS103 Programming in C Lecture 14: Searching and Sorting.
1 Lecture 23:Applications of Arrays Introduction to Computer Science Spring 2006.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A10 – Elementary Algorithms (Revision)
Algorithm Efficiency and Sorting
Searching Arrays Linear search Binary search small arrays
Main Index Contents 11 Main Index Contents Selection Sort Selection SortSelection Sort Selection Sort (3 slides) Selection Sort Alg. Selection Sort Alg.Selection.
Analysis of Algorithm.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 8 Ming Li Department of.
Main Index Contents 11 Main Index Contents Lecture 4 CSN 331 – Fall 2004 Chapter 15 Fibonacci (again) Dynamic Programming Permutations Eight Queens BacktrackingSummary.
1 Divide and Conquer Binary Search Mergesort Recurrence Relations CSE Lecture 4 – Algorithms II.
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
Chapter 8 ARRAYS Continued
Seattle Preparatory School Advanced Placement Computer Science Seattle Preparatory School Advanced Placement Computer Science LESSON 62 FEBRUARY 12, 2015.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
1 Sorting Algorithms (Basic) Search Algorithms BinaryInterpolation Big-O Notation Complexity Sorting, Searching, Recursion Intro to Algorithms Selection.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
Chapter 2 Array Data Structure Winter Array The Array is the most commonly used Data Storage Structure. It’s built into most Programming languages.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
CS 162 Intro to Programming II Searching 1. Data is stored in various structures – Typically it is organized on the type of data – Optimized for retrieval.
Big Oh Algorithms are compared to each other by expressing their efficiency in big-oh notation Big O notation is used in Computer Science to describe the.
ADSA: IntroAlgs/ Advanced Data Structures and Algorithms Objective –introduce algorithm design using basic searching and sorting, and remind.
 O(1) – constant time  The time is independent of n  O(log n) – logarithmic time  Usually the log is to the base 2  O(n) – linear time  O(n*logn)
Data Structures & Algorithms Week 4. CHAPTER 3: SEARCHING TECHNIQUES 1. LINEAR (SEQUENTIAL) SEARCH 2.BINARY SEARCH 3. COMPLEXITY OF ALGORITHMS.
Data Structure Introduction.
ADSA: Generics/ Advanced Data Structures and Algorithms Objective –to describe basic forms of generic classes, interfaces, and methods for searching.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 5 Generic.
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.
Introduction to Algorithms: Verification, Complexity, and Searching (2) Andy Wang Data Structures, Algorithms, and Generic Programming.
Introduction to Analysis of Algorithms CS342 S2004.
 MergeSort is a sorting algorithm which is more on the advanced end.  It is very fast, but unfortunately uses up a lot of memory due to the recursions.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 4 Introduction.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
Algorithm Analysis Lakshmish Ramaswamy. Formal Definitions Big-Oh: T(N) is O(F(N)) if there exists positive constants N 0 and c such that T(N) N 0 Big-Omega:
1 11/12/04CS150 Introduction to Computer Science 1 More Arrays.
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
The Selection Sort Mr. Dave Clausen La Cañada High School.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
12. Searching/Sorting Programming in C++ Computer Science Dept Va Tech August, 2000 © Barnette ND, McQuain WD, Keenan MA 1 Simple Searching Many.
Searching and Sorting Arrays
CS1010 Programming Methodology
Introduction to Search Algorithms
Computer Science 112 Fundamentals of Programming II
Searching & Sorting "There's nothing hidden in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting.
Sorting Data are arranged according to their values.
Topics discussed in this section:
Lecture 14: binary search and complexity reading:
Sorting Data are arranged according to their values.
Lecture 15: binary search reading:
Searching and Sorting Arrays
Searching and Sorting 1-D Arrays
Data Structures for Java William H. Ford William R. Topp
Searching and Sorting Arrays
Sum this up for me Let’s write a method to calculate the sum from 1 to some n public static int sum1(int n) { int sum = 0; for (int i = 1; i
Running Time Exercises
Podcast Ch22a Title: Array-based Binary Trees
Presentation transcript:

Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Sort, Search, and Running Time Analysis Ming Li Department of Computer Science California State University, Fresno Spring 2007

Introduction to Data Structure, Spring 2007 Slide- 2 California State University, Fresno Selection Sort Pass 0: Scan the entire list from arr[0] to arr[4] and identify 20 at index 1 as the smallest element. Exchange 20 with arr[0] = 50, the first element in the list pass = 0 20

Introduction to Data Structure, Spring 2007 Slide- 3 California State University, Fresno Selection Sort Pass 1: Scan the entire list from arr[1] to arr[4] and identify 35 at index 4 as the smallest element. Exchange 20 with arr[0] = 50, the first element in the list pass = 1 50

Introduction to Data Structure, Spring 2007 Slide- 4 California State University, Fresno Selection Sort Pass 2: Scan the entire list from arr[2] to arr[4] and identify 40 at index 2 as the smallest element. No exchange necessary since arr[2] is the smallest pass = 2 35

Introduction to Data Structure, Spring 2007 Slide- 5 California State University, Fresno Selection Sort Pass 3: Scan the entire list from arr[3] to arr[4] and identify 50 at index 4 as the smallest element. Exchange arr[3] and arr[4] pass = 3 35

Introduction to Data Structure, Spring 2007 Slide- 6 California State University, Fresno Selection Sort Finally: The list is sorted End 35

Introduction to Data Structure, Spring 2007 Slide- 7 California State University, Fresno Selection Sort - Algorithm for each pass i from 0 to n-2 { find the smallest element from arr[i] to arr[n-1] -> k; if (k == i) continue; else exchange arr[i] and arr[k]; } end

Introduction to Data Structure, Spring 2007 Slide- 8 California State University, Fresno Selection Sort - Algorithm for each pass i from 0 to n-2 { k=i; for each element j from i+1 to n-1 if (arr[j] < arr[k]) k = j; if (k == i) continue; else exchange arr[i] and arr[k]; } end

Introduction to Data Structure, Spring 2007 Slide- 9 California State University, Fresno Selection Sort - Algorithm for each pass i from 0 to n-2 { k = i; for each element j from i+1 to n-1 if (arr[j] < arr[k]) k = j; if (k == i) continue; else temp = arr[i]; arr[i] = arr[k]; arr[k] = temp; } end

Introduction to Data Structure, Spring 2007 Slide- 10 California State University, Fresno Selection Sort - Code void selectionSort(int arr[], int n) { int smallIndex, pass, j, temp; for (pass = 0; pass < n-1; pass++) { for (j = pass+1; j < n; j++) if (arr[j] < arr[smallIndex]) smallIndex = j; if (smallIndex != pass) { temp = arr[pass]; arr[pass] = arr[smallIndex]; arr[smallIndex] = temp; }

Introduction to Data Structure, Spring 2007 Slide- 11 California State University, Fresno Sequential Search index =seqSearch(arr, 0, 8, 3); 7 Index target = 3 8 match at index = 5 return index 5

Introduction to Data Structure, Spring 2007 Slide- 12 California State University, Fresno Sequential Search Index target = 9 8 no match return index 8 index =seqSearch(arr, 0, 8, 9);

Introduction to Data Structure, Spring 2007 Slide- 13 California State University, Fresno Sequential Search - Algorithm Search algorithms start with a target value and employ some strategy to visit the elements looking for a match. –If target is found, the index of the matching element becomes the return value. –Otherwise, return NONE.

Introduction to Data Structure, Spring 2007 Slide- 14 California State University, Fresno Sequential Search - Algorithm matchIndex = NONE; matchValue = x; for each element i from 0 to n-1 if(arr[i] == x) matchIndex = i; break; return matchIndex;

Introduction to Data Structure, Spring 2007 Slide- 15 California State University, Fresno Sequential Search - Code int seqSearch(const int arr[], int first, int last, int target) { int i = first; while(i != last && arr[i] != target) i++; return i; }

Introduction to Data Structure, Spring 2007 Slide- 16 California State University, Fresno Binary Search - Example For sorted list: If target=mid, then find it. midfirst target last-1last

Introduction to Data Structure, Spring 2007 Slide- 17 California State University, Fresno Binary Search - Example For sorted list: If target<mid, then find between (first, mid). mid firstlast-1last Targe t mid-1

Introduction to Data Structure, Spring 2007 Slide- 18 California State University, Fresno Binary Search - Example For sorted list: If target>mid, then find between (first, mid). mid firstlast-1last Targe t mid+1

Introduction to Data Structure, Spring 2007 Slide- 19 California State University, Fresno Binary Search – target=23 Mid = (0+9)/2 = 4

Introduction to Data Structure, Spring 2007 Slide- 20 California State University, Fresno Binary Search – target=23 Mid = (5+9)/2 = 7

Introduction to Data Structure, Spring 2007 Slide- 21 California State University, Fresno Binary Search – target=23 Mid = (5+7)/2 = 6, got it!

Introduction to Data Structure, Spring 2007 Slide- 22 California State University, Fresno Binary Search – target=4 Mid = (0+9)/2 = 4

Introduction to Data Structure, Spring 2007 Slide- 23 California State University, Fresno Binary Search – target=4 mid = (0+4)/2 = 2

Introduction to Data Structure, Spring 2007 Slide- 24 California State University, Fresno Binary Search – target=4 mid = (0+2)/2 = 1, unsuccessful! Since target = 4 > midvalue = 3, step 4 should search the upper sublist with first = 2 and last =2. However, since first >= last, the target is not in the list and we return index last = 9.

Introduction to Data Structure, Spring 2007 Slide- 25 California State University, Fresno Binary Search - Algorithm int BinarySearch(first, last, x) { if(first >= last) return NONE; Mid = (first+last)/2; If (x == arr[mid]) return mid; else if (x > arr[mid]) return BinarySearch(mid+1, last, target); else return BinarySearch(first, mid-1, target); }

Introduction to Data Structure, Spring 2007 Slide- 26 California State University, Fresno Binary Search - Code Int binSearch(const int arr[], int first, int last, int target) { int mid, int midvalue; int origLast = last; while (first < last){ mid = (first+last)/2;midvalue = arr[mid]; if (target == midvalue)return mid; else if (target < midvalue)last = mid; else first = mid+1; } return origLast; }

Introduction to Data Structure, Spring 2007 Slide- 27 California State University, Fresno Examples Given a sorted list: –Find two numbers such that x+y = c (constant)

Introduction to Data Structure, Spring 2007 Slide- 28 California State University, Fresno Big O Notation Identifies the time efficiency –Q: Does the number of operations depend on the array size? Given array size of n –Each comparison/assignment: O(1) –Selection Sort: O(n 2 ) –Sequential Search: O(n) –Binary Search: O(logn)

Introduction to Data Structure, Spring 2007 Slide- 29 California State University, Fresno Exponential Algorithm Algorithms with running time O(n 2 ) are quadratic. –practical only for relatively small values of n. Whenever n doubles, the running time of the algorithm increases by a factor of 4. Algorithms with running time O(n 3 )are cubic. –efficiency is generally poor; doubling the size of n increases the running time eight-fold.

Introduction to Data Structure, Spring 2007 Slide- 30 California State University, Fresno Exponential Algorithm

Introduction to Data Structure, Spring 2007 Slide- 31 California State University, Fresno Comparison – Big O