0% found this document useful (0 votes)
64 views23 pages

Searching: Lakshman Jayaratne

This document discusses arrays and searching algorithms. It defines an array as an indexed list of elements of the same type. It covers initializing and declaring arrays, and performing simple processing like finding the largest element. It then discusses different searching algorithms like linear search and binary search. Linear search compares the search key to each element in turn, while binary search takes advantage of a sorted array to narrow the search range. The document provides pseudocode and C++ code examples to demonstrate implementing linear and binary search. It analyzes the efficiency of each approach. Finally, it introduces paired and parallel arrays.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views23 pages

Searching: Lakshman Jayaratne

This document discusses arrays and searching algorithms. It defines an array as an indexed list of elements of the same type. It covers initializing and declaring arrays, and performing simple processing like finding the largest element. It then discusses different searching algorithms like linear search and binary search. Linear search compares the search key to each element in turn, while binary search takes advantage of a sorted array to narrow the search range. The document provides pseudocode and C++ code examples to demonstrate implementing linear and binary search. It analyzes the efficiency of each approach. Finally, it introduces paired and parallel arrays.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 23

Searching

Lakshman Jayaratne

Topics Covered
Define and understand an ARRAY Initialising an array Searching an array

- linear search - binary search


Paired arrays and parallel arrays

Definition of Array
An array is an indexed/subscripted list of elements of the same type (a homogeneous collection). 4
a[0]

21
a[1]

5
a[2]

3
a[3]

16 25 11 23
a[4] a[5] a[6] a[7]

2
a[8]

80
a[9]

Declaration: int a[10] Declare ten memory cells to store ten integers.

Usage: a[2] = 5; i =2; a[i] = 5;

Put integer 5 to the third memory cell.


3

Declaration and Usage of an Array


The type of the array
#include <iostream> void main() { int num[10]; int total;

The name of the array


// Declare an array

num[0] int num[1] int num[2] int num[3] int num[4] int num[5] int num[6] int num[7] int num[8] int num[9] int
4

The size of the array Index /subscript

cout << "Please input ten integers:\n"; for ( int i=0; i<= 9; i++){ cin >> num[i]; total = total + num[i]; } // end for

cout << "\n The total of the ten integers is " << total; } // end main

Initialising an array: Enumerating


2 5 9 10 4 2 7 8 5

int scores[ ] = {2, 5, 9, 10, 4, 2, 7, 8, 5};

Compiler sets the size to match the number of elements in the list

Initialising an Array
? ? ? ? ? ? ? ? ?
scores int scores[9]; for (int i = 0; i <= 8; i++) { scores[i] = 0; } // end for

0 0 0 0 0 0 0 0 0
scores 6

Simple Processing: find largest


2 5 9 10 4 2 7 8 5
scores
void main( ) { int scores[ ] = {2, 5, 9, 10, 4, 2, 7, 8, 5}; int largest = 0; for (int i = 0; i <= 8; i++) { if (scores[i] > largest) { largest = scores[i]; } //end if }//end for } // end main

Searching Problem
Given a collection of data, find a particular item in the collection. Store the data in an array Compare the search key with each element in the array. If there is a match, return the position of the element; Otherwise, the user can be informed of this fact. We call this algorithm Linear

Linear Search
a[0] a[1] a[2] a[3] a[n]

?
LookFor

If there is a match, record the position and then stop.

Linear Search
a[0] a[1] a[2] a[3] a[i] a[n-1] a[n]

?
LookFor

If there is a match, record the position and then stop.

10

Linear Search
a[0] a[1] a[2] a[3] a[i] a[n-1] a[n]

?
LookFor

If there is a match, record the position and then stop.

11

Linear Search
a[0] a[1] a[2] a[3] a[i] a[n-1] a[n]

?
LookFor

If there isn't a match, report the fact.

12

Pseudocode for Linear Search


linearSearch set last to max_num_elements set found to false set index to 0; DOWHILE ( (index<=last) AND (NOT found )) IF ( array(index) = lookFor ) found = true ELSE index = index +1 ENDIF ENDDO IF found Print index ELSE Print "value not found' ENDIF END

13

C++ Code for Linear Search


int linearSearch(int scores[ ], int last, int lookFor) { bool found = false; int index = 0; while ( (index<=last) && !found ) { if ( scores[index] == lookFor ) found = true; else index++; } // end while if (found) return index; else return -1; } // end linearSearch
14

Invoke LinearSearch Function


#include <iostream> int linearSearch(int[ ], int, int); void main(){ int scores[10]; int position; for (int i =0; i<=9; i++) cin >> scores[i]; position = linearSearch(scores,9,10); if ( position != -1 ) cout << " Value found at" << position; else cout << "Not found!";

}// end main

15

Efficiency of Linear Search


0 1 2 3 4 5 6 7 8

9 10 4

Efficiency of Linear Search


- 7 comparisons for 7 - 4 comparisons for 10. - 9 comparisons for looking for 3. Generally, the worst case of linear search requires n comparisons for a size n array.
16

Ordered Arrays and Binary Search


Given a sequence of numbers sorted in ascending order, find a particular item in the sequence.
a[0] a[1] a[2] a[3] a[i] a[n-1] a[n]

Low index

High index
17

Binary Search : Example 1


0 1 2 3 4 5 6 7 8

2
0

2
1

4
2

5
3

5
4

7
5

8
6

9 10
7 8

Look for 7

2
0

2
1

4
2

5
3

5
4

7
5

8
6

9 10
7 8

9 10
18

Binary Search: Example 2


0 1 2 3 4 5 6 7 8

2
0

2
1

4
2

5
3

5
4

7
5

8
6

9 10
7 8

Look for 3

Low index

high index

2
Low index

2
1

4
2

5
3

5
high index

7
5

8
6

9 10
7 8

5
high index

9 10
19

Low index

Binary Search for Ascending Array


int binarySearch(int scores[ ], int last, int lookFor) { bool found = false; int lowIndex = 0; int highIndex = last; int index;

Calculate middle element

while ((lowIndex <= highIndex) && (!found)) { lowindex highindex index = (lowIndex + highIndex) / 2; if (scores[index] == lookFor) found = true; lowindex highindex else if (lookFor < scores[index]) highIndex = index - 1; lowindex highindex else lowIndex = index + 1; } // end while if (found) return index; else return -1; } // end binarySearch
20

Efficiency of Searching Algorithms


Test Data: 2, 2, 4, 5, 5, 7, 8, 9,10 Efficiency of Linear Search
- 6 comparisons for 7 - 9 comparisons for 10. - 9 comparisons for looking for 3. - the worst case is 9 comparisons.

Efficiency of Binary Search


- 3 comparisons for 7 - 3 comparisons for 10. - 3 comparisons for looking for 3. - the worst case is 3 comparisons. Note: Binary Search works only for ordered array.
21

Paired Arrays and Parallel Arrays


Who got mark ten?
name studentID scores Jones 13456955 2 Smith Cook Twit Argy Lee 13509321 13048976 13892311 13001032 10389201 5 9 10 4 2 7 8 5 If (index > -1) { cout << name[index] << "got ten."; Else cout << "No one got ten.";
22

Parallel arrays: name[] studentID[] scores[] Index = linearSearch(scores, last, 10);

Adams 13002134 Chang 99140394

Walsh 99382103

The End of the Lecture!

23

You might also like