2012-06-05 156 views
-6
A - an array containing the list of numbers 
numItems - the number of numbers in the list 

for i = 0 to numItems - 1 
    for j = i+1 to numItems 
     if A[i] > A[j] 
      // Swap the entries 
      Temp = A[i] 
      A[i] = A[j] 
      A[j] = Temp 
     End If  
    Next j 
Next i\ 

可以comeone將它轉換爲java給我嗎? 我試過了,但無法弄清楚。Java選擇排序

+1

反正你很近...應該很容易查找... – NominSim

+0

http://stackoverflow.com/questions/10852522/selection-sort-for-java –

回答

0
public static void selectionSort1(int[] x) { 
    for (int i=0; i<x.length-1; i++) { 
     for (int j=i+1; j<x.length; j++) { 
      if (x[i] > x[j]) { 
       // Exchange elements 
       int temp = x[i]; 
       x[i] = x[j]; 
       x[j] = temp; 
      } 
     } 
    } 
}