Han

Han Ferik

Home | Projects | AP CSA

Project Memory Card

The NewSorting program is designed to allow users to input a list of integers and perform various operations on them, such as finding the maximum and minimum values, calculating the total sum, computing the average, and sorting the list using the Bubble Sort algorithm.

The program starts with the SortingInput method, where the user is prompted to enter the size of the array, followed by each number in the array. This input is collected through the Scanner class, which reads the values entered by the user. The numbers are then stored in an array, and the method ensures that only valid integers are entered. If an invalid input is detected, the value 0 is used as a default for that element in the array.

The Maximum method is responsible for finding the largest number in the array. It initializes the maximum variable with the first element of the array, and then iterates through the array, comparing each element to find a larger value. If a larger value is found, the maximum value is updated accordingly. Finally, the method prints the largest number.

Similarly, the Minimum method finds the smallest number in the array. It works in a similar way to the Maximum method but looks for smaller values during the iteration. Once the smallest value is found, it is printed for the user.

The TotalSum method calculates the sum of all numbers in the array. It iterates through each element, adding them to a running total, and then outputs the total sum of the array.

The AverageArray method calculates the average of the numbers in the array. It sums the values in the array, keeps count of the number of elements, and then divides the sum by the number of elements to get the average. The calculated average is then printed.

The most notable feature of the program is the bubbleSort method, which uses the Bubble Sort algorithm to sort the list in ascending order. The algorithm works by repeatedly comparing adjacent elements and swapping them if they are in the wrong order. This process continues until the array is sorted. After each pass, the array is printed to show the progress of the sorting process. If no swaps are made during a pass, the algorithm stops early, optimizing the sorting process.

Finally, the printArray method is used to print the current state of the array after any operation, allowing the user to see the list at any point in time.

Overall, the program demonstrates basic array operations, user input validation, and the implementation of the Bubble Sort algorithm. It provides the user with an interactive experience to work with a list of numbers, calculate various statistics, and observe how the sorting algorithm works step by step.


Sorting Program in Java

Sorting Program in Java


		import java.util.Scanner;
		
		public class NewSorting {
		    int maximum;
		    int minimum;
		    int sum;
		    int[] SortingList;
		
		    public void SortingInput() {
		        Scanner HowlongArrayInput = new Scanner(System.in);
		        System.out.println("Write the length of the list ? ");
		        int HowlongArray = HowlongArrayInput.nextInt();
		        SortingList = new int[HowlongArray];
		        int Number = 0;
		
		        for (int i = 0; i < HowlongArray; i++) {
		            Scanner NumberInput = new Scanner(System.in);
		            System.out.println("Enter Number: ");
		            if (NumberInput.hasNextInt()) {
		                Number = NumberInput.nextInt();
		                SortingList[i] = Number;
		            } else {
		                System.out.println("Invalid Input. Defaulting to 0");
		                SortingList[i] = 0;
		            }
		        }
		        printArray();
		    }
		
		    public void Maximum() {
		        maximum = SortingList[0];
		        for (int index : SortingList) {
		            if (index > maximum) {
		                maximum = index;
		            }
		        }
		        System.out.println("Maximum Value: " + maximum);
		    }
		
		    public void Minimum() {
		        minimum = SortingList[0];
		        for (int index : SortingList) {
		            if (index < minimum) {
		                minimum = index;
		            }
		        }
		        System.out.println("Minimum Value: " + minimum);
		    }
		
		    public void TotalSum() {
		        int Total = 0;
		        for (int index : SortingList) {
		            Total += index;
		        }
		        System.out.println("Total " + Total);
		    }
		
		    public void AverageArray() {
		        double Total = 0;
		        int Counter = 0;
		        for (int index : SortingList) {
		            Total += index;
		            Counter++;
		        }
		        System.out.println("Average " + (Total / Counter));
		    }
		
		    public void bubbleSort() {
		        System.out.println("Starting Bubble Sort...");
		        System.out.println("Bubble Sort steps: ");
		        for (int i = 0; i < SortingList.length - 1; i++) {
		            boolean swapped = false;
		            for (int j = 0; j < SortingList.length - i - 1; j++) {
		                if (SortingList[j] > SortingList[j + 1]) {
		                    int temp = SortingList[j];
		                    SortingList[j] = SortingList[j + 1];
		                    SortingList[j + 1] = temp;
		                    swapped = true;
		                }
		            }
		            System.out.print("Step " + (i + 1) + ": ");
		            printArray();
		            if (!swapped) {
		                break;
		            }
		        }
		    }
		
		    public void printArray() {
		        for (int num : SortingList) {
		            System.out.print(num + " ");
		        }
		        System.out.println();
		    }
		}
		
		public class NewSortingTester {
		    public static void main(String[] args) {
		        NewSorting program = new NewSorting();
		
		        program.SortingInput();
		
		        System.out.print("Entered Array: ");
		        program.printArray();
		
		        program.Maximum();
		        program.Minimum();
		        program.TotalSum();
		        program.AverageArray();
		
		        program.bubbleSort();
		    }
		}
		

Find me on the interwebs!

Github LinkedIn Instagram Facebook