-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursionBinarySearchApp.java
More file actions
94 lines (93 loc) · 3.76 KB
/
RecursionBinarySearchApp.java
File metadata and controls
94 lines (93 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// binarySearch.java
// Рекурсивный двоичный поиск
////////////////////////////////////////////////////////////////
class ordArray
{
private long[] a; // Ссылка на массив a
private int nElems; // number of data items
//-----------------------------------------------------------
public ordArray(int max) // Конструктор
{
a = new long[max]; // Создание массива
nElems = 0;
}
//-----------------------------------------------------------
public int size()
{ return nElems; }
//-----------------------------------------------------------
public int find(long searchKey)
{
return recFind(searchKey, 0, nElems-1);
}
//-----------------------------------------------------------
private int recFind(long searchKey, int lowerBound,
int upperBound)
{
int curIn;
curIn = (lowerBound + upperBound ) / 2;
if(a[curIn]==searchKey)
return curIn; // Элемент найден
else if(lowerBound > upperBound)
return nElems; // Элемент не найден
else // Деление диапазона
{
if(a[curIn] < searchKey) // В верхней половине
return recFind(searchKey, curIn+1, upperBound);
else // В нижней половине
return recFind(searchKey, lowerBound, curIn-1);
}
}
//-----------------------------------------------------------
public void insert(long value) // Сохранение элемента в массиве
{
int j;
for(j=0; j<nElems; j++) // Определение позиции
if(a[j] > value) // (линейный поиск)
break;
for(int k=nElems; k>j; k--) // Перемещение элементов
a[k] = a[k-1]; // с большим значением ключа
a[j] = value; // Вставка
nElems++; // Увеличение размера
}
//-----------------------------------------------------------
public void display() // Вывод содержимого массива
{
for(int j=0; j<nElems; j++) // Для каждого элемента
System.out.print(a[j] + " "); // Вывод текущего элемента
System.out.println("");
}
//-----------------------------------------------------------
} // Конец класса ordArray
////////////////////////////////////////////////////////////////
class RecursionBinarySearchApp
{
public static void main(String[] args)
{
int maxSize = 100; // Размер массива
ordArray arr; // Ссылка на массив
arr = new ordArray(maxSize); // Создание массива
arr.insert(72); // Вставка элементов
arr.insert(90);
arr.insert(45);
arr.insert(126);
arr.insert(54);
arr.insert(99);
arr.insert(144);
arr.insert(27);
arr.insert(135);
arr.insert(81);
arr.insert(18);
arr.insert(108);
arr.insert(9);
arr.insert(117);
arr.insert(63);
arr.insert(36);
arr.display(); // Вывод содержимого массива
int searchKey = 27; // Поиск элемента
if( arr.find(searchKey) != arr.size() )
System.out.println("Found " + searchKey);
else
System.out.println("Can’t find " + searchKey);
}
} // Конец класса BinarySearchApp
////////////////////////////////////////////////////////////////