All Downloads are FREE. Search and download functionalities are using the official Maven repository.

utils.Sorting Maven / Gradle / Ivy

Go to download

Extension of classic Java data structures that avoid the use of Stream abstraction in favour of direct list transformation methods

The newest version!
package utils;

import structures.ArrayList;

import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
/**
 * Created by mikedev on 01/08/16.
 */
public class Sorting {

    private static  ArrayList bubbleSort(Collection collection, Comparator comparator, int order)
    {
        if(!(order == -1 || order == 0 || order == 1)){
            throw new IllegalArgumentException("Order value can be only -1,0,1");
        }
        ArrayList orderedList = new ArrayList();
        T[] unorderedArray = (T[]) new Object[collection.size()];
        collection.toArray(unorderedArray);
        boolean flagStop = true;
        for(int orderedElements = 0; orderedElements < unorderedArray.length; orderedElements++)
        {
            flagStop = true;
            for(int j = 1 ; j < unorderedArray.length; j++)
            {
                int first = j - 1;
                int second = j;
                int cResult = comparator.compare(unorderedArray[first], unorderedArray[second]);
                if(cResult == order)
                {
                    flagStop = false;
                    //swap
                    T tmp = unorderedArray[first];
                    unorderedArray[first] =  unorderedArray[second];
                    unorderedArray[second] = tmp;
                }
            }
            if(flagStop){
                break;
            }
        }

        for(T e: unorderedArray){
            orderedList.add(e);
        }
        return orderedList;
    }

    public static  ArrayList bubbleSortDecrescent(Collection collection, Comparator comparator)
    {
       return bubbleSort(collection, comparator, -1);
    }

    public static  ArrayList bubbleSort(Collection collection, Comparator comparator)
    {
        return bubbleSort(collection, comparator, 1);
    }

    private static  void bubbleSort(Collection collection, Collection dest, Comparator comparator, int order)
    {
        if(!(order == -1 || order == 0 || order == 1)){
            throw new IllegalArgumentException("Order value can be only -1,0,1");
        }
        T[] unorderedArray = (T[]) new Object[collection.size()];
        collection.toArray(unorderedArray);
        boolean flagStop = true;
        for(int orderedElements = 0; orderedElements < unorderedArray.length; orderedElements++)
        {
            flagStop = true;
            for(int j = 1 ; j < unorderedArray.length; j++)
            {
                int first = j - 1;
                int second = j;
                int cResult = comparator.compare(unorderedArray[first], unorderedArray[second]);
                if(cResult == order)
                {
                    flagStop = false;
                    //swap
                    T tmp = unorderedArray[first];
                    unorderedArray[first] =  unorderedArray[second];
                    unorderedArray[second] = tmp;
                }
            }
            if(flagStop){
                break;
            }
        }

        //dest.clear();
        //Collections.addAll(dest, unorderedArray);
        dest.addAll(Arrays.asList(unorderedArray));
    }

    public static  void bubbleSortDecrescent(Collection collection,Collection dest, Comparator comparator)
    {
        bubbleSort(collection,dest, comparator, -1);
    }

    public static  void bubbleSort(Collection collection, Collection dest, Comparator comparator)
    {
        bubbleSort(collection, dest, comparator, 1);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy