Skip to content
This repository was archived by the owner on Apr 15, 2024. It is now read-only.

Commit 24df5d3

Browse files
authored
Add data structures
Create files for data structure implementation in Java
1 parent 5b0ff91 commit 24df5d3

10 files changed

+1798
-0
lines changed

BasicArray.java

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import java.util.Scanner;
2+
3+
// implementation of arrays and searching with linearSearch and binarySearch
4+
5+
public class BasicArray {
6+
7+
static int[] array;
8+
static int size;
9+
10+
public BasicArray() {
11+
12+
}
13+
14+
public BasicArray(int[] array, int size) {
15+
BasicArray.array = array;
16+
BasicArray.size = size;
17+
}
18+
19+
public int searchLinear(int input) {
20+
21+
for(int i=0;i<size;i++) {
22+
if(array[i] == input)
23+
return i+1;
24+
}
25+
26+
return -1;
27+
}
28+
29+
public static int binarySearch(int input) {
30+
31+
int low = 0;
32+
int high = size-1;
33+
int curIn;
34+
35+
while(true) {
36+
37+
curIn = (low+high)/2;
38+
39+
if(array[curIn]==input)
40+
return curIn+1;
41+
42+
else if(array[curIn]<input) {
43+
low = curIn+1;
44+
}
45+
46+
else if(array[curIn]>input) {
47+
high = curIn -1;
48+
}
49+
50+
}
51+
52+
}
53+
54+
55+
public static void displayArray() {
56+
57+
58+
for(int i=0;i<size;i++)
59+
System.out.print(array[i]+" | ");
60+
61+
}
62+
63+
public void setSize(int size) {
64+
BasicArray.size = size;
65+
}
66+
67+
public static void main(String[] args) {
68+
69+
Scanner scanner = new Scanner(System.in);
70+
71+
System.out.println("Enter the array elements");
72+
73+
74+
int[] strings = new int[10];
75+
76+
for(int i=0;i<strings.length;i++) {
77+
strings[i]= scanner.nextInt();
78+
scanner.nextLine();
79+
}
80+
81+
displayArray();
82+
83+
84+
BasicArray arr = new BasicArray(strings,10);
85+
86+
87+
int k = binarySearch(5);
88+
89+
90+
System.out.println(k);
91+
92+
scanner.close();
93+
94+
}
95+
96+
}

0 commit comments

Comments
 (0)