From this time, I will post about sorting.
BubbleSort.java
public class BubbleSort {
public static void sort(int[] array) {
for(int i=0;i<array.length;i++) {
for (int j=0;j<array.length-1;j++) {
if(array[j]>array[j+1]) {
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
}
public static void main(String args[]) {
int[] array = {5,4,3,2,1};
sort(array);
for(int i=0;i<array.length;i++) {
System.out.print(array[i]);
}
}
}
This time I tried bubble sort. Next time I will post a selection sort.
Recommended Posts