Top Banner
DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING
22

DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.

Jan 01, 2016

Download

Documents

Rolf Walters
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.

DATA STRUCTURE & ALGORITHMS(BCS 1223)CHAPTER 8 : SEARCHING

Page 2: DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.

OVERVIEW

• At the end of this chapter you’ll be learnt about:– Primary key– Secondary key– Sequential access– Random– Binary search– Linear/Sequential Search

Page 3: DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.

Primary Key vs Secondary Key

• A primary key is a key or field item that is unique for each record. It is a unique identifier, such as an IC number, matric number, telephone number, and staff number. In the database, it must always have one and only primary key.

• A choice of primary key, typically depends on the preference of the administrator. It is possible to change the primary key for a given database when the specific needs of the users change

Page 4: DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.

Primary Key vs Secondary Key

• A secondary key also known as an alternate key, or in SQL may also referred to as a candidate key, is a data field or location that is used for data searches and retrieval

Page 5: DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.

File Access

• Refers to the process of retrieving data from a file. There are two types of file access:– Random Access– Sequential Access

• To understand these types of access, u first need to understand some concepts related to how data is organized within a file

Page 6: DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.

File Access: Sequential

• The term file organization refers to the way data is stored in a file

• The files we have used, and will continue to use, all have a sequential organization. This means that the characters within the file are stored in a sequential manner, one after another

• To being sequentially organized, we have also read each file, after it has been opened, in a sequential manner. That is, we have accessed each character sequentially, one after another. This type of access is referred to sequential access

Page 7: DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.

File Access: Random Access

• In random access, any character in the opened file can be read directly, without first having to sequentially read all characters stored ahead of it

• To provide random access to files, each ifstream object automatically creates a file position marker. This marker is a long integer that represents an offset from the beginning of each file & keep track of where the next character is to be read from or written to

Page 8: DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.

What is searching?

• Searching is the operation which finds the location LOC in memory of some given ITEM of information or sends some message that ITEM does not belong to S

• The searching is said to be successful or unsuccessful according to whether ITEM does or does not belong to S

• The searching algorithm that is used depends mainly on the type of data structure that is used to maintain S in memory

Page 9: DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.

What is searching?

• Data modification refers to the operations of inserting, deleting and updating

• Data modification will mainly refer to inserting & deleting

• These operations are closely related to searching, since usually one must search for the location of the ITEM to be deleted or one must search for the proper place to insert ITEM in the table

• It also requires a certain amount of execution time, which also depends mainly on the type of data structure that is used

Page 10: DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.

What is searching?

• Sorted array– Here, one can use a binary search to find the

location LOC of a given ITEM in time O (logn)– On the other hand, inserting & deleting are

very slow, since, on the average n/2 = O(n) elements must be moved for a given insertion or deletion

– Thus a sorted array would likely be used when there is a great deal of searching but only very little data modification

Page 11: DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.

What is searching?

• Linked list– Here, one can only perform a linear search to

find the location LOC of a given ITEM, and the search may be very slow, possibly requiring time O(n).

– On the other hand, inserting and deleting requires only a few pointers to be changed

– Thus, a linked list would be used when there is a great deal of data modification, as in word processing

Page 12: DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.

What is searching?

• Binary Search Tree– This data structure combines the advantages of the sorted

array and the linked list– Searching is reduced to search only a certain path P in the

tree T, which on the average requires only O(log n) comparisons

– Furthermore, the tree T is maintained in memory by a linked representation, so only certain pointers need to be changed after the location of the insertion or deletion is found

– The main drawback is that the tree may be very unbalanced, so that length of a path P may be O(n) rather than O(log n). This will reduce the searching to approximately a linear search

Page 13: DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.

Linear Search

• Each item in the list is examined in the order in which it occurs until the desired item is found or the end of the list is reached

• This is an analogous of to looking at every name in the phone directory, beginning with Ali, Azahari, until u find the one u want or until u reach Zahara, Zura.

• Obviously, this is not an efficient way to search a long alphabetized list

Page 14: DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.

Linear Search (cont…)

It has 2 advantages: The algorithm is simple The list need not be in any particular order

The search begins at the first item & continues sequentially, item by item, through the list. The pseudocode for a function performing a linear search is:

For all the items in the listCompare the item with the desired itemIf the item was found,

Return the index value of the current itemEndIf

EndForReturn – 1 because the item was not found

Page 15: DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.

Linear Search (cont…)

• The function linearSearch() illistrates this procedure as C++ function:// this function returns the location of key in the list

// a -1 is returned if the value is not found

int linearSearch (int list[], int size, int key)

{

int i;

for ( i = 0; i < size; i++)

{

if ( list [i] == key)

return i;

}

return -1;

}

Page 16: DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.

Linear Search (cont…)

• In reviewing the linearSearch(), notice that the for loop is simply used to access each element in the list, from first element to last, until a match is found with the desired item

• If the desired item is located, the index value of the current item is returned, which causes the loop to terminate; otherwise the search continues until the end of the list is encountered

Page 17: DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.

Binary Search

• In binary search, the list must be in sorted order. Starting with an ordered list, the desired item is first compared to the element in the middle of the list (for lists with an even number of elements, either of the 2 middle elements can be used)

• 3 possibilities present themselves once the comparison is made:– The desired item may be equal to the middle element– It may be greater than the middle element– It may be less than the middle element

Page 18: DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.

Binary Search (cont…)

In the 1st case, the search has been successful and no further searches are required

In the 2nd case, because the desired item is greater than the middle element, if it is found at all, it must be in the upper part of the list. This means that the lower part of the list consisting of all the elements from the first to the midpoint element can be discarded from the further search

In the 3rd case, because the desired item is less than the middle element, if it is found at all, it must be found in the lower part of the list. For this case, the upper part of the list containing all elements from the midpoint to last can be discarded from any further search

Page 19: DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.

Binary Search (cont…)

The pseudocode for binary search:Set the lower index to 0Set the upper index to 1 less than the size of the listBegin with the first item in the listWhile the lower index is less than or equal to the upper index

Set the midpoint index to the integer average of the lower and upper

Compare the desired item to the midpoint elementIf the desired item equals the midpoint element,

Return the index value of current itemElse if the item is greater than midpoint,

Set the lower index value to the midpoint + 1Else if the item is less than the midpoint,

Set the upper index value to the midpoint – 1EndIf

EndWhileReturn -1 because the item was not found

Page 20: DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.

Binary Search (cont…)

• As illustrated in the pseudocode, a while loop is used to control the search. The initial list is defined by setting the lower index value to 0 & the upper index value to 1 less than the number of elements in the list

• Once the comparison to midpoint made, the search is subsequently restricted by moving either the lower index to one integer value above the midpoint or by moving the upper index to one integer value below the midpoint

• The process is continued until the desired element is found

Page 21: DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.

Binary Search (cont…)

• The function binarySearch()in C++//this function returns the location of key in the list

//a -1 is returned if the value is not found

int binarySearch (int list[], int size, int key)

{

int left, right, midpt;

left = 0;

right = size -1;

while (left <= right)

{

midpt = (int) ((left + right)/2);

if (key == list [midpt])

{

return midpt;

}

else if (key > list [midpt])

left = midpt + 1;

else

right = midpt – 1;

}

return 0;

}

Page 22: DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.

Summary

• The primary key is a key or unique item than can represent record, and the secondary key is an alternate key

• There are 2 common types of file access which are sequential file access and random file access

• 2 common search techniques:– Linear Search– Binary Search