MSIS 655 Advanced Business Applications Programming

Slides:



Advertisements
Similar presentations
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Advertisements

CS0007: Introduction to Computer Programming Array Algorithms.
HST 952 Computing for Biomedical Scientists Lecture 9.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
Searching/Sorting Introduction to Computing Science and Programming I.
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Arrays
Searching Chapter 18 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search -Reading p
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
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.
C++ How to Program, 9/e © by Pearson Education, Inc. All Rights Reserved.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 19 Searching, Sorting and Big O
1 Searching and Sorting Linear Search Binary Search.
 2005 Pearson Education, Inc. All rights reserved Searching and Sorting.
 Pearson Education, Inc. All rights reserved Searching and Sorting.
Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
1 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow can put more commonly.
Searching and Sorting Copyright Prentice Hall (with additions / modifications by Evan Korth)
Copyright Prentice Hall Modified by Sana odeh, NYU
Searching Arrays Linear search Binary search small arrays
Chapter 19 Searching and Sorting
Searching and Sorting Searching algorithms with simple arrays
Sort Algorithm.
Using recursion for Searching and Sorting
Chapter 16: Searching, Sorting, and the vector Type
Chapter 9: Sorting and Searching Arrays
16 Searching and Sorting.
19 Searching and Sorting.
Sorting and "Big Oh" ASFA AP Computer Science A SortingBigOh.
Searching and Sorting Algorithms
Growth of Functions & Algorithms
Searching – Linear and Binary Searches
Lecture 14 Searching and Sorting Richard Gesick.
Introduction to Search Algorithms
Recitation 13 Searching and Sorting.
Introduction to complexity
COMP 53 – Week Seven Big O Sorting.
Sorting by Tammy Bailey
Chapter 20 Searching and Sorting
Algorithm Analysis CSE 2011 Winter September 2018.
Teach A level Computing: Algorithms and Data Structures
Divide and Conquer.
Linear and Binary Search
CSC215 Lecture Algorithms.
ITEC 2620M Introduction to Data Structures
MSIS 655 Advanced Business Applications Programming
Lecture 11 Searching and Sorting Richard Gesick.
8/04/2009 Many thanks to David Sun for some of the included slides!
Searching and Sorting 1-D Arrays
25 Searching and Sorting Many slides modified by Prof. L. Lilien (even many without an explicit message indicating an update). Slides added or modified.
Search,Sort,Recursion.
24 Searching and Sorting.
Principles of Computing – UFCFA3-30-1
Sorting "There's nothing 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 Hat, Harry Potter.
Chapter 4.
Search,Sort,Recursion.
Chapter 19 Searching, Sorting and Big O
Principles of Computing – UFCFA3-30-1
Presentation transcript:

MSIS 655 Advanced Business Applications Programming Week 12 Searching and Sorting (Ch. 16) In this chapter you will learn: To search for a given value in an array using linear search and binary search. To sort arrays using the iterative selection and insertion sort algorithms. To sort arrays using the recursive merge sort algorithm. To determine the efficiency of searching and sorting algorithms. 12/7/2018 12.1

Introduction Searching Sorting Determining whether a search key is present in data Sorting Places data in order based on one or more sort keys In chapter 17 (Data Structure), the linked Lists (nodes) class and linear search function on the list are explained. Also, class Collections (chapter 19) has a method, binarySearch( list ). Those are class specific functions. What I am trying here is more on the logics to figure out the functions. Examples of searching Looking up a phone number Accessing a Web site Checking a word in the dictionary 12/7/2018

Searching Algorithms Linear search Searches each element sequentially If search key is not present Tests each element When algorithm reaches end of array, informs user search key is not present If search key is present Test each element until it finds a match 12/7/2018

LinearArray.java (Fig. 16.2 p. 788) 12/7/2018

Efficiency of Linear Search Big O Notation Indicates the worst-case run time for an algorithm O(1): Constant O(n): Linear O(n2): Quadratic Sometimes the simplest algorithms perform poorly. Their virtue is that they are easy to program, test and debug. Sometimes more complex algorithms are required to realize maximum performance. 12/7/2018

Binary Search More efficient than linear search Requires elements to be sorted Tests the middle element in an array If it is the search key, algorithm returns Otherwise, if the search key is smaller, eliminates larger half of array If the search key is larger, eliminates smaller half of array Each iteration eliminates half of the remaining elements 12/7/2018

BinaryArray.java (pp. 792-793) 12/7/2018

Efficiency of Binary Search Each comparison halves the size of the remaining array Results in O(log n) Called logarithmic run time 12/7/2018

Sorting Algorithms Sorting data Placing data into some particular order A bank sorts checks by account number Telephone companies sort accounts by name End result is always the same – a sorted array Choice of algorithm affects how you achieve the result and, most importantly, how fast you achieve the result 12/7/2018

Selection Sort Simple, but inefficient sorting algorithm First iteration selects smallest element in array and swaps it with the first element Each iteration selects the smallest remaining unsorted element and swaps it with the next element at the front of the array After i iterations, the smallest i elements will be sorted in the first i elements of the array 12/7/2018

(pp. 798-799) SelectionSort.java 12/7/2018 For each of the i’s in the first for loop, rest of the array is looked through using the second for loop, and the index of the smallest value is chosen as “smallest” (including ith element and forward). Swap (int i, int smallest) will swap the value of the Array elements data[ i ] and data[ smallest ]. – data[ i ] becomes the smallest of the array (I’th and forward). 12/7/2018

Efficiency of Selection Sort Outer for loop iterates over n – 1 elements Inner for loop iterates over remaining elements in the array Results in O(n2) 12/7/2018

Insertion Sort Another simple, but inefficient sorting algorithm First pass takes the second element and inserts it into the correct order with the first element Each iteration takes the next element in the array and inserts it into the sorted elements at the beginning of the array After i iterations, the first i elements of the array are in sorted order 12/7/2018

12/7/2018

Merge Sort Combination of Sort algorithm and recursion To be continued… 12/7/2018

Lab activities (Week 12) Exercises (pp. 814-815) 16.5, 16.6 Advanced: 16.7 12/7/2018