How do I call the Sorting Algorithm to sort input data from the keyboard? | -- How to input data from the keyboard | -- how to sort data by bubble
The source code is as follows (detailed comments are as follows ):
Import java. util. arraylist;
Import java. util. collections;
// Define the keyboard input data class
Public class keyboardinputdata {
Public static void main (string [] ARGs ){
// You are prompted to enter data from the console because while (while. hasnext () cannot jump out of the loop. hasnextint (), when the input data is not int type, it jumps out of the loop, so it ends with over.
System. Out. Print ("enter a number and separate it with spaces. End with over after the input :");
// Define a scanner to scan data input from the keyboard
Required bytes = new bytes (system. In );
// Define the arraylist to store the input data. It is inconvenient to define an array here. I don't know how big the array is. In the while loop, the lower mark value of the array cannot be auto-incremented, so the data cannot be stored in the array through loops, the Collection framework has this function.
Arraylist <integer> arraylist = new arraylist <integer> ();
// Save the scanned data to arraylist.
While (iterator. hasnextint ()){
Arraylist. Add (New INTEGER (response. nextint ()));
}
// View the number of input data
System. Out. println ("Number of input data:" + arraylist. Size ());
// Define an appropriate array to store input data
Int [] A = new int [arraylist. Size ()];
// Print the array to display the entered data
For (INT I = 0; I <A. length; I ++ ){
A [I] = arraylist. Get (I). intvalue ();
System. Out. Print (A [I] + "");
}
System. Out. println ();
// Call the Bubble Sorting Algorithm to sort the input data
Bubblesort ();
// Print the sorted input data
For (INT I = 0; I <A. length; I ++ ){
System. Out. Print (A [I] + "");
}
}
// Define the Bubble sorting method
Public static void bubblesort (INT [] ARR) {// bubble sort in ascending order
Int temp = 0;
For (INT I = 0; I <arr. length; I ++ ){
For (Int J = I; j <arr. length; j ++ ){
If (ARR [I]> arr [J]) {
Temp = arr [I];
Arr [I] = arr [J];
Arr [J] = temp;
}
}
}
}
}