This Java program takes an array of numbers as input and sorts them in ascending order using a basic Bubble Sort algorithm. Users specify the number of elements in the array and input their values, which are then sorted and displayed.
- Accepts user input for the number of elements.
- Reads multiple integers from the user.
- Displays the original array before sorting.
- Sorts the array in ascending order (modifiable for descending order).
- Prints the sorted array.
Array_Sort.java
β Contains the implementation for sorting an array.
- Compile the Java file:
javac Array_Sort.java
- Run the program:
java Array_Sort
- Enter the number of elements and the array values:
Enter number of arrays: 5 Input 5 number/s: 8 3 7 1 9
- Example Output:
Original Array: 8, 3, 7, 1, 9 Sorted Array: 1, 3, 7, 8, 9
- The program prompts the user to input the size of the array.
- It then collects integer values into an array.
- The Bubble Sort algorithm is used to arrange the numbers in ascending order.
- The sorted array is displayed as output.
- To switch to descending order, simply change the comparison operator (
>
to<
) in the sorting loop.
This project is open-source and free to use.