public class Exercise {
public static void main(String[] args) {
int[] array = {2,333,-40,1,5,21,0,10,99,-26};
System.out.println("my normal array is: " + Arrays.toString(array));
bubbleSort(array);
System.out.println();
System.out.println("After Sorting From Small number to Large number. ");
System.out.println(Arrays.toString(array));
sort(array);
System.out.println();
System.out.println("After Sorting From Large number to Small number: ");
System.out.println(Arrays.toString(array));
}
public static void bubbleSort(int[] array) {
for(int i=0; i<array.length; i++) {
for(int j=0; j<array.length-1-i; j++) {
if(array[j] > array[j+1]) {
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
}
public static void sort(int[] array) {
for(int i=0; i<array.length; i++) {
for(int j=0; j<array.length-1-i; j++) {
if(array[j] < array[j+1]) {
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
}
}
//OUTPUT:
my normal array is: [2, 333, -40, 1, 5, 21, 0, 10, 99, -26]
After Sorting From Small number to Large number.
[-40, -26, 0, 1, 2, 5, 10, 21, 99, 333]
After Sorting From Large number to Small number:
[333, 99, 21, 10, 5, 2, 1, 0, -26, -40]
Comments