D'accord pour ma tâche, je dois trier des séries de nombres aléatoires dans l'ordre du plus grand au plus petit. Les nombres aléatoires vont d'indexOfMaxInRange, prennent l'index du plus grand nombre qui le renvoie. Dans swapElements, il doit fondamentalement permuter l'index avec l'élément le plus haut à un [0], le second le plus haut à un [1] et ainsi de suite. Ces deux méthodes sont transmises via sortarray. S'il vous plaît donnez-moi quelques commentaires sur la façon de terminer cela et ce que je fais mal merci. PS: Tout n'est pas appelé dans le code, pour diverses raisons telles que l'impression.Trier une liste de tableaux aléatoires du plus grand au plus petit
Voici mon code:
public static void main(String[] args) {
int[] array = randomIntArray (10);
int index = indexOfMaxInRange (array, -5, 15);
swapElement (array, index, 0);
}
public static int randomInt (int low, int high){ // Create a serie of random numbers
int x = 0;
for (int i = 0; i < 10; i++){
x = (int)(Math.random()* (high - low) +low);
}
return x;
}
public static int[] randomIntArray (int n) { // Size of array
int[] a = new int [n];
for (int i = 0; i <a.length; i++){
a[i] = randomInt (-5, 15);
}
return a;
}
public static int indexOfMaxInRange (int[] a, int low , int high){ //Find the index of the largest element
int [] b = new int [a.length];
int index = 0;
for (int i = 0; i < a.length-1; i++){
if (a[i] >= low && a[i] < high)index++;
if (a[i] > a[i+1]){
b[i] = a[i];
}
System.out.println (b[i]+"\t"+ (index));
}
return index ;
}
public static int swapElement (int []a, int index , int i){ // Swap the element within the array
int temp = 0;
System.out.println();
for (i = 0; i <a.length; i++){
temp = index;
a[i] = a[a.length - i - 1];
temp = a[a.length - i - 1];
System.out.println (temp + "\t"+ index);
}
return temp;
}
public static void sortArray (int[] array){
for (int i = 0; i <array.length; i++){
int index = indexOfMaxInRange (array, -5, 15);
swapElement (array, index, 0);
System.out.println(); // This will print out the newly arranged order of the numbers.
}
}
vous voulez sans doute dire ce que/où les choses vont mal. –
sinon vous échangez seulement une fois, vous aurez besoin de faire cela encore quelques fois si vous voulez vraiment trier votre liste. –
Je ne comprends pas ce que vous dites, faites quoi Jay? – Bao