How do I call the Sorting Algorithm to sort input data from the keyboard?

Source: Internet
Author: User
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;
}
}
}
}
}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.