Java: insert sorting issue with array size

I am trying to write a sorting program that will ask the user what type of sort method to use (insert, bubble, select) and then ask them to input integers to sort.

I think I have everything right except the array: I want the size of the array to be as large as the number of integers the user enters, but I don't seem to be doing it right.

In a sort class where the insertionSort method is used, should I have an input parameter named like this (via an algorithm), or should I create a common name like "arr"?

Where can I improve and fix my code?

Thanks for any help!

DriverSort class:

import java.util.Scanner;

public class DriverSort 
{
    public static void main(String[] args) 
    {
        Scanner scan =new Scanner(System.in);
        Sorter sorter = new Sorter();

        int choice;// variable which says which sorting algorithm to use

        System.out.println("1-Insertion sort\n"
                    +"2-Selection sort\n"
                    + "3-Bubble sort\n"
                    + "0-quit\n");
        int size = scan.nextInt();
        int input[] = new int[size];

        System.out.println("Please enter the number for a sorting method or enter 0 to quit: ");
        size = scan.nextInt();


        System.out.println("\nBefore Sorting: ");
        sorter.printArray(input);

        // sort the array
        Sorter.insertionSort(input);
        System.out.println("\nAfter Sorting: ");
        sorter.printArray(input);

        switch (choice) 
        {
            case 0:
                System.out.println("Goodbye!");
                System.exit(0);
                break;

            case 1: 
                Sorter.insertionSort(input);
                sorter.printArray(input);
                break;
        }
   }
}

      

Sorter class:

public class Sorter
{
    public static int[] insertionSort(int[] input) 
    {
        for (int i = 1; i < input.length; i++) 
        {
            int valueToSort = input[i];
            int j = i;

            while (j > 0 && input[j - 1] > valueToSort) 
            {
                input[j] = input[j - 1];
                j--;
            }//end while loop.

            // insert the element
            input[j] = valueToSort;
        }//end for loop    

        return input;
    }//end insertionSort          

    public void printArray(int[] input) 
    { 
        System.out.println(input.toString());
    }
}   

      

+3


source to share


2 answers


I would suggest dropping the Sorter class and adding the functionality of the Sorter class as methods in the DriverSorter. I say this because the way the Sorter is implemented does not create useful instances.

import java.util.Scanner;

public class DriverSort {

    public static void main(String[] args) {

        Scanner scan =new Scanner(System.in);

        // this makes more sense to put this at the start of the program
        int choice;  // variable which says which sorting algorithm to use
        System.out.println("1-Insertion sort\n"
                +"2-Selection sort\n"
                + "3-Bubble sort\n"
                + "0-quit\n");
        choice = scan.nextInt();

        if (choice != 0) { // a simple if else statement will do just fine
            // must prompt user for the "input first"
            System.out.println("Enter the length vector to be modified: ");
            int size = scan.nextInt();
            // now actually get the vector
            int input[] = new int[size];
            for (int i = 0; i < size; i++) {
                System.out.println("Enter next array element: ");
                input[i] = scan.nextInt();
            }

            System.out.println("\nBefore Sorting: ");
            System.out.println(input); // use the builtin functionality
            // sort the array
            int[] output = insertionSort(input);
            System.out.println("\nAfter Sorting: ");
            System.out.println(output);
        } else { 
            System.out.println("Goodbye!");
            System.exit(0);
        }  
    }

    // returns a sorted list (add more detail here)
    // add a new input that tells what sort of sorting to do
    public static int[] insertionSort(int[] input) {

        for (int i = 1; i < input.length; i++) {
            int valueToSort = input[i];
            int j = i;
            while (j > 0 && input[j - 1] > valueToSort) {
                input[j] = input[j - 1];
                j--;
            }//end while loop.
            // insert the element
            input[j] = valueToSort;
        }//end for loop    
        return input;
    } 
}

      

Saving the sorter class:



public class Sorter {
    private int vector; \\ private just means only things inside this class can affect this variable
    // now initializer 
    public Sorter(int[] input) {
        this.vector = input; \\ set our field to be equal to the vector you input when making an instance
    // so the call to make a Sorter object will now be "Sorter sorter = new Sorter(input);"
    }

    // make this act on vector instead
    public static int[] insertionSort() // no input because it can "see" our vector field
    {
        int[] copy = this.vector; // make a copy so you don't mess vector up before your finished
        for (int i = 1; i < copy.length; i++) 
        {
            int valueToSort = copy[i];
             int j = i;

            while (j > 0 && copy[j - 1] > valueToSort) 
            {
                copy[j] = copy[j - 1];
                j--;
            }//end while loop.

            // insert the element
            copy[j] = valueToSort;
        }//end for loop    

        this.vector = copy; // now replace old field with our sorted copy!
    }//end insertionSort          

    // this is an excellent way to be able to see "vector" without allowing other
    // mischievous programs to accidentally change "vector." This is very similar reasoning
    // to why you very frequently have fields be private. Read up on encapsulation - it 
    // super useful.
    public void printArray(int[] input) 
    { 
    System.out.println(this.vector.toString());
    }

      

}

+2


source


The first thing I see is

int size = input.length;
int input[] = new int[size];

      

What's so illegal, I stopped reading. Either specify the size of the hardcode, or offer the user one.

int size = 10; // <-- 10
int input[] = new int[size]; // <-- this is fine.

      



or

int input[] = new int[10]; // <-- 10
int size = input.length; // <-- this if also fine.

      

So, you want something like -

System.out.println("Please enter the number for a sorting "
    + "method or enter 0 to quit: ");
int choice = scan.nextInt();
System.out.println("How many numbers do you want to enter: ");
int size = scan.nextInt();
int input[] = new int[size];

      

+1


source







All Articles