-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSearchAndSortStrings.java
More file actions
191 lines (172 loc) · 6.83 KB
/
SearchAndSortStrings.java
File metadata and controls
191 lines (172 loc) · 6.83 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import java.util.ArrayList;
//note: these methods will all be STATIC, and will input an array as a parameter
//instead of an instance field/
//The purpose of this class is to apply our search and sort algorithms to Strings not numbers.
//Unless otherwise specified, the sort methods should always be sorted in alphabetical order.
public class SearchAndSortStrings {
// returns the index where target is located in words, -1 if target is not found
public static int linearSearchStrings(String[] words, String target) {
for (int i = 0; i < words.length; i++) {
if (words[i].equals(target)) {
return i;
}
}
return -1;
}
// use the binary Search algorithm to return the index where target is located
// return -1 if target is not found. NOTE the parameter is an ArrayList not an
// array.
public static int binarySearchStrings(ArrayList<String> wordList, String target) {
int l = 0;
int r = wordList.size() - 1;
int m = (l + r) / 2;
while (l <= m) {
m = (l + r) / 2;
if (wordList.get(m).equals(target)) {
return m;
}
if (wordList.get(m).compareTo(target) >= 1) {
r = m - 1;
} else {
l = m + 1;
}
}
return -1;
}
// use the bubble sort algorithm to return an array that is a sorted version of
// words.
public static String[] bubbleSortStrings(String[] words) {
boolean temp = true;
while (temp) {
temp = false;
for (int i = 0; i < words.length - 1; i++) {
if (words[i].compareTo(words[i + 1]) >= 1) {
String tempString = words[i];
words[i] = words[i + 1];
words[i + 1] = tempString;
temp = true;
}
}
}
return words;
}
// use the selection sort algorithm to return an ArrayList that is a sorted
// version of wordList.
public static ArrayList<String> selectionSortStrings(ArrayList<String> wordList) {
int index ;
for(int i =0;i<wordList.size();i++){
String min = wordList.get(i);
index = i;
for(int j =i ;j<wordList.size();j++){
if(min.compareTo(wordList.get(j))>=1){
min = wordList.get(j);
index = j;
}
}
String temp = wordList.get(i);
wordList.set(i,min);
wordList.set(index,temp);
}
return wordList;
}
// use the insertion sort algorithm to return an array that is a sorted version
// of words.
public static String[] insertionSortStrings(String[] words) {
// your code goes here
int j = 0 ;
for (int i = 1; i < words.length; i++) {
j = i;
String why = words[i];
while (j > 0 && words[j - 1].compareTo(why) > 0) {
words[j] = words[j-1];
j--;
}
words[j] = why;
}
return words;
}
// FINALLY, for this last method... use ANY SORTING ALGORITHM YOU WANT to sort
// the String array...
// but this time, don't sort alphabetically, sort by the length() of each of the
// String elements!
public static String[] sortStringLength(String[] words) {
// your code goes here
int index = 0;
for(int i =0;i<words.length;i++){
int temp = words[i].length();
index = i;
for(int j = i;j<words.length;j++){
if(temp>words[j].length()){
temp = words[j].length();
index = j;
}
}
String temp2 = words[i];
words[i] = words[index];
words[index] = temp2;
}
return words;
}
// Here is the main method - run it, output SHOULD MATCH the Expected Output
// provided at the end
public static void main(String[] args) {
String[] testWords1 = { "luuuuuke", "i", "am", "your", "father" };
ArrayList<String> testWords2 = new ArrayList<String>();
testWords2.add("apple");
testWords2.add("banana");
testWords2.add("cookie");
testWords2.add("dessert");
String[] testWords3 = { "how", "bout", "them", "apples" };
ArrayList<String> testWords4 = new ArrayList<String>();
testWords4.add("hope");
testWords4.add("is");
testWords4.add("a");
testWords4.add("dangerous");
testWords4.add("thing");
String[] testWords5 = { "not", "all", "who", "wander", "are", "lost" };
System.out.println("testing the linear search with testWords1:");
System.out.println("\"your\" is at index " + linearSearchStrings(testWords1, "your")
+ ", and \"mother\" is at index " + linearSearchStrings(testWords1, "mother") + "\n");
System.out.println("testing the binary search with testWords2:");
System.out.println("\"banana\" is at index " + binarySearchStrings(testWords2, "banana")
+ ", and \"pizza\" is at index " + binarySearchStrings(testWords2, "pizza") + "\n");
System.out.println("\ntesting the bubble sort with testWords3:");
String[] bubbleSorted = bubbleSortStrings(testWords3);
for (String s : bubbleSorted)
System.out.print(s + " | ");
System.out.println("\n\ntesting the selection sort with testWords4:");
ArrayList<String> selectionSorted = selectionSortStrings(testWords4);
for (String s : selectionSorted)
System.out.print(s + " | ");
System.out.println("\n\ntesting the insertion sort with testWords5:");
String[] insertionSorted = insertionSortStrings(testWords5);
for (String s : insertionSorted)
System.out.print(s + " | ");
System.out.println("\n\ntesting the sortStringLength sort with testWords4:");
String[] lengthSorted = sortStringLength(testWords1);
for (String s : lengthSorted)
System.out.print(s + " | ");
}
}
/*
* EXPECTED OUTPUT
* testing the linear search with testWords1:
* "your" is at index 3, and "mother" is at index -1
*
* testing the binary search with testWords2:
* "banana" is at index 1, and "pizza" is at index -1
*
*
* testing the bubble sort with testWords3:
* apples | bout | how | them |
*
* testing the selection sort with testWords4:
* a | dangerous | hope | is | thing |
*
* testing the insertion sort with testWords5:
* all | are | lost | not | wander | who |
*
* testing the sortStringLength sort with testWords4:
* i | am | your | father | luuuuuke |
*
*/