private static int binSearch(int[] data, int key) { - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

private static int binSearch(int[] data, int key) {

Description:

Title: Subject: . . Author: ... – PowerPoint PPT presentation

Number of Views:89
Avg rating:3.0/5.0
Slides: 21
Provided by: 54087
Category:

less

Transcript and Presenter's Notes

Title: private static int binSearch(int[] data, int key) {


1
???????? ????? ? ????????????? ???????
33
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
0
1
3
4
8
10
14
16
21
24
27
31
33
36
38
42
44
50
51
53
59
private static int binSearch(int data, int key)
int l 0, h data.length-1
while (l lt h) int m (l h) / 2
// (l lt m lt h) if (datam lt key) l m
1 else h m return (datal key
? l -1)
????????? ????? l lt h ???? key datak,
?? l lt k lt h
2
?????????? ???????
1. ?????????? ?????????
public static void bubbleSort(int data)
int n data.length for (int i 1 i lt n
i) for (int j 0 j lt n-i j)
if (dataj gt data j1)
int tmp dataj dataj
dataj1 dataj1 tmp

3
2. ?????????? ???????? ?????????
1
3
4
8
10
14
16
21
24
31
33
36
38
42
44
50
53
59
1
3
4
8
10
14
16
21
24
31
33
36
38
42
44
50
27
51
53
59
1
3
4
8
10
14
16
21
24
31
33
36
38
42
44
50
27
51
53
59
1
3
8
10
16
21
24
33
36
38
44
50
53
59
public static void simpleSort(int data)
int i, j for (i 1 i lt data.length i)
int c datai for (j i-1
j gt 0 dataj gt c j--)
dataj1 dataj dataj1
c
4
3. ?????????? ????????? ?????????
public static void binInsertSort(int data)
int n data.length // ????? ???????
for (int i 1 i lt n i) int c
datai // ??????????? ???????? //
??????????? ?????? ????? ??? ??????? ???????? c
int low 0, high i // Inv
(low lt high) ????? ??? c - ??????
datalowhigh while (low lt high)
int m (lowhigh) gtgt 1 // low
lt m lt high if (datam lt c) low
m1 else high m // ???????
????? ??????? - low // ???????? ????????
? ??????? ??????? ????????. for (int j
i-1 j gt low j--) dataj1
dataj // ??????? ???????? ??
????????? ????? datalow c
5
4. ?????????? ?????
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
0
1
3
4
8
10
14
16
21
24
31
33
36
38
42
44
50
27
51
53
59
step7
step3
step2
step1
6
4. ?????????? ?????
public static void ShellSort(int data)
int n data.length // ????? ??????? int
step n // ??? ??????? ? ???????
int i, j do // ????????? ?????
??? step step / 3 1 //
?????????? ?????????? ???????? ????????? ?
???????? ????? for (i step i lt n i)
int c datai for (j
i-step j gt 0 dataj gt c j - step)
datajstep dataj
datajstep c
while (step ! 1)
?????????? ???????????? ????????? (??
??????????? ????????????? ?? ????????? ????????)
n 25 n 1000 n 100000
?????????? ????? 50 7700 2 100 000
?????????? ???????? ????????? 150 240 000 2.5 ????.
7
5. ???????? ??????? ????????????? ????????
4
public static int merge(int a, int b)
int na a.length, nb b.length,
nc int c new intnc na nb
int ia 0, ib 0, ic 0
while (ia lt na ib lt nb) if (aia lt
bib) cic aia
else cic bib
while (ia lt na) cic aia while (ib
lt nb) cic bib return c
14
27
51
1
3
8
24
31
42
59
8
6. ?????????? ??? ??????? (????????)
1
3
4
8
10
14
16
21
24
31
33
36
38
42
44
50
27
51
53
59
? ??? ?????
9
???????? ?????????? ??? ???????
public static void mergeSort(int data)
int n data.length // ????? ???????
int altData new intn // ??????????????
?????? int from data, //
????????? "??????" ? "????" ?????????? ???????
to altData int len 1
// ????? ?????????? ????????? do
int start 0 while (start lt n)
// ??????? ??????? fromstart(startlen-1)
? from(startlen)(start2len-1)
// ? tostart(start2len-1)
mergeSection( from, start,
Math.min(startlen, n), from,
Math.min(startlen, n), Math.min(start(lenltlt1),
n), to, start)
start (len ltlt 1) // ??????
??????????? ??????? int interm from
from to to interm while ((len ltlt 1)
lt n) // ???? ????? ?????????? ???????
????????? ???????? "?? ???", // ?? ?????????
????????? "???? ????" if (from ! data)
mergeSection(from, 0, n, from, n, n, data,
0)
10
7. ????????????? ?????????? (?????????? ?????)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
0
1
3
4
8
10
14
16
21
24
31
33
36
38
42
44
50
27
51
53
59
4
0
27
51
1
2
14
31
42
1
3
4
5
6
8
24
3
59
33
44
53
16
7
8
9
10
11
12
13
14
10
38
50
21
36
15
16
17
18
19
? ??? ?????
11
7. ????????????? ?????????? (???????????)
59
0
51
53
1
2
42
44
50
36
3
4
5
6
27
14
31
1
24
33
16
38
7
8
9
10
11
12
13
14
4
8
3
10
21
15
16
17
18
19
? ??? ?????
12
???????? ????????????? ??????????
public static void heapSort(int data) int
n data.length // ????? ???????
buildPyramid // ?????????? ????????
for (int i 1 i lt n i) // Inv
???????? data0i-1 ??? ???????? ????????
int c datai // "??????????????"
???????? int p i, q // ???????
??? "?????????????" ????? ? ??????? do
q p if ((p (q-1) gtgt 1) gt 0
datap lt c) dataq datap else
dataq c
continue buildPyramid
while (true) meltPyramid
// ??????????? ?????????? ???????? for (int i
n-1 i gt 0 i--) int c datai
datai data0 int q, p 0
// ??????? ??? ????????????? do q p
p (q ltlt 1) 1 if (p
gt i) // ????? ?? ??????? ????????
dataq c continue
meltPyramid if (p lt
i-1 datap1 gt datap) p if
(datap gt c) dataq datap
else dataq c
continue meltPyramid
while (true)
13
8. ??????? ??????????
1
3
4
8
10
14
16
21
24
31
33
36
38
42
44
50
27
51
53
59
14
?????????? ? ????????? ?????????
  • ?????? ??????? ? ??????????? ????? ??????????
    ???????? ? ????????????
  • ???????, ??????? ????????????? ???????,
    ??????????? ???????? ?????????
  • ?????? ?????????? ??????????? ??????? ???????? ?
    ????????? ?????????????.

15
????????? ??????? ??????????
public static void quickSort(int data)
quickSort(data, 0, data.length-1) private
static void quickSort(int data, int from, int
to) if (to-from lt 50) // ?????????
????????? ??????? ??????????? ??????? ???????
??????? simpleSort(data, from, to)
else int c datafrom // ????????
????????? ??????? // ????????????
???????? ??????? ?? ???????? ??????? ? ???????
c. int low from, high to1
// Inv (low lt high) datafrom(low-1) lt c
datahighto gt c while (low lt high)
while (low lt high data--high
gt c) datalow datahigh
while (low lt high datalow lt c)
datahigh datalow
// ????????? ??????? ?? ???? ?????
datalow c // ?????????? ?????????
??????? ? ?????? ???????? ???????
quickSort(data, from, low-1)
quickSort(data, low1, to)
16
9. ????? a-???????
public static int mediana(int data, double
alpha) int n data.length //
????? ??????? int m (int)(alpha(n-1))
// ????? alpha-??????? ? ????????????? ???????
// ???? ??????? ????? m ? ??????? ??????? ?
????????? ?? low ?? high. // ??? ?????
???????? ???????????? ??????? ? ????? ?????? ??
??? ????? ???, // ??? ??? ???????? ?
????????? ??????? ??????????. int low 0,
high n-1 // ??????? ??????????????? ???????
??????? do int c datalow //
????????? ??????? int ndxLow low,
ndxHigh high // "????????????" ???????
// ???? ?????????? ????????? ?? ??????? c ?
??????? c. while (ndxLow lt ndxHigh)
while (ndxLow lt ndxHigh datandxHigh
gt c) ndxHigh-- datandxLow
datandxHigh while (ndxLow lt
ndxHigh datandxLow lt c) ndxLow
datandxHigh datandxLow
// ????? ?????????? ????? ???????? c.
datandxLow c if (ndxLow m) return
datam // ?????????? ????? ? ????? ??
???? ??????? ??????? if (m lt ndxLow) high
ndxLow-1 else low ndxLow1 while
(true)
17
10. ?????????? ?????????
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
0
1
3
4
8
0
4
6
1
4
1
3
6
8
2
4
0
7
1
3
9
18
????????? ??? ?????????? ?????????
public static void countSort (int src, int
dest) // ??????????????, ??? ??? ????????
srci ?? ????????? 0..15 int n src.length
// ????? ??????? int count new
int16 // ?????? ??? ???????? ?????????
// 1. ??????? for (int i 0 i lt n i)
countsrci // 2.
???????????? for (int i 1 i lt 16 i)
counti counti-1 // 3.
??????????? for (int i n-1 i gt 0 i--)
dest--countsrci srci
19
11. ???????? ??????????
1
3
4
8
10
14
16
21
24
31
33
36
39
42
44
50
27
51
53
59
1
3
4
8
10
14
16
21
24
31
33
36
39
42
44
50
27
51
53
59
20
????????? ??? ???????? ??????????
public static void digitSort(int data)
int n data.length // ????? ???????
int data2 new intn // ???????????????
?????? for (int step 0 step lt 8 step)
// step - ????? "?????" // ??????????
"?????????" ?? ????? ? ???????? ???????
countSort(data, step, data2) // ??????
????????? ?? ??????? int temp data
data data2 data2 temp private
static void countSort (int src, int nDig, int
dest) int n src.length // ?????
??????? int count new int16 //
?????? ??? ???????? ????????? // 1. ???????
nDig ltlt 2 for (int i 0 i lt n i)
count(srci (0xF ltlt nDig)) gtgt nDig
// 2. ???????????? for (int i 1 i
lt 16 i) counti counti-1
// 3. ??????????? for (int i n-1 i
gt 0 i--) dest--count(srci (0xF
ltlt nDig)) gtgt nDig srci
Write a Comment
User Comments (0)
About PowerShow.com