-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringOpsLauncher.java
More file actions
95 lines (74 loc) · 2.67 KB
/
Copy pathStringOpsLauncher.java
File metadata and controls
95 lines (74 loc) · 2.67 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
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
public class StringOpsLauncher {
public static String [] getBigArray() throws FileNotFoundException {
// create a Scanner that reads from a file, instead of the keyboard
Scanner s = new Scanner(new FileReader("unsrtunique.txt"));
// read the first "word" (an int specifying the # of words to follow)
int length = s.nextInt();
// read the remaining part of the line (and discard it)
s.nextLine();
String [] a = new String[length]; // create a new array to hold the file data
// read the strings from the file into our array
int i = 0;
while(i < length && s.hasNextLine()) {
a[i] = s.nextLine().trim();
i += 1;
}
// ensure we didn't bail in an unexpected fashion
if (i < length) System.err.println("Error? - file ended prematurely");
if (s.hasNextLine()) System.err.println("Error? - file longer than expected");
return a;
}
/**
* @return a copy of a short String array
*/
public static String [] getSmallArray() {
String [] array = {"cat", "dog", "ape"};
// don't return array itself, but instead return a copy of it.
// that way, we can get another copy later and be sure it hasn't been
// reordered or whatnot.
String [] toreturn = new String[array.length];
for(int i = 0; i < array.length; i++) {
toreturn[i] = array[i];
}
return toreturn;
}
/**
* Print an array (helpful for debugging)
* @param array - the array to print
*/
public static void print(String [] array, int maxitems) {
System.out.println("-----------------");
for(int i = 0; i < maxitems; i++) {
System.out.println( " ["+i+"]\t-- " + array[i]);
}
System.out.println("-----------------");
}
public static void main(String [] args) throws FileNotFoundException {
System.out.println("A compare to B ==> " + ("A".compareTo("B"))); // -1
System.out.println("B compare to A ==> " + ("B".compareTo("A"))); // 1
System.out.println("-------------------");
StringOps ops = new StringOps();
String [] array = getSmallArray();
String [] arrayOne = getBigArray();
String query = "chinese";
System.out.println("Unsorted array:");
print(array, array.length);
ops.swapemSort(array);
System.out.println("Sorted array:");
print(array, array.length);
int indexer = ops.linearSearch(arrayOne, query, 1, arrayOne.length-1);
System.out.println("Test Array:");
print(arrayOne, 20);
System.out.println(indexer);
array = getBigArray();
ops.swapemSort(array);
print(array, 20);
System.out.println(ops.binarySearch(array, "chinese", 0, array.length));
arrayOne = getSmallArray();
ops.insertSort(arrayOne);
print(arrayOne,3);
}
}