Vous êtes sur la page 1sur 18

, 2008

1. .................................................................................................................... 1
1.1. ................................................................................................. 1
1.2. : ..................................................................................... 2
2. ............................................................................................................. 4
3. .......................................................................................................................... 5
4. (Searching Algorithms) .......................................................................... 7
4.1. (Sequential Search)................................................................................ 7
4.2. (Binary Search) ...................................................................................... 8
4.3. (Recursive Binary Search) .............................................. 11
5. (Hash Tables) .................................................................................... 12
5.1. (Hash Functions) ................................................................. 13
5.2. (Collision Resolution) ...................................................................... 14
5.2. Java ...................................................................................... 15

1.

. .
.
.
;
,
. ,

.

1.1.
:
,
.


.
,
.
.
,

, .
,

.


.


.
,

.

2

/ .

/
. ,
.


.
, /,
.
, .

.
.
.

1.2. :
. .

.
;
:
. ,
. .
,
.

.
, .

. 1
.
maxDonation
. main maxDon
, maxDonation.
,
.

3
{1} , ,
{2} 1.
,
{2.1.1}.
(
) (
).

class MaxDon {
public static void main (String[] args) {
float[] donArray = {4, 21, 9, 15, 7, 18, 27, 12};
float maxDon = maxDonation(donArray);
System.out.println("Donations:");
for (int i=0; i<donArray.length; i++) {
System.out.print(donArray[i]);
if (donArray[i]==maxDon)
System.out.println(" <== maximum donation");
else
System.out.println();
}
}
static public float maxDonation (float[] donateArray) {
int i;
float max;
max= donateArray[0];
{1}
for (i=1; i<donateArray.length; i++)
{2}
if (max < donateArray[i])
{2.1}
max = donateArray[i];
{2.1.1}
return max;
}
}
1.
1:
10 .
{2.1.1} , 9
.


. , 10
:

{1}
{2}
{2.1}
{2.1.1} 0

19 28 .

2:
( ) 1000 .

.

.
n
. n :
{1}
{2} n-1
{2.1} n-1
{2.1.1} 0 n-1

(1 + (n - 1) + (n - 1) + 0) : 2n - 1

(1 + (n - 1) + (n - 1) + (n - 1))

: 3n - 2

.
.

3:
.
.
.

2.
,
.
:

; ,
;

; ,
,

;


( ) n .
.

:
__(n) = 3 n - 2

, .

.

3.

. ,
.
. ,
.
, for
while 2. H
maxDonation2 .
( n ) :
{1}
{2}
{3} n
{3.1} n-1
{3.1.1} 0 n-1
{3.2} n-1

public static float maxDonation2 (float[] donateArray) {


float max = donateArray[0];
{1}
int i = 1;
{2}
while (i < donateArray.length) {
{3}
if (max < donateArray[i])
{3.1}
max = donateArray[i];
{3.1.1}
i = i+1; {3.2}
}
return max;
}
2.

maxDonation2
. ( n )
:
{1}
{2}
{3} n
{3.1} n-1
{3.1.1} 0 n-1
{3.2} n-1

3n 4n-1
.
.
;
n.
,
n.
, 5000
1000 .

.

.

4. (Searching Algorithms)

4.1. (Sequential Search)


( ),
,
. ,
.

.
3, SeqSearch
(sequential search) key
numbers.

public class SeqSearch {


public static void main (String[] args) {
int numbers[] = {7, -3, 7, 2, 8, -1, 3, 2, 5, 6, 7};
System.out.println("The index of 6 is " +
LinearSearch.search (numbers, 6));
} // End of main
} // End of class SeqSearch
class LinearSearch {
public static int search (int[] a, int key) {
for (int i=0; i<a.length; i++)
if (a[i] == key)
return i;
return -1;
}
}

{1}
{1.1}
{1.1.1}
{2}

3.

n
:
{1} n+1
{1.1} {1} .

{1.1.1}
{2}

{1.1}. {1} +1
{1.1.1} {2} ,
. ( + 1) + +1.
0 n
2 ( =0) 2+2n ( =n).
,
.
,
.

. {1.1} 1, 2, 3, ..., n,
. {1.1} :

1 2 3 ... n

n(n 1)
n 1
2

n
2

,
:
n 1 n 1
1
1 n 3

2
2

4:

.
search.
. ;

4.2. (Binary Search)


(binary search)
. .
.
:

9
.
.
,
. ,
(
), .
,
1/4 ... ,
.
4 BinarySearch
(binSearch) key
numbers.
n
{1} {3} .
{2}
. {2} :
{2.1}
{2.2}
{2.2.1}
{2.3}
{2.3.1}
{2.3.2}

public class TestBinSearch {


public static void main (String[] args) {
int[] numbers = {1, 3, 5, 7, 8, 9, 11, 15, 22, 33, 44};
int key=11;
int pos = BinarySearch.search (numbers, key);
if (pos != -1)
System.out.println ("The index of "+key+" is " + pos);
else
System.out.println (key + " is not part of the array");
}
}
public class BinarySearch {
public static int search (int[] numbers, int key) {
int left=0, right=numbers.length-1;
return binSearch(numbers, key, left, right);
}

10
private static int binSearch (int[] numbers, int key, int
left, int right) {
int mid, pos=-1;
{1}
while (pos==-1 && left<=right) {
{2}
mid = (left+right)/2;
{2.1}
if (key < numbers[mid])
{2.2}
right = mid-1;
{2.2.1}
else
if (key > numbers[mid])
{2.3}
left = mid+1;
{2.3.1}
else
pos = mid;
{2.3.2}
}
return pos;
{3}
}
}
4.

.

{2} : {2.1},{2.2},{2.3}, {2.3.1} {2.3.2}.
{2},
/ 4 2.

.
n .
n/2. .
: n.
n 2
. 2 0;
L(n) n 2.
L(n) ( )
n:
n
L(n)
10
4
100
7
1.000
10
10.000
14
1.000.000
20

{2} L(n), 4L(n)+2.
,

11
20
82 . 1.000.003
.

4.3. (Recursive Binary Search)


,
.
.
public class RecBinSearch {
public static void main (String[] args) {
int[] numbers = {2, 3, 5, 7, 8, 9, 11, 15, 19, 22, 33};
int key=7;
int pos = RecursiveBinSearch.search (numbers, key);
if (pos != -1)
System.out.println ("The index of "+key+" is "+pos);
else
System.out.println (key+" is not part of the array");
}
}
class RecursiveBinSearch {
public static int search (int[] numbers, int key) {
int left=0, right=numbers.length-1;
return recSearch(numbers, key, left, right);
}
private static int recSearch (int[] numbers, int key, int
left,
int
right) {
int index=(left+right)/2;
if (left>right)
return -1;
else
if (key == numbers[index])
return index;
else
if (key>numbers[index])
return recSearch (numbers, key, index+1, right);
else
return recSearch (numbers, key, left, index-1);
}
}
5.

12
5,
(recSearch) key
numbers.
recSearch
.
( -),
-.

5. (Hash Tables)
. 1 n
.
. ().

.
( ) .

.

,
.
(hash function),
(hashing)
(hash table).
(hash table hash map)
. ,
.

. (collision).
.
:

(collision resolution strategy)

13

5.1. (Hash Functions)


H(k)
.
.

.
,
. :
.
.
H(k) = k % m
m . (
m ).
:
olive
carrot
onion
tomatopaste

cheese
salt

ham
mushroom

.
, .

olive
cheese
onion
salt

63 (15+12+9+22+5)
45 (3+8+5+5+9+5)
67 (15+14+9+15+14)
52 (19+1+12+20)

carrot
ham
tomatopaste
mushroom

75 (3+1+18+18+15+20)
22 (8+1+13)
145 (20+15+13+1+20+15+16+1+19+20+5)
122 (13+21+19+8+18+15+15+13)


, 260
10.
.
13 ( ) :

olive
cheese
onion
salt

H()

63
45
67
52

(%13)
11 (63%13)
6 (45%13)
2 (67%13)
0 (%13)

H()

carrot
75
ham
22
tomatopaste
145
mushroom
122

(%13)
10 (75%13)
9 (22%13)
2 (145%13)
5 (122%13)

14

5.2. (Collision Resolution)


(tomatopaste)
: (onion).
.
bean (H[bean]=22 - 9 (22%13))
- ham, .
ham.
(collision resolution)
.

(linear probing). ,
( )
.
13
6.
[11] olive
[6] cheese
[2] onion
[0] salt
[10] carrot
[9] ham
[2]
[3] tomatopaste
[5] mushroom
[9]
[10]
[11]
[12] bean

0 salt
1
2 onion
3 tomatopaste
4
5 mushroom
6 cheese
7
8
9 ham
10 carrot
11 olive

12 bean
6.

: olive 11 ( 12
), .
6, 2, 0, 10 9 .
tomatopaste 2.
onion

15
. (3) .
mushroom 5 . bean
9 ham
. carrot
olive. bean 12.

5.2. Java
Java
, .
, ,
.
Object hashCode()
.
.
, java.util Hashtable
.
.
,
hashCode equals.
Hashtable :
(initial capacity) (load factor).

.
.

,
rehash.
(public) Hashtable :
public Hashtable()
public Hashtable(int size)
public Hashtable(int size, float load) throws IllegalArgumentException
, (
101)
( 0.75).
Hashtable :
public void clear()

16

public boolean contains(Object arg) throws NullPointerException


true arg
public boolean containsKey(Object index)
true
index.
public Object get(Object index)
public Object put(Object index, Object arg) throws NullPointerException
public Object remove(Object index)
, arg
index.
public boolean isEmpty()
true .
protected void rehash()
.
.
public int size()
.
public String toString()
- string.

Vous aimerez peut-être aussi