-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathSearches.java
More file actions
44 lines (40 loc) · 1.09 KB
/
Searches.java
File metadata and controls
44 lines (40 loc) · 1.09 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
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package array.visualizer.utils;
import array.visualizer.ArrayController;
import static array.visualizer.ArrayVisualizer.*;
/**
*
* @author S630690
*/
public class Searches {
public static void linearSearch(final ArrayController ac, int find) throws Exception {
for(int i = 0; i < ac.length; i++){
ac.aa++;
ac.comps++;
if(ac.array[i]==find)
break;
ac.marked.set(0, i);
sleep(5);
}
sleep(1000);
}
public static void binarySearch(final ArrayController ac, int find) throws Exception {
int at = ac.length/2;
int change = ac.length/4;
while(ac.array[at]!=find && change > 0){
ac.marked.set(0, ac.array[at]);
ac.comps+=2;
ac.aa++;
Thread.sleep(1000);
if(ac.array[at]<find)
at += change;
else
at -= change;
change /= 2;
}
sleep(1000);
}
}