/**
 * Created by alexanderb on 15/09/15.
 */

import java.util.List;

public class BubbleSort implements AlgorithmSort {

    public void sort(List<Integer> list, Order order) {
        int listSize = list.size();
        boolean swap = true;

        while(swap){
            swap = false;
            for (int i = 1; i < listSize; i++) {
                swap = isSwap(list, swap, i, order);
            }
        }
    }

    private boolean isSwap(List<Integer> list, boolean swap, int i, Order order) {
        if (list.get(i - 1) <= list.get(i) && order.equals(Order.ASCENDING) ||
                list.get(i - 1) >= list.get(i) && order.equals(Order.DESCENDING)) {
            return swap;
        }
        swap(list, i);
        return true;
    }

    private void swap(List<Integer> list, int i) {
        Integer elementAfter = list.get(i - 1);
        list.set(i - 1, list.get(i));
        list.set(i, elementAfter);
    }


}
