Skip to content

Commit 3e796a6

Browse files
Create ArrayADT.java
1 parent 040825a commit 3e796a6

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

ArrayADT.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
/*
3+
Linear search Function will return position if found,
4+
otherwise it returns -1.
5+
6+
Here ADT - Abstract Data type nothing but a class
7+
8+
*/
9+
10+
public class ArrayADT
11+
{
12+
public static void main(String args[])
13+
{
14+
int a[] = {23,34,43,54,5,45,65,67,77,66,88,87,76};
15+
int position,e;
16+
17+
e = Integer.parseInt(args[0]);
18+
19+
position = linearSearch(a,a.length,e);
20+
21+
if(position==-1)
22+
System.out.println("No key found");
23+
else
24+
System.out.println("Key found at location:"+position);
25+
}
26+
27+
public static int linearSearch(int b[],int n, int key)
28+
{
29+
for (int i=0;i<n;i++)
30+
{
31+
if(b[i] == key)
32+
return i;
33+
}
34+
return -1;
35+
}
36+
}
37+
38+
39+
40+
41+

0 commit comments

Comments
 (0)