-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BinarySearch, QuickSort and City classes
- Loading branch information
Showing
11 changed files
with
10,281 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import java.io.BufferedReader; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
import java.util.StringTokenizer; | ||
|
||
public class BinarySearch { | ||
public static int getIndex(Comparable key, Comparable[] array) { | ||
int low = 0; | ||
int high = array.length; | ||
|
||
while(high >= low) { | ||
int mid = (high + low) / 2; | ||
|
||
if(key.compareTo(array[mid]) > 0) | ||
low = mid + 1; | ||
else if(key.compareTo(array[mid]) < 0) | ||
high = mid - 1; | ||
else | ||
return mid; | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
public static void main(String[] args) { | ||
if(args.length < 2) { | ||
System.out.println("\n\nUsage: java BinarySearch file1 file2" ); | ||
System.exit(1); | ||
} | ||
|
||
try { | ||
// Reading first file | ||
FileReader in = new FileReader(args[0]); | ||
BufferedReader br = new BufferedReader(in); | ||
int size = Integer.parseInt(br.readLine()); | ||
City[] cities = new City[size]; | ||
|
||
String line; | ||
StringTokenizer st; | ||
|
||
for(int i = 0; i < size; i++) { | ||
line = br.readLine(); | ||
st = new StringTokenizer(line); | ||
|
||
cities[i] = new City(st.nextToken(), Float.parseFloat(st.nextToken())); | ||
} | ||
|
||
in.close(); | ||
br.close(); | ||
|
||
// Sorting | ||
Sort sort = new QuickSort(); | ||
sort.sort(cities); | ||
|
||
// Second file | ||
in = new FileReader(args[1]); | ||
br = new BufferedReader(in); | ||
size = Integer.parseInt(br.readLine()); | ||
|
||
for(int i = 0; i < size; i++) { | ||
line = br.readLine(); | ||
st = new StringTokenizer(line); | ||
String name = st.nextToken(); | ||
|
||
int position = BinarySearch.getIndex(new City(name, 0), cities); | ||
|
||
if(position < 0) | ||
System.out.println("[Failed] " + name + "wasn't found."); | ||
else | ||
System.out.println("[Ok] " + name + " was found at position " + position + "."); | ||
} | ||
|
||
}catch(IOException err) {} | ||
|
||
} | ||
|
||
} | ||
|
||
|
||
|
||
|
||
|
||
|
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
public class City implements Comparable<City>{ | ||
private String name; | ||
private float temperature; | ||
|
||
public City(String name, float temperature) { | ||
this.name = name; | ||
this.temperature = temperature; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public float getTemperature() { | ||
return temperature; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public void setTemperature(float temperature) { | ||
this.temperature = temperature; | ||
} | ||
|
||
@Override | ||
public int compareTo(City other) { | ||
return name.compareTo(other.getName()); | ||
} | ||
|
||
public String toString() { | ||
String message = "The city " + name + " is measuring " | ||
+ temperature + "°"; | ||
return message; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
10 | ||
a -114 | ||
AAA -100 | ||
AAAS -140 | ||
Aarhus 162 | ||
Aaron -234 | ||
AAU -171 | ||
ABA 44 | ||
Ababa -193 | ||
aback -148 | ||
abacus 5 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
100 | ||
Aaron -234 | ||
Abramson -200 | ||
able 134 | ||
abc -267 | ||
aboriginal -242 | ||
abrasive 54 | ||
abed -50 | ||
abdicate 167 | ||
abridgment 197 | ||
AAAS -140 | ||
abet -23 | ||
abdominal -114 | ||
abovementioned -131 | ||
abhorred 21 | ||
abrupt 94 | ||
abhorrent 0 | ||
abolition -110 | ||
abater -52 | ||
abbas 185 | ||
absorption -110 | ||
abduct -19 | ||
abbot -190 | ||
abnormal -233 | ||
abscissae 26 | ||
Abernathy 146 | ||
absorb 119 | ||
Abe -138 | ||
abject 32 | ||
aborning -268 | ||
Abelson -147 | ||
Abigail -96 | ||
Aarhus 162 | ||
abort 29 | ||
abrasion -160 | ||
abeyant -7 | ||
absenteeism -121 | ||
Abo -8 | ||
aboveboard 128 | ||
absent 95 | ||
abide 45 | ||
aborigine -272 | ||
absinthe -47 | ||
absentminded 106 | ||
abode 122 | ||
abolish 1 | ||
ABA 44 | ||
aboveground -249 | ||
abeyance 103 | ||
absentee -15 | ||
abroad 7 | ||
abetting -58 | ||
abetted -270 | ||
about 87 | ||
abalone 168 | ||
aback -148 | ||
abate -133 | ||
abreact 39 | ||
abound -266 | ||
abbreviate 144 | ||
absentia 60 | ||
AAU -171 | ||
Abelian 57 | ||
aberrate 59 | ||
Abraham 182 | ||
a -114 | ||
abscess 198 | ||
abbey -256 | ||
abash 92 | ||
abdomen -7 | ||
abominate 26 | ||
Abbott 170 | ||
Abner -75 | ||
abridge -133 | ||
Abidjan -164 | ||
abbe -73 | ||
abrogate -183 | ||
Abram 188 | ||
abase -38 | ||
Aberdeen 136 | ||
above -59 | ||
absolute 28 | ||
abominable -42 | ||
absolve -210 | ||
abscissa -61 | ||
Ababa -193 | ||
ablaze 58 | ||
ablution -28 | ||
aberrant 68 | ||
AAA -100 | ||
absence 67 | ||
abrade 143 | ||
abreast -241 | ||
absorptive -166 | ||
Abel -170 | ||
absolution -265 | ||
absorbent 108 | ||
abandon -45 | ||
ablate -200 | ||
aboard -121 | ||
abacus 5 |
Oops, something went wrong.