0

I have a method to output the highest value, lowest value, average value and I need a sort method. I have tried to put what is called a "bubble method" but it isn't working out. Anyone know other sort methods I can use?

import java.util.Scanner;

public class Arrayassignment {

    public static void main(String[] args) {
        Scanner sin = new Scanner(System.in);

        System.out.println("Enter an intiger for array size.");

        int number = sin.nextInt();

        int array[] = new int[number];

        System.out.println("Array size " + number + " initiated.\n");

        System.out.println("Now enter the array intigers.");
        for (int i = 0; i < number; i++) {
            array[i] = sin.nextInt();

        }

        //System.out.println ( "\nLargest " + max (1, 3, 5) ); 
        System.out.println("sorting" + sort(array));
        System.out.println("The highest number in the array is " + max(array));
        System.out.println("The smallest number in the array is " + min(array));
        System.out.println("The average of the numbers in the array is " + avg(array));
    }

    public static int sort(int[] arg) {

        for (int i = 1; i < arg.length - 1; i++) {
            for (int j = i + 1; j < arg.length; j++) {

                if (arg[i] > arg[j]) {
                    int arrange = arg[i];

                    arg[i] = arg[j];
                    arg[j] = arrange;

                }
            }
        }
        return arrange;
    }

    public static int max(int[] arg) {
        if (arg.length == 0) {
            System.out.println(" empty arguement list ");
            return 0;
        }
        int largest = arg[0];
        for (int i = 1; i < arg.length; i++) {
            if (arg[i] > largest) {
                largest = arg[i];
            }
        }
        return largest;
    }

    public static int min(int[] arg) {
        if (arg.length == 0) {
            System.out.println(" empty arguement list ");
            return 0;
        }
        int smallest = arg[0];
        for (int i = 1; i < arg.length; i++) {
            if (arg[i] < smallest) {
                smallest = arg[i];
            }
        }
        return smallest;
    }

    public static double avg(int... arr) {
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            sum += arr[i];
        }
        double average = (double) sum / arr.length;
        return average;
    }
}
robotlos
  • 536
  • 2
  • 10
sjames14
  • 33
  • 1
  • 4

2 Answers2

0

There are many other sort methods you can use. The one you were trying to use is called "Bubble Sort" and is very expensive on large data sets unless they are somewhat ordered. I would recommend using selection sort or insertion sort for what you are trying to accomplish.

Here is a link to the many sorting algorithms you can implement: Sorting Algorithms

Here are some animations showing the process of these sorts (Highly recommend you look at these before implementing your algorithm): Helpful animations

Dane Lowrey
  • 170
  • 1
  • 10
0

You can use any sorting method as your convenient and according to your requirement. After sorted the array you can easily pick up the minimum and maximum value from the sorted array, first element and the last element of the array.

For calculate the average you have to use separate method as you used or you can use static variable to calculate the total inside the sorting method.

Refer this code.

    public class Arrayassignment {
    public static void main(String[] args) {
        Scanner sin = new Scanner(System.in);
        System.out.println("Enter an intiger for array size.");

        int number = sin.nextInt();

        int array[] = new int[number];

        System.out.println("Array size " + number + " initiated.\n");

        System.out.println("Now enter the array intigers.");
        for (int i = 0; i < number; i++) {
            array[i] = sin.nextInt();
        }

        sin.close();

        System.out.println("sorting");
        printArray(array); //Before sort
        sort(array);
        printArray(array); //After sort
        System.out.println("The highest number in the array is " + array[array.length - 1]);
        System.out.println("The smallest number in the array is " + array[0]);
        System.out.println("The average of the numbers in the array is " + avg(array));
    }

    public static void sort(int[] arg) {
        int arrange;
        for (int i = 0; i < arg.length - 1; i++)
            for (int j = i + 1; j < arg.length; j++) {

                if (arg[i] > arg[j]) {
                    arrange = arg[i];
                    arg[i] = arg[j];
                    arg[j] = arrange;
                }
            }
    }

    public static double avg(int... arr) {
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            sum += arr[i];
        }
        double average = (double) sum / arr.length;
        return average;
    }

   public static void printArray(int[] arr) {
      for (int value : arr) {
         // print elements according to your convenient
         System.out.println(value);
    }
}

To print the array you have traversal through the array. See Above code method.

Geeth Lochana
  • 164
  • 13