Repository navigation
Expand file tree
/
Copy pathcontact_sorter.java
More file actions
138 lines (105 loc) · 3.68 KB
/
Copy pathcontact_sorter.java
File metadata and controls
138 lines (105 loc) · 3.68 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package dsa_project;
import java.util.*;
public class contact_sorter {
static final Scanner sc = new Scanner(System.in);
static String[] quickStrings(String[] s) {
if (s.length <= 1)
return s;
String pivot = s[0];
ArrayList<String> left = new ArrayList<>();
ArrayList<String> right = new ArrayList<>();
int i = 1;
while (i < s.length) {
if (s[i].compareTo(pivot) < 0) {
left.add(s[i]);
} else {
right.add(s[i]);
}
i++;
}
String[] leftStrings = quickStrings(toArrStr(left));
String[] rightStrings = quickStrings(toArrStr(right));
int k = 0, l = 0, r = 0;
while (l < leftStrings.length) {
s[k] = leftStrings[l];
l++;
k++;
}
s[k] = pivot;
k++;
while (r < rightStrings.length) {
s[k] = rightStrings[r];
r++;
k++;
}
return s;
}
static String[] toArrStr(List list) {
String[] res = new String[list.size()];
for (int i = 0; i < list.size(); i++) {
res[i] = (String) list.get(i);
}
return res;
}
static String[] binarySearch(String[] s) {
System.out.print("#################################################################\n"
+ "Choice 1: Search name.\n" + "Choice 2: Exit.\n" + "choice: ");
int choice = sc.nextInt();
String user_search = null;
while (true) {
if (choice == 1) {
System.out.print("Search Name: ");
user_search = sc.next();
int low = 0, high = s.length - 1;
boolean found = false;
while (low <= high) {
int mid = (low + high) / 2;
if (s[mid].compareTo(user_search) == 0) {
found = true;
System.out.println("Name Found: %s, at index: ".formatted(found) + mid);
break;
} else if (s[mid].compareTo(user_search) < 0) {
low = mid + 1;
} else {
high = mid - 1;
}
}
if (!found) {
System.out.print("Name not Found: %s, at any index \n".formatted(found));
}
System.out.print("#################################################################\n"
+ "Choice 1: Search name.\n" + "Choice 2: Exit.\n" + "choice: ");
choice = sc.nextInt();
} else {
break;
}
}
return s;
}
public static void main(String[] args) {
System.out.print("#################################################################\n"
+ "Choice 1: To Enter name.\n" + "Choice 2: To Exit.\n" + "choice: ");
int choice = sc.nextInt();
String user_input = null;
List name = new ArrayList();
while (true) {
if (choice == 1) {
System.out.print("enter name: ");
user_input = sc.next();
if (!user_input.equals("exit")) {
name.add(user_input);
}
} else {
break;
}
System.out.print("choice: ");
choice = sc.nextInt();
}
System.out.println(name);
String s[] = new String[name.size()];
for (int i = 0; i < name.size(); i++) {
s[i] = (String) name.get(i);
}
binarySearch(quickStrings(s));
}
}