• Non ci sono risultati.

Search on Unordered Arrays

N/A
N/A
Protected

Academic year: 2021

Condividi "Search on Unordered Arrays"

Copied!
19
0
0

Testo completo

(1)

Search Algorithms

Paolo Camurati and Stefano Quer

Dipartimento di Automatica e Informatica

Politecnico di Torino

(2)

Search on Unordered Arrays

 Problem definition

Given an array storing n integer values

Given a specific value, i.e., a key k

 Reply to the following question

 Is key k present in the array v ?

 Input

 v[n], n, k

 Output

 Yes/No

 If Yes in which position (index) k is in the array

(3)

Algorithm 1: Sequential Search

 Sequential search

 Scan the array from the first element to potentially the last one

 Compare key k and current value

 Return index if the comparison is successful

 When all comparisons have been unsuccessful,

i.e., at the end of the scan, return -1

(4)

Algorithm 1: Sequential Search

 Successful search

0 1 2 3 4

v 1 6 4 2 0

k 4

return 2 n = 5

Step 1: v[0] k

Step 2: v[1] k

Step 3: v[2] = k

(5)

Algorithm 1: Sequential Search

 Unsuccessful search

Step 1: v[0] k

0 1 2 3 4

v 1 6 4 2 0

k 8

Step 2: v[1] k

Step 3: v[2] k

Step 4: v[3] k

Step 5: v[4] k

return -1

(6)

Algorithm 1: Sequential Search

 Notation for arrays

0 n-1

v

l r

Array v of n elements

n

Leftmost element left or l index

0 n-1

v

l r

n Entire array: l=0, r=n-1

Subarray: l, r

Rightmost element right or r index

We will use l and r in this section

(7)

int LinearSearch (int v[], int l, int r, int k) { int i = l;

int found = 0;

while (i<=r && found==0) { if (k == v[i]) {

found = 1;

} else { i++;

} }

if (found==0) return -1;

else

return i;

}

Algorithm 1: Sequential Search

Leftmost array index Rightmost array index

(8)

Complexity Analysis

 Analytic analysis

- 1 1 - r-l+1+1

r-l+1 - r-l+1

- - - 1 - 1 - int LinearSearch (...) {

int i = l;

int found = 0;

while (i<=r && found==0) { if (k == v[i]) {

found = 1;

} else { i++;

} }

if (found==0) return -1;

else

return i;

}

Number of operations (with unit cost) for the worst case

(9)

Complexity Analysis

T(n) = 1 + 1 + (r–l+1+1) + 2(r–l+1) + 1 + 1

= 1 + 1 + (n + 1) + 2n + 1 + 1

= 3n + 5

= Θ (n)

 T(n) grows linearly

Worst case O(n) overall

- 1 1 - r-l+1+1

r-l+1 - r-l+1

- - - 1 - 1 -

(10)

Complexity Analysis

 Intuitive analysis

 The worst case scenario is the one of an unsuccessful search

 We have n steps for a search miss

 In average n/2 steps for a search hit

 T(n) grows linearly with n

 T(n) = Θ (n)

 Analytic analysis

 Worst case

 Unsuccessful search

 We assume unit cost for all operations

(11)

Search on Ordered Arrays

 Problem definition

Given an array storing n integer values in ascending order

Given a specific value, i.e., a key k

 Reply to the following question

 Is key k present in array v ?

 Input

 v[n], n, k

 Output

 Yes/No

 If Yes in which position (index) k is in the array

(12)

int LinearSearch (int v[], int l, int r, int k) { int i = l;

while (i<=r && k>v[i]) { i++;

}

if (k == v[i]) { return (i);

} else {

return (-1);

} }

Algorithm 2: Sequential Search

Leftmost array index Rightmost array index

If k<v[i] it is not possibile to find k any

more

(13)

Complexity Analysis

 Has complexity been improved?

 Intuitive analysis

 The worst case scenario is the one of an

unsuccessful search with an element larger than all array values

 We have n steps

 T(n) still grows linearly with n

 T(n) = Θ (n)

 Can we do better?

(14)

Algorithm 3: Binary Search

 Binary search in a sorted array

 First version: 1946

 First bug-free version: 1962

 Approach

 Start with (sub-)array of extremes l and r

 At each step

 Find middle element c=(int)((l+r)/2)

 Compare k with middle element in the array

● =: termination with success

● <: search continues on left subarray

● >: search continues on right subarray

Found bug in java Arrays.binarySearch():

2006

(15)

Algorithm 3: Binary Search

 Successful search

Step 1: c=(l+r)/2=4; k < v[c]

0 l

1 2 3 4 5 6 7 8 9

r v 2 4 6 8 10 12 14 16 18 20

k 8

Step 2: c=(l+r)/2=1; k > v[c]

0 l

1 2 3

r

4 5 6 7 8 9

r v 2 4 6 8 10 12 14 16 18 20

l = leftmost array index r = rightmost array index c = index of middle element

(16)

Algorithm 3: Binary Search

Step 3: c=(l+r)/2=2; k > v[c]

0 l

1 2 l

3 r

4 5 6 7 8 9

r v 2 4 6 8 10 12 14 16 18 20

k 8

Step 4: c=(l+r)/2=3; k = v[c]

0 l

1 2 3

lr

4 5 6 7 8 9

r v 2 4 6 8 10 12 14 16 18 20

return 3

(17)

Algorithm 3: Binary Search

Step 4: c=(l+r)/2=3; k v[c]

0 l

1 2 3

lr

4 5 6 7 8 9

r v 2 4 6 7 10 12 14 16 18 20

 Unsuccessful search

Step 1: c=(l+r)/2=4; k < v[c]

0 l

1 2 3 4 5 6 7 8 9

r v 2 4 6 7 10 12 14 16 18 20

k 8

return -1

… like previous case but last step …

(18)

int BinarySearch (int v[], int l, int r, int k) { int c;

while (l<=r){

c = (int) ((l+r) / 2);

if (k == v[c]) { return(c);

}

if (k < v[c]) { r = c-1;

} else { l = c+1;

} }

return(-1);

}

Algorithm 3: Binary Search

Leftmost array index Rightmost array index

Or just c = (l+r)/2;

as c is an integer variable

(19)

Complexity Analysis

 Analytic analysis

 The worst case scenario is the one of an

unsuccessful search or a successful search with a hit at the final step

 We have n elements

 At each step, we divide n by 2

 In i steps, we divide n by 2

i

, i.e., n/2

i

 We stop after i steps, when

● n/2i = 1

● n = 2i

● i = log2 n

 T(n) grows logaritmically with n

 T(n) = O (log

2

n)

Riferimenti

Documenti correlati

○ SC1-HCC-02-2019 Support for the large scale uptake of open service platforms in the Active and Healthy Ageing domain, SC1-DTH-05-2019 Large scale implementation of digital

dalla documentazione attestante competenze professionali rilevanti ai fini dell’incarico e da ogni altra documentazione che gli interessati ritengano utile ai fini

The framework is based on so-called open nets, a mild generalisation of ordinary Petri nets introduced in [2, 3] to answer the first of the requirements above, i.e., the possibility

Per la valutazione comparativa, il Dipartimento si avvarrà di apposite commissioni istruttorie, nominate dal Consiglio di Dipartimento che erogherà l'insegnamento e

Le domande di ammissione al concorso in formato pdf, redatte secondo il fac-simile allegato, corredate della documentazione richiesta in formato pdf (o zip ove

Desalination is a widespread process characterized by huge energy requirement and used to remove dissolved salts and other minerals from seawater in order to get water

Verbale di approvazione degli esiti della procedura di selezione di docenti esterni inseriti nell'&#34;Albo idonei per attività didattica&#34; finalizzata alla copertura di

Le forze di attrito dipendono dalla forza assiale che subisce la vite È quindi possibile trovare una relazione fra momento di serraggio (e di svitamento) e forza assiale sulla vite