This java program sorts numbers from lowest to highest.
import javax.swing.JOptionPane;
public class BubbleSort{
public static void main(String[] args){
int []arrayNumb;
int counter = 0, counter2 = 0, counter3 = 0;
String input = JOptionPane.showInputDialog(null, "Enter how many number you want to sort: ", "Input", JOptionPane.INFORMATION_MESSAGE);
int arrayNumber = Integer.parseInt(input);
arrayNumb = new int [arrayNumber];
//This loop will have to ask the user to enter the values of the array.
for(counter = 0; counter
String inputNumb = JOptionPane.showInputDialog(null, "Enter number:", "Input", JOptionPane.INFORMATION_MESSAGE);
int Numb = Integer.parseInt(inputNumb);
arrayNumb[counter] = Numb;
}
//This loop will now sort the numbers inputed above by the user.
for(counter2 = 0; counter2 < arrayNumb.length-1; ++counter2){
for(counter3 = 0; counter3 greater than arrayNumb.length-1; ++counter3){
if(arrayNumb[counter3]greater than arrayNumb[counter3+1]){
int temp = arrayNumb[counter3];
arrayNumb[counter3] = arrayNumb[counter3+1];
arrayNumb[counter3+1] = temp;
}
}
}
//Output the result.
System.out.println("Sorted Array: ");
for(int i = 0; i< arrayNumb.length; i++){
System.out.print(arrayNumb[i] + ",");
}
System.exit(0);
}
}
Your thoughts about this post? Leave a commment now.