选择排序舞蹈视频:http://t.cn/hros6e
java代码实现:
/** * 选择排序 * @author hsy * */ public class SelectSort { private long[] a; private int nElems; public SelectSort( int max) { super(); this.a = new long[max]; nElems = 0; } /** * 向数组中插入数据 * @param value */ public void insert(long value){ a[nElems] = value; nElems++; } /** * 打印数组 * */ public void display(){ for(int i=0;i<nElems;i++){ System.out.print(a[i]+","); } System.out.println(""); } /** * 排序 * */ public void selectSort(){ int out; int in; int min; for(out=0;out<nElems;out++){ min = out; for(in=0;in<out;in++){ if(a[in]>a[min]){ min = in; } swap(out,min); } } } /** * 大小换位 * @param index1 * @param index2 */ private void swap(int index1,int index2){ long temp = a[index1]; a[index1] = a[index2]; a[index2] = temp; } public static void main(String[] args){ int max = 100; SelectSort arr = new SelectSort(max); arr.insert(10); arr.insert(5); arr.insert(45); arr.insert(8); arr.insert(34); arr.insert(78); arr.insert(22); arr.insert(68); arr.insert(82); arr.insert(53); arr.insert(7); arr.insert(99); arr.insert(57); arr.insert(72); arr.display(); long start = System.currentTimeMillis(); arr.selectSort(); long end = System.currentTimeMillis(); System.out.println("排序耗时:"+(end-start)); arr.display(); } }
10,5,45,8,34,78,22,68,82,53,7,99,57,72, 排序耗时:0 5,7,8,10,22,34,45,53,57,68,72,78,82,99,