Demo entry 6720745

sort

   

Submitted by anonymous on Mar 18, 2018 at 03:26
Language: Java. Code size: 4.5 kB.

public class Sort {

    public static void main(String args[]){
        int[] array={5,2,4,6,1,3};
        print(array);
//        InsertionSort.insertion_sort(array);
//        QuickSort.quick_sort(array,0,array.length-1);
//        BubbleSort.bubble_sort(array);
//        SelectSort.select_sort(array);
//        ShellSort.shell_sort(array);
        int[] array1=new int[6];
        MergeSort.merge_sort(array,array1);
        print(array);
    }

    static class InsertionSort{
        static void insertion_sort(int[] array){
            for(int j=1;j<array.length;j++){
                int key=array[j];
                int i=j-1;
                while(i>=0&&array[i]>key){
                    array[i+1]=array[i];
                    i--;
                }
                array[i+1]=key;
            }
        }

    }

    static class QuickSort{
        static void quick_sort(int[] array,int p,int q){
            if(p<q){
                int r=partition(array,p,q);
                quick_sort(array,p,r-1);
                quick_sort(array,r+1,q);
            }
        }

        static int partition(int[] array,int p,int q){
            int key=array[q];
            int i=p-1;
            for(int j=p;j<q;j++){
                if(array[j]<key){
                    i++;
                    exchange(array,i,j);
                }
            }
            exchange(array,++i,q);
            return i;
        }
    }

    static class BubbleSort{
        static void bubble_sort(int[] array){
           for(int i=0;i<array.length-1;i++){
               for(int j=0;j<array.length-i-1;j++){
                   if(array[j]>array[j+1]){
                       exchange(array,j,j+1);
                   }
               }
           }
        }
    }

    static class SelectSort{
        static void select_sort(int[] array){
            for(int i=0;i<array.length;i++){
                int min=i;
                for(int j=i+1;j<array.length;j++){
                    if(array[min]>array[j]){
                        min=j;
                    }
                }
                if(min!=i){
                    exchange(array,min,i);
                }
            }
        }
    }

    static class ShellSort{
        static void shell_sort(int[] array){
            for(int d=array.length/2;d>=1;d=d/2){
                for(int i=d;i<array.length;i++){
                    if(array[i]<array[i-d]){
                        int key=array[i];
                        int j=i-d;
                        for(;j>=0&&key<array[j];j=j-d){
                            array[j+d]=array[j];
                        }
                        array[j+d]=key;
                    }
                }
            }
        }
    }

    static class MergeSort{
        static void merge_sort(int[] array,int[] array1){
            int h=1;
            while(h<array.length){
                merge_pass(array,array1,h);
                h=2*h;
                for(int i=0;i<array.length;i++){
                    array[i]=array1[i];
                }
            }
        }

        static void merge_pass(int[] array,int[] array1,int h){
            int i=0;
            while(i<array.length-2*h+1){
                merge(array,array1,i,i+h-1,i+2*h-1);
                i+=2*h;
            }
            if(i<array.length-h+1){
                merge(array,array1,i,i+h-1,array.length-1);
            }else{
                for(;i<array.length;i++){
                    array1[i]=array[i];
                }
            }
        }

        static void merge(int[] array,int[] array1,int a,int b,int c){
            int i=a;
            int j=b+1;
            int k=a;

            while(i<=b&&j<=c){
                if(array[i]<array[j]){
                    array1[k++]=array[i++];
                }else{
                    array1[k++]=array[j++];
                }
            }

            while(i<=b)array1[k++]=array[i++];
            while(j<=c)array1[k++]=array[j++];

        }
    }

    static void exchange(int[] array,int x,int y){
        if(x==y) return;
        int t=array[x];
        array[x]=array[y];
        array[y]=t;
    }

    private static void print(int[] array){
        for(int i=0;i<array.length;i++){
            System.out.print(array[i]+" ");
        }
        System.out.println();
    }

}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).