public class Demo {
public static void main(String[] args) {
// int[] arr = { 50, 54, 97, 54, 60, 51 };
// int[] arr1 = { 12, 43, 34, 50 };
int[] arr2 = { 50, 54, 97, 54, 60, 51, 12, 43, 34, 50 };
sort(arr2);
for (int i = 0; i < arr2.length; i++) {
System.out.print(arr2[i] + "\t");
}
}
public static void sort(int[] arrInt) {
for (int i = 0; i < arrInt.length - 1; i++) {
for (int j = i + 1; j < arrInt.length; j++) {
if (Math.abs(arrInt[i] - 50) >= Math.abs(arrInt[j] - 50))
swap(arrInt, i, j);
}
}
}
public static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
} |
|