Posted at

Javaでバブルソート

More than 3 years have passed since last update.


BubbleSort.java

import java.util.Arrays;

public class BubbleSort {
public static void main(String[] args) {
int[] arr = {3, 2, 1, 8, 6, 9, 7, 0, 5, 4};
Arrays.stream(bubbleSort(arr)).forEach(e -> System.out.println(e));
}

public static int[] bubbleSort(int[] arr) {
for(int i=arr.length-1; i>0; i--) {
for(int j=0;j<i;j++) {
if(arr[j] > arr[j+1]) {
int tmp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = tmp;
}
}
}
return arr;
}
}