Sorts and Algo Rhythmic
public class Sorts {
//bubble
public void Bubble(int arr[]) {
int n = arr.length;
int t;
for (int j = 0; j < n - 1; j++) {
for (int i = 0; i < n - 1 - j; i++) {
if (arr[i] > arr[i + 1]) {
t = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = t;
}
}
}
}
//selection
public void Selection(int arr[]) {
int n = arr.length;
int t;
int min;
int k;
for (int j = 0; j < n - 1; j++) {
min = arr[j];
k = j;
for (int i = j + 1; i < n; i++) {
if (min > arr[i]) {
min = arr[i];
k = i;
}
}
if (k != j) {
t = arr[j];
arr[j] = arr[k];
arr[k] = t;
}
}
}
//insertion
public void Insertion(int arr[]) {
}
public static void main(String args[]) {
Sorts obj = new Sorts();
int arr[] = {2, 1, 8, 4, 7};
obj.Bubble(arr);
// obj.Selection(arr);
int n = arr.length;
System.out.println("Bubble:");
for (int i = 0; i < n; ++i) {
System.out.print(arr[i] + " ");
}
System.out.println();
Sorts obj2 = new Sorts();
int arr2[] = {2, 1, 8, 4, 7};
obj.Selection(arr2);
int m = arr2.length;
System.out.println("Selection:");
for (int i = 0; i < m; ++i) {
System.out.print(arr2[i] + " ");
}
System.out.println();
}
}
Sorts.main(null);