public class BubbleSort {
public static void sort(int[] array)
{
int tmp;
int lastExchangeIndex = 0;
int sortBorder = array.length - 1;
for(int i = 0; i < array.length; i++)
{
boolean isSorted = true;
for(int j = 0; j < sortBorder; j++)
{
if(array[j] > array[j+1])
{
tmp = array[j];
array[j] = array[j+1];
array[j+1] = tmp;
isSorted = false;
lastExchangeIndex = j;
}
}
sortBorder = lastExchangeIndex;
if(isSorted){
break;
}
}
}
public static void main(String[] args){
int[] array = new int[]{3,4,2,1,5,6,7,8};
sort(array);
System.out.println(Arrays.toString(array));
}
}