可以用JAVA进行选择排序:
1 // selectSort.java 2 // demonstrates selection sort 3 // to run this program: C>java SelectSortApp 4 //////////////////////////////////////////////////////////////// 5 package SelectSortApp; 6 7 class ArraySel 8 { 9 private long[] a; // ref to array a 10 private int nElems; // number of data items 11 //-------------------------------------------------------------- 12 public ArraySel(int max) // constructor 13 { 14 a = new long[max]; // create the array 15 nElems = 0; // no items yet 16 } 17 //-------------------------------------------------------------- 18 public void insert(long value) // put element into array 19 { 20 a[nElems] = value; // insert it 21 nElems++; // increment size 22 } 23 //-------------------------------------------------------------- 24 public void display() // displays array contents 25 { 26 for(int j=0; j<nElems; j++) // for each element, 27 System.out.print(a[j] + " "); // display it 28 System.out.println(""); 29 } 30 //-------------------------------------------------------------- 31 public void selectionSort() 32 { 33 int out, in, min; 34 35 for(out=0; out<nElems-1; out++) // outer loop 36 { 37 min = out; // minimum 38 for(in=out+1; in<nElems; in++) // inner loop 39 if(a[in] < a[min] ) // if min greater, 40 min = in; // we have a new min 41 swap(out, min); // swap them 42 } // end for(out) 43 } // end selectionSort() 44 //-------------------------------------------------------------- 45 private void swap(int one, int two) 46 { 47 long temp = a[one]; 48 a[one] = a[two]; 49 a[two] = temp; 50 } 51 //-------------------------------------------------------------- 52 } // end class ArraySel 53 //////////////////////////////////////////////////////////////// 54 public class SelectSort 55 { 56 public static void main(String[] args) 57 { 58 int maxSize = 100; // array size 59 ArraySel arr; // reference to array 60 arr = new ArraySel(maxSize); // create the array 61 62 arr.insert(77); // insert 10 items 63 arr.insert(99); 64 arr.insert(44); 65 arr.insert(55); 66 arr.insert(22); 67 arr.insert(88); 68 arr.insert(11); 69 arr.insert(00); 70 arr.insert(66); 71 arr.insert(33); 72 73 arr.display(); // display items 74 75 arr.selectionSort(); // selection-sort them 76 77 arr.display(); // display them again 78 } // end main() 79 } // end class SelectSortApp 80 ////////////////////////////////////////////////////////////////