Searching Arrays. COMP104 Array Sorting & Searching / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and save its.

Slides:



Advertisements
Similar presentations
Bubble Sort Algorithm 1.Initialize the size of the list to be sorted to be the actual size of the list. 2.Loop through the list until no element needs.
Advertisements

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Topic 24 sorting and searching arrays "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."
Visual C++ Programming: Concepts and Projects
CPS120: Introduction to Computer Science Searching and Sorting.
Starting Out with C++, 3 rd Edition 1 Chapter 8 – Searching and Sorting Arrays.
Programming Searching Arrays. COMP102 Prog. Fundamentals: Searching Arrays/ Slide 2 Copyright © 2000 by Broks/Cole Publishing Company A division of International.
Sorting Algorithms and Average Case Time Complexity
Searching Arrays. COMP104 Lecture 22 / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and return its index if the.
Programming Sorting Arrays. COMP102 Prog. Fundamentals. Sorting I/ Slide 2 Sorting l To arrange a set of items in sequence. l It is estimated that 25~50%
Overview Sort – placing data in an array in some order (usually decreasing or increasing order) Bubble Sort More efficient bubble sort.
C++ Plus Data Structures
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.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Sorting and Searching Algorithms Week 11 DSA. Recap etc. Arrays are lists of data 1-D, 2-D etc. Lists associated with searching and sorting Other structures.
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Arrays
Ordered Arrays An array is ordered if the elements are in ascending or descending order. The array may be ordered numerically or alphabetically (which.
Programming Sorting Arrays. COMP104 Lecture 25 / Slide 2 Sorting l To arrange a set of items in sequence. l It was estimated that 25~50% of all computing.
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.
1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
Fall 2013 Instructor: Reza Entezari-Maleki Sharif University of Technology 1 Fundamentals of Programming Session 17 These.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
COMP102 Lab 131 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
Chapter 16: Searching, Sorting, and the vector Type.
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.
1 Lecture 5: Part 1 Searching Arrays Searching Arrays: Linear Search and Binary Search Search array for a key value Linear search  Compare each.
Starting Out with C++, 3 rd Edition 1 Searching an Arrays.
COMP102 Lab 121 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 19 Thanks for Lecture Slides:
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Searching Course Lecture Slides 28 May 2010 “ Some things Man was never.
Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
LAB#7. Insertion sort In the outer for loop, out starts at 1 and moves right. It marks the leftmost unsorted data. In the inner while loop, in starts.
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.
Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use.
Sorting. 2 contents 3 kinds of sorting methods – Selection, exchange, and insertion O(n 2 ) sorts – VERY inefficient, but OK for ≈ 10 elements – Simple.
1 Lecture 8 Arrays Part II Sorting Arrays Sorting data  Important computing application  Virtually every organization must sort some data Massive.
1 C++ Plus Data Structures Nell Dale Chapter 10 Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
LAB#6. 2 Overview Before we go to our lesson we must know about : 1. data structure. 2.Algorithms. data structure is an arrangement of data in a computer.
CSCI 51 Introduction to Programming March 12, 2009.
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 and Searching. Selection Sort  “Search-and-Swap” algorithm 1) Find the smallest element in the array and exchange it with a[0], the first element.
SORTING & SEARCHING - Bubble SortBubble Sort - Insertion SortInsertion Sort - Quick SortQuick Sort - Binary SearchBinary Search 2 nd June 2005 Thursday.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
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.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
CS 116 Object Oriented Programming II Lecture 4 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
SORTING Sorting is storage of data in some order, it can be in ascending or descending order. The term Sorting comes along-with the term Searching. There.
Chapter 16: Searching, Sorting, and the vector Type.
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
Chapter 16: Searching, Sorting, and the vector Type
Chapter 9: Sorting and Searching Arrays
Algorithm design and Analysis
Introduction to Programming
Chapter 8 – Searching and Sorting Arrays
Presentation transcript:

Searching Arrays

COMP104 Array Sorting & Searching / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and save its index if the value is found. Otherwise, set index to -1. * Algorithm: Assume value not in array and set index = -1 Start with the first array element (index 0) WHILE(more elements in array){ If value found at current index, save index Try next element (increment index) }

COMP104 Array Sorting & Searching / Slide 3 Unordered Linear Search void main() { int A[8] = {10, 7, 9, 1, 17, 30, 5, 6}; int x, n, index; cout << "Enter search element: "; cin >> x; index = -1; for(n=0; n<8; n++) if(A[n]==x) index = n; if(index==-1) cout << "Not found!!" << endl; else cout << "Found at: " << index << endl; }

COMP104 Array Sorting & Searching / Slide 4 Ordered Linear Search * Search an ordered array of integers for a value and save its index if the value is found. Otherwise, set index to -1. * The key difference in design is that this array is ordered. * If we perform a linear search, and find that we have already passed where the element might be found, we can quit early.

COMP104 Array Sorting & Searching / Slide 5 Ordered Linear Search void main() { int A[8] = { 1, 5, 6, 7, 9, 10, 17, 30}; int x, n, index; cout << "Enter search element: "; cin >> x; index = -1; for(n=0; n<8 && A[n]<=x; n++) if(A[n] == x) index = n; if(index==-1) cout << "Not found!!" << endl; else cout << "Found at: " << index << endl; }

COMP104 Array Sorting & Searching / Slide 6 Binary Search * Search an ordered array of integers for a value and save its index if the value is found. Otherwise, set index to -1. * Binary search takes advantage of the sorting, to search the array efficiently.

COMP104 Array Sorting & Searching / Slide 7 Binary Search * Binary search is based on a divide-and- conquer strategy which works as follows: n Start by looking at the middle element of the array  1. If the value it holds is lower than the search element, eliminate the first half of the array.  2. If the value it holds is higher than the search element, eliminate the second half of the array. n Repeat this process until the element is found, or until the entire array is eliminated.

COMP104 Array Sorting & Searching / Slide 8 Binary Search void main() { int A[8] = {1, 5, 6, 7, 9, 10, 17, 30}; int x, lower, middle, upper; cout << "Enter search element: "; cin >> x; lower = 0; upper = 7;// array size - 1 middle = (lower + upper)/2; while(A[middle]!=x && lower<upper){ if(x<A[middle]) upper = middle - 1; else lower = middle + 1; middle = (lower + upper)/2; } if(A[middle]!=x) cout << "Not found!!" << endl; else cout << "Found at: " << middle << endl; }

Sorting Arrays

COMP104 Array Sorting & Searching / Slide 10 Sorting * To arrange a set of items in sequence. * About 25~50% of all computing power is used in sorting. * Possible reasons: n Many applications require sorting n Many applications perform sorting when they don't have to n Many applications use inefficient sorting algorithms

COMP104 Array Sorting & Searching / Slide 11 Sorting Applications * List of student ID names and numbers in a table (sorted by name) * List of scores before letter grade assignment (sorted by students' scores) * List of horses after a race (sorted by the finishing times) * To prepare an originally unsorted array for ordered binary searching

COMP104 Array Sorting & Searching / Slide 12 Some Sorting Methods * Selection sort * Bubble sort * Shell sort (a simple but faster sorting method) * Quick sort (a much faster sorting method for most applications)

COMP104 Array Sorting & Searching / Slide 13 Selection Sort * Selection sort performs sorting by repeatedly putting the largest element to the end of the unsorted part of the array until the whole array is sorted. * It is similar to the way that many people do their sorting.

COMP104 Array Sorting & Searching / Slide 14 Selection Sort * Algorithm 1. Define the unsorted part of the array as the entire array 2. While the unsorted part of the array has more than one element:  Find its largest element  Swap with last element  Reduce unsorted part of the array by 1

COMP104 Array Sorting & Searching / Slide 15 Selection Sort // Selection Sort of integers into ascending order int main(){ const int size = 9; int A[size] = { 10, 7, 9, 1, 9, 17, 30, 5, 6}; int i, rightmost, temp, max_index; for(rightmost=size-1; rightmost>0; rightmost--){ // find largest item max_index = 0; for(i=1; i<=rightmost; i++) if(A[i] > A[max_index]) max_index = i; // swap largest item with last item temp = A[max_index]; A[max_index] = A[rightmost]; A[rightmost] = temp; } cout << "The sorted array: "; for(i=0; i<size; i++) cout << A[i] << " "; cout << endl; return 0; }

COMP104 Array Sorting & Searching / Slide 16 Bubble Sort * Bubble sort examines the array from start to finish, comparing elements as it goes. * Any time it finds a larger element before a smaller element, it swaps the two. * In this way, the larger elements are passed towards the end. * The largest element of the array therefore "bubbles" to the end of the array. * It then repeats the process for the unsorted part of the array until the whole array is sorted.

COMP104 Array Sorting & Searching / Slide 17 Bubble Sort * Bubble sort works on the same general principle as shaking a soft drink bottle. * Right after shaking, the contents are a mixture of bubbles and soft drink, distributed randomly. * Because bubbles are lighter than the soft drink, they rise to the surface, displacing the soft drink downwards. * This is how bubble sort got its name, because the smaller elements "float" to the top, while the larger elements "sink" to the bottom.

COMP104 Array Sorting & Searching / Slide 18 Bubble Sort * Algorithm n Define the unsorted part of the array to be the entire array n While the unsorted part of the array has more than one element: 1. For every element in the unsorted part, swap with the next neighbor if it is larger than the neighbor 2. Reduce the unprocessed part of the array by 1

COMP104 Array Sorting & Searching / Slide 19 Bubble Sort // Bubble Sort of integers into ascending order int main(){ const int size = 9; int A[size] = { 10, 7, 9, 1, 9, 17, 30, 5, 6}; int i, rightmost, temp; for(rightmost=size-1; rightmost>0; rightmost--){ for(i=0; i<rightmost; i++){ if(A[i] > A[i+1] ){ // swap if greater temp = A[i]; A[i] = A[i+1]; A[i+1] = temp; } cout << "The sorted array: "; for(i=0; i<size; i++) cout << A[i] << " "; cout << endl; return 0; }