1 Lecture 23:Applications of Arrays Introduction to Computer Science Spring 2006.

Slides:



Advertisements
Similar presentations
Zabin Visram Room CS115 CS126 Searching
Advertisements

Chapter 10: Applications of Arrays and Strings J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second.
Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
CHAPTER 10 ARRAYS II Applications and Extensions.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Searching and Sorting Algorithms Based on D. S
Chapter 19: Searching and Sorting Algorithms
Data Structures & Algorithms CHAPTER 3 Sorting Ms. Manal Al-Asmari.
1 Sorting II: Bubble Sort and Selection Sort CSC326 Information Structure Spring 2009.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 10 Applications of Arrays.
Problem Solving #6: Search & Sort ICS Outline Review of Key Topics Review of Key Topics Problem 1: Recursive Binary Search … Problem 1: Recursive.
Searching Algorithms. Lecture Objectives Learn how to implement the sequential search algorithm Learn how to implement the binary search algorithm To.
1 Lecture 25:Pointers Introduction to Computer Science Spring 2006.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
Sorting Algorithms: Selection, Insertion and Bubble.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
Searching Arrays Linear search Binary search small arrays
1 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006.
1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.
Chapter 8 ARRAYS Continued
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
Chapter 16: Searching, Sorting, and the vector Type.
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.
Data Structures and Algorithms.
Data Structures Using C++1 Search Algorithms Sequential Search (Linear Search) Binary Search.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Applications of Arrays (Searching and Sorting) and Strings
Chapter 19: Searching and Sorting Algorithms
Lecture 12. Searching Algorithms and its analysis 1.
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
Computer Science Searching & Sorting.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
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.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
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.
Lecture 6 Sorting Algorithms: Bubble, Selection, and Insertion.
Chapter 14: Searching and Sorting
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 10: Applications of Arrays (Searching and Sorting) and the vector Type.
1 C++ Plus Data Structures Nell Dale Chapter 10 Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential 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 Algorithms: Selection, Insertion and Bubble.
Data Structures Using C++1 Chapter 10 Sorting Algorithms.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Searching When we maintain a collection of data,
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.
M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.
Chapter 10: Class Vector and String, and Enumeration Types Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
CHAPTER 10 ARRAYS II Applications and Extensions.
 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.
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 and Sorting Arrays
Chapter 16: Searching, Sorting, and the vector Type
Introduction to Search Algorithms
Sorting Algorithms: Selection, Insertion and Bubble
Search Algorithms Sequential Search (Linear Search) Binary Search
Searching and Sorting Arrays
Searching and Sorting Arrays
Applications of Arrays
Presentation transcript:

1 Lecture 23:Applications of Arrays Introduction to Computer Science Spring 2006

2 Sequential Search Sequential search: search a list for an item Compare search item with other elements until either Item is found List has no more elements left Average number of comparisons made by the sequential search equals half the list size Good only for very short lists

3 int seqSearch(const int list[], int listLength, int searchItem) { int loc; for (loc = 0; loc < listLength; loc++) if (list[loc] == searchItem) return loc; return -1; }

4 Sorting a List: Bubble Sort Suppose list[0]...list[n - 1] is a list of n elements, indexed 0 to n – 1 Bubble sort algorithm: In a series of n - 1 iterations, compare successive elements, list[index] and list[index + 1] If list[index] is greater than list[index + 1], then swap them

5

6

7 void bubbleSort(int list[], int length) { int temp; int i, j; for (i = 0; i < length - 1; i++) { for (j = 0; j < length i; j++) if (list[j] > list[j + 1]) { temp = list[j]; list[j] = list[j + 1]; list[j + 1] = temp; } Bubble Sort Algorithm for i = 0 to n-1 do for j = 0 to n - i - 1 do if list[j] > list[j+1] then Swap(list[j], list[j+1])

8 Sorting a List: Selection Sort Selection sort: rearrange list by selecting an element and moving it to its proper position Find the smallest (or largest) element and move it to the beginning (end) of the list On successive passes, locate the smallest item in the list starting from the next element

9 void selectionSort(int list[], int length) { int index; int smallestIndex; int minIndex; int temp; for (index = 0; index < length - 1; index++) { //Step a smallestIndex = index; for (minIndex = index + 1; minIndex < length; minIndex++) if (list[minIndex] < list[smallestIndex]) smallestIndex = minIndex; //Step b temp = list[smallestIndex]; list[smallestIndex] = list[index]; list[index] = temp; } Selection Sort Algorithm for (index = 0; index < length - 1; index++) { a.Find the location, smallestIndex, of the smallest element in list[index]…list[length-1]. a.Swap the smallest element with list[index]. That is, swap list[smallestIndex] with list[index] }

10 Sequential Search on an Ordered List General form of sequential search algorithm on a sorted list:

11 int seqOrderedSearch(const int list[], int listLength, int searchItem) { int loc;//Line 1 bool found = false;//Line 2 for (loc = 0; loc < listLength; loc++)//Line 3 if (list[loc] >= searchItem)//Line 4 { found = true;//Line 5 break;//Line 6 } if (found)//Line 7 if (list[loc] == searchItem)//Line 8 return loc;//Line 9 else//Line 10 return -1;//Line 11 else//Line 12 return -1;//Line 13 } Sequential Search on an Ordered List

12 Binary Search Binary search can be applied to sorted lists Uses the “ divide and conquer ” technique Compare search item to middle element mid= (first+last)/2 first is the starting index of the search list last is the ending index of the search list mid is the index of the middle element of the search list If search item is less than middle element, restrict the search to the lower half of the list Otherwise search the upper half of the list

13 Search 75 in the following list: search list

14 int binarySearch(const int list[], int listLength, int searchItem) { int first = 0; int last = listLength - 1; int mid; bool found = false; while(first <= last && !found) { mid = (first + last) / 2; if(list[mid] == searchItem) found = true; else if(list[mid] > searchItem) last = mid - 1; else first = mid + 1; } if(found) return mid; else return -1; } Binary Search

15 Binary Search (continued) Every iteration cuts size of search list in half If list L has 1000 items At most 11 iterations needed to determine if an item x is in list Every iteration makes 2 key (item) comparisons Binary search makes at most 22 key comparisons to determine if x is in L Sequential search makes 500 key comparisons (average) to if x is in L for the same size list

16 End of lecture 23 Thank you!