Tools: VSCode | Source Code: GitHub
The Sorting program is designed to interact with the user by receiving a list of numbers, performing various operations such as finding the largest and smallest values, calculating the sum of the numbers, and sorting the list in ascending order using the Bubble Sort algorithm. The program prints the intermediate steps during the sorting process, allowing the user to observe how the list is sorted step by step.
The program begins with a method called InputArray
. This method prompts the user to define the size of the list and then takes input for each number in the list. A Scanner
object is used to collect user input, and the numbers are stored in a dynamically allocated array called Han
. The method uses a loop to iterate as many times as the size specified by the user, ensuring all numbers are stored properly.
The BiggySort
method is designed to find the largest number in the array. It uses nested loops to compare each element with all other elements in the list. If a larger value is found, it updates the variable biggest
. Although the nested loops make the method less efficient, it demonstrates a basic approach to comparing elements in the list.
The LillySort
method attempts to find the smallest number in the array. Similar to BiggySort
, it uses nested loops to compare each element of the array. The smally
variable is updated whenever a smaller value is encountered. If two elements are equal, the method skips any action using a continue
statement. The method ensures that the smallest value in the array is returned.
The Sum
method calculates the sum of all the numbers stored in the array. It iterates through the array using a single loop and accumulates the values of all elements into the summy
variable. Once the loop completes, the total sum of the array is returned, providing the user with a straightforward calculation of the list's total value.
The most significant part of the program is the BubbleSort
method, which sorts the list in ascending order using the Bubble Sort algorithm. The method works by repeatedly comparing adjacent elements in the list and swapping them if they are out of order. This process continues until the entire list is sorted. The method also prints the list after each swap, allowing the user to observe the progress of the sorting process in real-time. This step-by-step display highlights how Bubble Sort gradually moves the largest unsorted elements to their correct positions.
Overall, the program combines user interaction with basic sorting and calculation methods. It allows the user to input data dynamically, observe the sorting process, and obtain key results such as the largest number, smallest number, and sum of the list. While the program uses a simple approach, it demonstrates foundational programming concepts like loops, nested comparisons, and sorting algorithms.
import java.util.Scanner;
public class Sorting {
double biggest;
double smally;
double summy;
double[] Han;
public void InputArray() {
Scanner HowlongArrayInput = new Scanner(System.in);
System.out.println("How big list?");
int HowlongArray = HowlongArrayInput.nextInt();
Han = new double[HowlongArray];
for (int g = 0; g < HowlongArray; g++) {
Scanner NumberInput = new Scanner(System.in);
System.out.println("Enter Number?");
double Number = NumberInput.nextDouble();
Han[g] = Number;
}
}
public double BiggySort() {
for (int i = 0; i < Han.length; i++) {
for (int k = 0; k < Han.length; k++) {
if (Han[i] > Han[k]) {
biggest = Han[i];
}
else if (Han[i] == Han[k]) {
continue;
}
else if (Han[i] < Han[k]) {
break;
}
}
}
return biggest;
}
public double LillySort() {
for (int i = 0; i < Han.length; i++) {
for (int k = 0; k < Han.length; k++) {
if (Han[k] < Han[i]) {
smally = Han[k];
}
else if (Han[i] == Han[k]) {
continue;
}
}
}
return smally;
}
public double Sum() {
for (int i = 0; i < Han.length; i++) {
summy += Han[i];
}
return summy;
}
public void BubbleSort() {
int n = Han.length;
for (int b = 0; b < n - 1; b++) {
for (int j = 0; j < n - b - 1; j++) {
if (Han[j] > Han[j + 1]) {
double temp = Han[j];
Han[j] = Han[j + 1];
Han[j + 1] = temp;
System.out.print("List: ");
for (double num : Han) {
System.out.print(num + " ");
}
System.out.println();
}
}
}
}
}