bubble,insert,select三种排序的java实现

bubble,insert,select三种排序的java实现
package com.liu.Sort;

public class InsertSort {
    private static ArrayInsert arr;
    private static int number = 50;
    public static void main(String args[])
    {
        arr = new ArrayInsert(number);
        
        arr.insert(45);
        arr.insert(0);
        arr.insert(-543);
        arr.insert(545);
        arr.insert(432);
        arr.insert(87);
        arr.insert(32);
        arr.insert(72);
        arr.insert(262);
        arr.insert(-543);
        arr.insert(4);
        arr.insert(4);
        arr.insert(5);
        
        arr.display();
        //arr.insertsort();
        //System.out.print("insert:");
        //arr.display();
//        arr.bubbleSort();
//        System.out.print("bubble:");
//        arr.display();
        arr.selectSort();
        System.out.print("select:");
        arr.display();
    }
}

class ArrayInsert
{
    private long[] a;
    private int size;
    public ArrayInsert(int maxsize)
    {
        a = new long[maxsize];
        size = 0;
    }
    
    public void insert(long value)
    {
        a[size] = value;
        size++;
    }
    
    public void swap(int one,int two)
    {
        long temp = a[one];
        a[one] = a[two];
        a[two] = temp;
    }
    
    public void insertsort()
    {
        int in,out;
        long temp_1;
        for(out = 1; out<size; out++)
        {
            in = out;
            temp_1 = a[in];
            while(in>0&&temp_1<a[in-1])
            {
                a[in]=a[in-1];
                --in;
            }
            a[in]=temp_1;
        }
    }
    
    public void bubbleSort()
    {
        int in,out;
        for(out=size;out>0;out--)
        {
            for(in=0;in<out;in++)
            {
                if(a[in]>a[in+1])
                    swap(in,in+1);
            }
        }
    }
    
    public void selectSort()
    {
        int out,in,min;
        for(out=0;out<size;out++)
        {
            min=out;
            for(in=out+1;in<size;in++)
            {
                if(a[in]<a[min])
                    min = in;
            }
            swap(out,min);
        }
    }
    
    public void display()
    {
        for(int j=0;j<size;j++)
            System.out.print(a[j]+" ");
        System.out.println();
            
    }
}
bubble,insert,select三种排序的java实现

 

bubble,insert,select三种排序的java实现,布布扣,bubuko.com

bubble,insert,select三种排序的java实现

上一篇:微信小程序——常用快捷键【四】


下一篇:20个优秀的 JavaScript 键盘事件处理库