EbbeVang
3/10/2016 - 11:06 AM

bubblesort

package com.company;

import java.util.ArrayDeque;
import java.util.Arrays;

public class Main {

    public static void main(String[] args) {

        String[] messy = {"Mikkel", "Ebbe", "Liv"};
        GenericSort<String> genericSort = new GenericSort<String>();
        String[] orderedArray = genericSort.bubblesort(messy);
        System.out.println(Arrays.toString(orderedArray));

        //int[] messyArray = {2,43,7,1,5,89,12,3,1,1234};
        //int[] orderedArray = bubblesort(messyArray);
        //System.out.println("ordered array: "+ Arrays.toString(orderedArray));

    }

    public static int[] bubblesort(int[] array)
    {
        System.out.println(Arrays.toString(array));

        for (int i = 0; i < array.length-1; i++) {
            for (int j = 0; j < array.length-1 ; j++) {
                if (array[j] > array[j+1])
                {
                    swap(array, j);

                }

                System.out.println(Arrays.toString(array));
            }
        }
        return array;
    }

    private static void swap(int[] array, int j) {
        //Switch elements
        int temp = array[j];
        array[j] = array[j+1];
        array[j+1] = temp;
    }
}