Sorting an Array

 

public class SortArray {
// this class will sort an array
public static void main(String[] args) {
int i; // array index
double min; // Current min value
double max;
double temp; // hold the array so it does not get overwritten
double[] a = { 2.3, 3.4 , 4.5, 5.6, 1.2, 7.8, 8.9 }; // 7 elements
double[] b = { -8.8, 9.7, -14.6, 89.8 }; // 4 elements

printArray(a);
System.out.println();
for (int j = 0; j< a.length; j++){ // repeat the sort process
for (i = 0; i< a.length -1; i++)
{
if (a[i + 1]<a[i])
{
temp = a[i]; // store the current value of a[i]
a[i] = a[i + 1]; // swap a[i] with the next value in the array
a[i + 1] = temp; // place the original a[i] value into the next place in the array
}// end of the inner for loop
}// end of the outer for loop

} // end of the for loop
printArray(a);

}// end of my main method

public static void printArray(double[] array){
for (int i=0; i<array.length; i++){ // iterate through the each row
System.out.print(array[i] + " ");
}// end of the inner for loop

}// end of the outer for loop

} // end of my class