...
double []test = new double []{88.6,88.9,-1,25,88.6};
int size = test.length;
System.out.println("*** un-sorted ***");
for (double d : test){
System.out.println(d);
}
// descending sort algo *************************************
int counter = 0;
int indexOfMax = 0;
// after each iteration of while, test[counter] should have the correct value
while (counter < size){
indexOfMax = counter; // assuming that test[counter] has the max value
// this loop finds the index of max value (stores in indexOfMax) from the test array starting from index counter+1
for(int i = counter+1; i < size; i++){
if (test[i]>test[indexOfMax]){
indexOfMax = i;
}
} // end of for loop -- now indexOfMax should have the max value
// swapping
double tmp = test[counter];
test[counter] = test[indexOfMax];
test[indexOfMax] = tmp;
counter++;
}
// end of algo *********************************************
System.out.println("*** sorted ***");
for (double d : test){
System.out.println(d);
}
...
double []test = new double []{88.6,88.9,-1,25,88.6};
int size = test.length;
System.out.println("*** un-sorted ***");
for (double d : test){
System.out.println(d);
}
// descending sort algo *************************************
int counter = 0;
int indexOfMax = 0;
// after each iteration of while, test[counter] should have the correct value
while (counter < size){
indexOfMax = counter; // assuming that test[counter] has the max value
// this loop finds the index of max value (stores in indexOfMax) from the test array starting from index counter+1
for(int i = counter+1; i < size; i++){
if (test[i]>test[indexOfMax]){
indexOfMax = i;
}
} // end of for loop -- now indexOfMax should have the max value
// swapping
double tmp = test[counter];
test[counter] = test[indexOfMax];
test[indexOfMax] = tmp;
counter++;
}
// end of algo *********************************************
System.out.println("*** sorted ***");
for (double d : test){
System.out.println(d);
}
...
Comments