0 Up votes0 Down votes

3 views8 pagesJul 19, 2013

© Attribution Non-Commercial (BY-NC)

PDF, TXT or read online from Scribd

Attribution Non-Commercial (BY-NC)

3 views

Attribution Non-Commercial (BY-NC)

- General Directions
- Generic RobotC Programming Book
- Final DAA Lab Manual
- 2012-01-25--hash_tables.pdf
- Data Structures
- data structures
- mcs011b2
- java questions
- MELJUN CORTES JAVA_Control_Structures_repition
- 930XII - Arrays Old Q&A
- Fortran Tutorial
- Arrays and Do Loops
- Introduction to Array1
- Downing Simple Rainflow Counting Algorithms
- Php
- lessonPlan CLB10503 -Sept2013
- LiveCode_Data_Grid
- Preface Table of Cont
- KONSEP ARRAY DI PHP
- Chapter 06

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

- General DirectionsUploaded byAshok Ghunawat
- Generic RobotC Programming BookUploaded byDownald Troump
- Final DAA Lab ManualUploaded byGina Barrera
- 2012-01-25--hash_tables.pdfUploaded byfakhrirf
- Data StructuresUploaded byEnrique Bautista Azuara
- data structuresUploaded byraghu p
- mcs011b2Uploaded byrkthbd5845
- java questionsUploaded bySudeep Mishra
- MELJUN CORTES JAVA_Control_Structures_repitionUploaded byMELJUN CORTES, MBA,MPA
- 930XII - Arrays Old Q&AUploaded byhuyeb
- Fortran TutorialUploaded byLeandro Muniz
- Arrays and Do LoopsUploaded byMpho Seutloali
- Introduction to Array1Uploaded byRAJENDRAN
- Downing Simple Rainflow Counting AlgorithmsUploaded byridzim4638
- PhpUploaded byChetan Padsalgi
- lessonPlan CLB10503 -Sept2013Uploaded byArif Hilmi
- LiveCode_Data_GridUploaded bybishwa
- Preface Table of ContUploaded bySuNny Khan
- KONSEP ARRAY DI PHPUploaded byfirmansyah arsyad
- Chapter 06Uploaded bysamsoom80
- 08082011Uploaded bySanjay Singh
- Stacks SonUploaded bySalman Khan
- Java CollectionGenericUploaded byniko.chu7100
- Excel FAQUploaded byPallab Datta
- Lecture IP1 2012Uploaded byKapil Govindwad
- Handout 1Uploaded byVinod Partihar
- StackUploaded byAbir Das
- Assignment 7Uploaded byMohit Sharma
- 01SoftwEngUploaded byRam Engineer
- ABAP-IUploaded bySekhar Chowdary

- Minato ROBDD lecture 2Uploaded byMehedi Sarwar
- Study FY Bsc_cs_ Paper-IIUploaded bynileshnemade
- Word Game LabUploaded byNeel Jani
- MergeSortUploaded byapi-3825559
- MCS-031 Solved 2014-15Uploaded bymuralidhar
- Recursion Trees and Master MethodUploaded byHarsh Vardhan Jha
- K Constrained Shortest Path ProblemUploaded byinstreng
- Daa Question BankUploaded byKumar Manish
- 7 Scheduling ArtiguesUploaded byDumb Scientist
- mcs-011Uploaded byShubhashreeBhattacharyaPanda
- DAA JK - Asymptotic Notation - Minggu 4_16Uploaded byDanielle Martin
- Pttkgt FinalUploaded byHuy Le Quang
- _Computer Application-BCA Fifth Sem (1)_2.pdfUploaded byfrank ansari
- DESIGN & ANALYSIS OF ALGORITHMS -Lecture 1Uploaded byadeleno
- Suggestion Paper for CS503 - Design and Analysis of AlgorithmUploaded byMyWBUT - Home for Engineers
- sol12.pdfUploaded byAashish D
- MEUploaded byMrunal Upasani
- cs301 (1)Uploaded byRizwanIlyas
- Solving the Pell EquationUploaded byppp2009swiss
- Top CoderUploaded byABHISHEK ANAND
- Lesion Identification and Tissue Segmentation in Magnetic Resonance (MR) Image using Interval type based ClusteringUploaded byEditor Ijdcn
- n Jobs and M- Machine ProblemUploaded byMohammed Afshan
- Parallel Computing Zernike Moments via Combined AlgorithmsUploaded bythesij
- Car ObstacleUploaded bypunam gavali
- DsaUploaded bydgbooklover0766571
- Course Material 1Uploaded byashok_bbsr
- Design and Analysis of Algorithms 1Uploaded byLohidas Paila
- ISC Computer Science ScopeUploaded byCalculus Coding
- DAA MID2 [UandiStar.org]Uploaded byRikki Revs
- CS109.MathLect04.ComplexityAlgorithmsUploaded byadetejubello

## Much more than documents.

Discover everything Scribd has to offer, including books and audiobooks from major publishers.

Cancel anytime.