Get a string from standard input
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader buffer = new BufferedReader(isr);
String s = buffer.readLine();
Type cast a String to int
String str = "123";
Integer.parseInt(str);
Type cast a String to double
String str = "123.45";
Double D = Double.valueOf(str);
double d = D.doubleValue();
Bubble Sort
public void bubbleSort(int[] arr) {
boolean swapped = true;
for (int j = 1; swapped; j++){
swapped = false;
for (int i = 0; i < arr.length - j; i++) {
if (arr[i] > arr[i + 1]) {
int tmp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = tmp;
swapped = true;
}
}
}
}
Selection Sort
public void selectionSort(int[] arr){
for(int j=0; j<arr.length-1; j++){
int min = j;
for(i=j+1; i<arr.length; i++)
if(arr[i] < arr[min])
min = in;
swap(arr[j],arr[min]);
}
}
No comments:
Post a Comment