3 views

Uploaded by ekaloke

- Introduction to Matlab
- Smvdu 5th Sem Java Lab
- PHP Question Answer
- c_arrays
- 14575 Sample Paper 1
- Temel MATLAB Komutları
- 2d Array Coding
- Clad Sample Exam2
- Arrays
- Syntax
- R RStudio Basics
- calculation commands
- Programs List for Class XII-2010
- 05 Arrays
- Unit 4 - Ch 7 Test KEY
- The Stl Vector Class
- 13908revision for First Semester Exam(Xi)
- JAVA MS 4 v.01(03)
- SDA-CIC2006
- JAVA Assignment.docx

You are on page 1of 8

Searching techniques

Searching algorithms in unordered lists Lists may be arrays or linked lists The algorithms are adaptable to either Searching algorithms in ordered lists Primarily arrays with values sorted into order We can exploit the order to search more efficiently We will only consider arrays The data held in the array might be Simple (e.g. numbers, strings) Or more complex objects: the search will probably be based on a chosen key field in the objects (eg, search student records by registration number) We will only consider simple data the searching techniques are the same An informal look at complexity analysis

CSC933 Searching University of Stirling 1

In these algorithms we will assume: The data is ints, held in an array variable numbers, in random order, with the number of data values indicated by a variable size So the data is in elements indexed 0 to (size-1) We are seeking the int held in variable val The basic technique to be used is sequential search Compare val with the value in numbers[0], then with that in numbers[1], etc We will look at two versions of an algorithm encoding this Other adaptations are possible

University of Stirling

CSC933/Searching

Here is a basic search algorithm. It leaves its result in a variable called position: int position = 0; while (position < size) { if (numbers[position] == val) break; // Exit loop if found position++; } If val is not present: The entire array will be scanned - taking size steps position will have a final value of size But if val is present: break; => the while loop terminates immediately The average number of scanning steps expected is size/2 Easy to adapt to return a boolean, or throw an exception

CSC933 Searching University of Stirling

If we are careful, we can combine the loop test and the array element check: int position = 0; while (position < size && numbers[position] != val) position++; The && test checks position < size first, and if it is false does not check numbers[position] != val ... otherwise would get ArrayIndexOutOfBoundsException if val is not present! This is called "conditional" or "short-circuit" behaviour: it applies to && and || There are also & and | that always check both sides

CSC933 Searching University of Stirling 4

University of Stirling

CSC933/Searching

We can improve the basic search algorithm if the array numbers has one extra element, numbers[size], that is never used for actual data: Instead we place a copy of the sought value there, so the search always succeeds This means that the loop does not need to carry out the end of array test - less work, so quicker: int[] numbers = new int[size+1]; ... int position = 0; numbers[size] = val; // Insert "sentinel" while (numbers[position] != val) position++; As before, position has the final value size if val is not present

CSC933 Searching University of Stirling 5

We try to identify how the time taken to execute an algorithm depends on the quantity of data to be processed True time is tricky - instead work in terms of abstract steps One abstract step should correspond to a consistent "unit of time" If the number of data items is N then: If the number of steps is independent of the quantity of data (eg, 6 steps) then we have a constant time algorithm, and we state that the order of complexity is O(1) (pronounced order one ) If the number of steps is proportional to the quantity of data (eg, N+2, N/2, 3N) then we have a linear algorithm, and we state that the order of complexity is O(N) (pronounced order N )

CSC933 Searching University of Stirling

University of Stirling

CSC933/Searching

The order of complexity does not usually tell us exactly how many steps are taken But it does indicate how the number of steps taken varies with the quantity of data And so how the overall time will vary with the quantity of data Examples: If an algorithm is constant time,O(1), and it takes 1 second to process 10 items of data, then it will take 1 second to process 1000 items of data If an algorithm is linear, O(N), and it takes 1 second to process 10 items of data, then it will take 100 seconds to process 1000 items of data

We may be interested in an algorithm s best case, worst case or average execution time: For the sequential search algorithm (with or without sentinel): Best case is 1 step: O(1) Worst case is N steps: O(N) The actual average number of steps depends on ratio of successful/unsuccessful searches: The average of successful searches is N/2 steps, and so is O (N) All unsuccessful searches take N steps, which is O(N), So overall the average complexity is O(N)

University of Stirling

CSC933/Searching

Again we will assume: The data is integers, held in an array sequence, So the data is in elements indexed 0 to (length-1) But this time we assume that the values are held in ascending numerical order We are seeking the integer held in val We could use the sequential search algorithm, but this does not take advantage of the knowledge that the data is ordered. (The complexity remains O(N). ) Instead, we will take advantage of the ordering to improve search efficiency (ie, to reduce the complexity)

CSC933 Searching University of Stirling 9

If the data is already ordered, we can do much better than a linear time algorithm: Here is the scheme: Pick the middle element If it is equal to val, stop the search If it is greater than val, search the lower half of the remaining array If it is less than val, search the remaining upper half At each iteration: We are searching in a remaining partition of the array We cut the remaining partition in half, rather than just removing one element Example: Searching for 11 in 1, 3, 5, 7, 9, 11, 13 First compare with 7, so search in 9, 11, 13 Now compare with 11 - found it - in two steps

CSC933 Searching University of Stirling 10

University of Stirling

CSC933/Searching

Concretely: Let variable low indicate the lowest element of the partition (index 0 initially) high (h) indicate the highest element (size-1 initially) middle (m) indicate the next element being tested The search for 11 proceeds like this:

0 1 2 3 4 5 6

low=0 h=6

! m=3

1 low

7 m

11 13 h

11 13 h

low m

11

0 1 2 3 4 5 6

low=0 h=6

! m=3

1 3 low 1 3

7 m

11 13 h 11 13 h

low m 3 5 7 9 low h m 3 5 7 9

Not found, and 10<11, so 1 low=4 h=(m-1)=4 ! m=4 Not found, and 10>9, so low=(m+1)=5 h=4 1

11 13

11 13

Now low>h, and the partition has vanished : the search has failed

CSC933 Searching University of Stirling

h low

12

University of Stirling

CSC933/Searching

Algorithm binarySearch: INPUT: val value of interest, sequence sorted data OUTPUT: object or value of interest if exists, null o/w. int low = 0, middle = 0, high = seq.length; while (high >= low) { middle = (high + low) / 2; if (sequence[middle] == val) return sequence[middle]; // Found it else if (sequence[middle] < val) low = middle + 1; // Search upper half else high = middle - 1; // Search lower half } return null;

The outcomes: Ordinary loop exit when the indexes cross ! not found (i.e. high < low) Loop exit on return ! found (detect this by testing high >= low)

CSC933 Searching University of Stirling 13

Best case: val is exactly sequence[middle] at the first step The search stops after first step, so complexity O(1) Worst case: N log2N This will be when we continue dividing until the partition 1 0 2 1 contains only one value: then it is either equal to val or not 4 2 8 3 For 250 elements this turns out to be about 8 iterations 16 4 32 5 64 6 For 500 it is about 9 128 7 256 8 For 1000 it is about 10 512 9 1024 10 Double the amount of data ! Add one step! 2048 11 4096 12 8192 13 In general: the size is approximately 2steps 16384 14 32768 15 So the number of steps is approximately log2 size 65536 16 131072 17 Complexity is O(logN) 262144 18 524288 19 1048576 20 For emphasis: double the amount of data ! Add one step! Average case: Don t need to consider this: the worst case is very good!

CSC933 Searching University of Stirling 14

University of Stirling

CSC933/Searching

End of lecture

15

University of Stirling

- Introduction to MatlabUploaded byZoraida17
- Smvdu 5th Sem Java LabUploaded byGyanendra Giri
- PHP Question AnswerUploaded byManish Sharma
- c_arraysUploaded byDonnabelle Varona
- 14575 Sample Paper 1Uploaded byChris Macman
- Temel MATLAB KomutlarıUploaded byaderanc
- 2d Array CodingUploaded byNurulSyafiqahNyum
- Clad Sample Exam2Uploaded byulaganathan
- ArraysUploaded byViswanadham Karu
- SyntaxUploaded byAarifaBegum
- R RStudio BasicsUploaded byWulandari Amor Ganelsa
- calculation commandsUploaded byShashi Shirke
- Programs List for Class XII-2010Uploaded byVikas Saxena
- 05 ArraysUploaded byBreeze Jaspreet
- Unit 4 - Ch 7 Test KEYUploaded byOsama Maher
- The Stl Vector ClassUploaded byGiovanni De Gasperis
- 13908revision for First Semester Exam(Xi)Uploaded byUpendra Yadav
- JAVA MS 4 v.01(03)Uploaded byAnishSahni
- SDA-CIC2006Uploaded byGilles Carpentier
- JAVA Assignment.docxUploaded byJacquelina Stanley
- AN IMPLEMENTATION OF.pdfUploaded bywulaningayu
- LeatherwoodCh 8 Lab 1Uploaded byChristy
- Teach PendantUploaded byChanel Méndez Ramírez
- SectionB_Group10Uploaded byAakash Rai Singhani
- Tb40laan SortUploaded byGilmer Calderon Quispe
- CPE102_2558-02_11_20160212.pdfUploaded byเฟท ฮาราวนด์
- Map Coloring WP2.pdfUploaded bySamuel Waweru
- Rm TreesUploaded byTom Tera
- 05_pointers.pdfUploaded byjeep2014
- ArraysUploaded byHFdzAl

- Improvement to RankineUploaded byRitika Singh
- Civil Engineering Objective Type Questions by S.S.bhavikatti Www.civilEnggForAll.com (1)Uploaded byShikhin Garg
- Arduino LibrariesUploaded byDanie Fsm
- Assignment 1Uploaded bySyếđ AbữBâkar
- gloc17_tables.pdfUploaded byMayank Sharma
- Additional Myths About LNGUploaded byRagerishcire Kanaalaq
- Send LogUploaded byRenato Goes
- Feeder Protection for OhlUploaded bymentong
- Elements of Corporate Decision MakingUploaded byMuh Resa Yudianto
- Ntse Mat AssignmentUploaded bySanjeevkumarSingh
- M2 experimentUploaded byalextty
- Issues in SLAR - D NunanUploaded byPatriciaFranco
- aq12fanUploaded byadkadim
- Van de Graaff GeneratorUploaded byshams
- Introduction of Microwave.pdfUploaded byAmit Garg
- SMR Steam Methane ReformingUploaded byHugo Márquez
- E2E Flow Control(RAN13.0_02)Uploaded byzapostikk
- Nsx 63 AdminUploaded byTatjana Kuburic
- History of Operating SystemsUploaded byChandrakant Karalkar
- Isni Maris Ayu Nintias17210019Komputasi Numerik.pdfUploaded byIsni maris
- PrefixUploaded byRozita Ali
- 2008 Effects of Cadence on Energy Generation and Absorption at Lower Estremity Joints During GaitUploaded byalkeshkhatri
- WAITING LINES AND CUSTOMER SATISFACTIONUploaded byM S Sridhar
- Display More Than One Internal Table in ALV Using Object Oriented ABAP ProgrammingUploaded bymdwaris
- SIM908_AT Command Manual_V1.02.pdfUploaded byAlfian Tjiptowiyono
- 2 Course SyllabusUploaded byghgh
- Chapter 12 PopulationsUploaded byBishok Dangol
- LecroyUploaded byVenugopal Santhanam
- You, The Kuhn-Tucker Conditions, And YouUploaded byisaimartinez06
- Aquasol BrochureUploaded byMunirajulu Mc