FANDOM


Code Edit

public class Sort{

    public static List gSort(List list){      // standard groovy sort
        return list.sort()
    }

    public static List bubbleSort(List l){
        def swapped = true

        while(swapped){
            swapped = false

            for(i in 0..(l.size() - 2)){
                if(l[i+1] < l[i]){
                
                Collections.swap(l, i+1, i)

                swapped = true
                }
            }

        }
        return l
    }

    public static List selectionSort(List l){
        for(i in 0..l.size() - 1){
            def curr = i

            for(j in curr..l.size() - 1){
               if(l[j] < l[curr]){ curr = j } 
            }

            Collections.swap(l,i,curr)
        }

        return l
    }

    public static List merge(List left, List right){
        List result = []

        while( left.size() > 0 && right.size() > 0){

            def l_size = left.size()
            def r_size = right.size()

            if(left[0] <= right[0]){
                result << left[0]

                if(l_size > 1){
                    left = left[1..l_size - 1]
                }else {
                    left = []
                }

            } else {
                result << right[0]
                if(r_size > 1){
                    right = right[1..r_size - 1]
                }else{
                    right = []
                }
            }

        }
        if(left.size() > 0) result += left
        if(right.size() > 0) result += right

        return result
    }

    public static List mergeSort(List list){

        List left, right
        def lst_size = list.size();
        int lst_mid = (lst_size - 1) / 2
        def result



        if(lst_size <= 1){
            return list
        }else{
            
            left = list[0..lst_mid]
            right = list[lst_mid + 1..lst_size - 1]

            left = mergeSort(left)
            right = mergeSort(right)

            result = merge(left, right)


            return result
        }
    }

    public static void main(String[] args){
        List l = [3,1,2,8,4]
        println Sort.selectionSort(l)
    }
    

See Also Edit

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.