选择排序

来源:互联网 发布:顾客数据库 编辑:程序博客网 时间:2024/06/11 20:07

选择排序

public class ArraySel {    private long[] a;    private int nElems;    public ArraySel(int max) {        a = new long[max];        nElems = 0;    }    public void insert(long value) {        a[nElems] = value;        nElems++;    }    public void display() {        for(int j = 0; j < nElems; j++) {            System.out.print(a[j] + " ");        }        System.out.println("");    }    public void selectionSort() {        int out, in, min;        for(out = 0; out < nElems-1; out++) {            min = out;            for(in = out+1; in < nElems; in++) {                if(a[in] < a[min]) {                    min = in;                }            }            swap(out, min);        }    }    private void swap(int one, int two) {        long temp = a[one];        a[one] = a[two];        a[two] = temp;    }}


0 0
原创粉丝点击