2017-03-11 1 views

Répondre

0

Il faut suivre l'indice minimum de la boucle externe et échanger deux éléments après chaque fin de boucle interne, par exemple .:

for (i = 0; i < size - 1; i++) { 
    minIndex = i; 
    for (j = i; j < size; j++) { 
    if (array[j] < array[minIndex]) { 
     minIndex = j; 
    } 
    } 
    swap(array, i, minIndex); 
} 

Votre code fixe:

public static void arraySort(int[] a) { 
    //for loop to go through array 
    for(int i = 0; i < a.length; i++) { 
     int temp = a[i]; //set a temp value for first value 
     int minIndex = i; 
     for (int x = i + 1; x < a.length; x++) { 
      if (a[x] < temp) { 
       a[i] = a[x]; 
       temp = a[x]; 
       minIndex = x; 
      } 
     } 
     //swap 
     a[minIndex] = a[i]; 
     a[i] = temp; 
    } 
}