CS 1400 March 30, 2007 Chapter 8 Searching and Sorting.

Slides:



Advertisements
Similar presentations
Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
Advertisements

Chapter 9: Searching, Sorting, and Algorithm Analysis
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Starting Out with C++, 3 rd Edition 1 Chapter 8 – Searching and Sorting Arrays.
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
CS Oct 2006 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
Sorting and Searching. Problem Read in a parameter value n, then read in a set of n numbers. Print the numbers in their original order. Sort the numbers.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
Algorithm Efficiency and Sorting
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
CS 1400 Apr 6, 2007 Chapters 9, 10 C-strings and Pointers.
Searching Arrays Linear search Binary search small arrays
CS 1400 Chapters 9, 10 C-strings and Pointers. A few C-string library functions… #include int strcmp (char a[ ], char b[ ]); returns 0 if string a is.
Searching and Sorting Arrays
1 Sorting/Searching and File I/O Sorting Searching Reading for this lecture: L&L
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 9 Searching.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
Chapter 8 ARRAYS Continued
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
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.
Lecture 5 Searching and Sorting Richard Gesick. The focus Searching - examining the contents of the array to see if an element exists within the array.
Searching and Sorting Topics Sequential Search on an Unordered File
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
Computer Science Searching & Sorting.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 9 Searching Arrays.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Chapter Searching and Sorting Arrays 8. Introduction to Search Algorithms 8.1.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 19 Thanks for Lecture Slides:
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Searching Course Lecture Slides 28 May 2010 “ Some things Man was never.
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.
Chapter 5 Searching and Sorting. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine the linear search and binary.
Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):
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.
Sorting & Searching Review. Selection Sort 1. Find the smallest element 2. Move to the front of the array (swap with front) 3. Repeat Steps 1&2, but ignoring.
1 Principles of Computer Science I Honors Section Note Set 5 CSE 1341.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
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 Arrays Linear search Binary search small arrays
Searching and Sorting Arrays
The Bubble Sort Mr. Dave Clausen La Cañada High School
Searching and Sorting Algorithms
Searching and Sorting Arrays
Recitation 13 Searching and Sorting.
Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.
Introduction to Programming
Searching and Sorting Arrays
Standard Version of Starting Out with C++, 4th Edition
Searching and Sorting 1-D Arrays
Search,Sort,Recursion.
Search,Sort,Recursion.
Searching and Sorting Arrays
Module 8 – Searching & Sorting Algorithms
Presentation transcript:

CS 1400 March 30, 2007 Chapter 8 Searching and Sorting

Linear Search… int LinearSearch (int list[], int size, int value) { bool found = false; int position = -1; for (int n=0; n<size && !found; n++) { if (list[n] == value) { position = n; found = true; } return position; }

Linear Search Advantages –easy to write –easy to understand –doesn’t require array elements to be in any particular order Disadvantages –very inefficient for large arrays (average search requires N/2 array comparisons)

Is there a better algorithm for searching? Binary SearchWhen array elements are in order, a Binary Search can be performed How do you search a phonebook for a given person’s listing?

Binary Search Pseudocode (assumes search item is in array) found = false position = -1 while search_item has not yet been found; set middle to halfway between the first and last index if array[middle] equals search_item item is at middle position!! else if array[middle] is less than search_item set first index to middle+1 else set last index to middle-1 end if end while

Binary Search function (assumes search item is in array) int BinarySearch (int array[ ], int first, int last, int search_item) { int middle; while (true) {middle = (first + last) / 2; if (array[middle] == search_item) return middle; else if (array[middle] < search_item) first = middle+1; else last = middle-1; }

Binary Search example: int eaglenums[10]; float grades[10];

cout << BinarySearch(eaglenums, 0, 9, 5122); cout << BinarySearch(eaglenums, 0, 9, 1456); cout << BinarySearch(eaglenums, 0, 9, 4556); int position = BinarySearch(eaglenums, 0, 9, 4556); cout << grades[position]; cout << BinarySearch(grades, 0, 9, 93.1); //???? What would be output?

Binary Search (1st refinement) Let’s move the return to the end of the function: int BinarySearch (int array[ ], int first, int last, int search_item) { bool found = false; int position = -1, middle; while (!found) {middle = (first + last) / 2; if (array[middle] == search_item) {position = middle; found = true; } else if (array[middle] < search_item) first = middle+1; else last = middle-1; } return position; }

Binary Search (2nd refinement) Let’s return -1 if the search_item isn’t found… int BinarySearch (int array[ ], int first, int last, int search_item) { bool found = false; int position = -1, middle; while (!found && first <= last) {middle = (first + last) / 2; if (array[middle] == search_item) {position = middle; found = true; } else if (array[middle] < search_item) first = middle+1; else last = middle-1; } return position; }

Binary Search (3rd refinement) Let’s simplify the parameters… int BinarySearch (int array[ ], int size, int search_item) { int first = 0, last = size-1; bool found = false; int position = -1, middle; while (!found && first <= last) {middle = (first + last) / 2; if (array[middle] == search_item) {position = middle; found = true; } else if (array[middle] < search_item) first = middle+1; else last = middle-1; } return position; }

Binary Search Advantages –very efficient (worst-cast search requires log 2 (N) array comparisons) Disadvantages –a bit more difficult to write and understand –requires the array to be ordered!

An overloaded BinarySearch() int BinarySearch (char array[ ][30], int size, char search_item[ ]) { int first = 0, last = size-1; bool found = false; int position = -1, middle; while (!found && first <= last) {middle = (first + last) / 2; if (strcmp(array[middle], search_item) == 0) {position = middle; found = true; } else if (strcmp(array[middle], search_item) < 0) first = middle+1; else last = middle-1; } return position; }

So, how does an array get ordered? void BubbleSort (int array[ ], int size); void BubbleSort (float array[ ], int size); void BubbleSort (char array[ ][30], int size);

Bubble sorting… Pseudocode: Repeatedly compare adjacent cells until array is in order a) if they are in order, do nothing b) if they are out of order, swap them

1 st Refinement an array is in order when no further swaps can be made: Set done to false While (not done) Set done to true Compare all adjacent cell pairs b) if they are out of order, swap them and set done to false end while

2 nd Refinement… bool done = false; while (!done) { done = true; Compare all adjacent cell pairs a) if they are out of order, swap them and set done to false }

3 rd Refinement If there are N cells in an array, there are N-1 adjacent pairs bool done = false; while (!done) { done = true; for (int n=0; n<N-1; n++) if (array[n] > array[n+1]) { Swap (array, n, n+1); done = false; }

As a function… void BubbleSort (int array[ ], int size) { bool done = false; while (!done) { done = true; for (int n=0; n<size-1; n++) if (array[n] > array[n+1]) { Swap (array, n, n+1); done = false; }

Swap() void Swap (int array[ ], int j, int k) { int temp; temp = array[j]; array[j] = array[k]; array[k] = temp; }

slight improvement… void BubbleSort (int array[ ], int size) { bool done = false; while (!done) { done = true; for (int n=0; n<size-1-n; n++) if (array[n] > array[n+1]) { Swap (array, n, n+1); done = false; } Each pass leaves the bottom value in the correct position

Selection Sort Advantages –somewhat more efficient than a Bubble Sort for large arrays Disadvantages –slightly more difficult to program and understand

Selection Sort pseudocode For (int n=0; n<N-1; n++) find the smallest value beginning at position n swap this value with element n

n= smallest First Pass n= smallest Second Pass

n= smallest Third Pass n= smallest Forth Pass

n= smallest Fifth Pass n= smallest Sixth Pass

n= smallest Seventh Pass Since there are 8 elements in this array, only 8-1 passes are required.

Selection Sort void SelectionSort (int array[ ], int size) { int smallest_position; for (int n=0; n<size-1; n++) { // find the smallest in array beginning at position n smallest_position = FindSmallest (array, n, size); // swap the elements at positions n and smallest_position Swap (array, n, smallest_position); }

This is the same swapping function used for BubbleSort() void Swap (int array[ ], int j, int k) { int temp; temp = array[j]; array[j] = array[k]; array[k] = temp; }

int FindSmallest (int array[ ], int start, int size) { int position = start; int smallest = array[start];// first assumption for (int k=start+1; k<size; k++) if (array[k] < smallest) { smallest = array[k];// change your mind position = k;// and remember pos. } return position; } FindSmallest()

Sorting parallel arrays… Consider parallel arrays for student eagle numbers and grades: int eagles[100]; float grades[100]; …… eagles grades

Anytime we move an eagle number, we need to move the corresponding grade along with it – so that matching eagle numbers and grades are always in the same row… …… eagles grades swap.. matching swap…

SelectionSort with parallel arrays… void SelectionSort (int array[ ], float array2[ ], int size) { int smallest_position; for (int n=0; n<size-1; n++) { smallest_position = FindSmallest (array1, n, size); Swap (array1, n, smallest_position); Swap (array2, n, smallest_position); } Naturally, Swap() would need to be overloaded to also handle float arrays…