Reputation: 627
I'm trying to write a sorting program that will ask a user what type of sorting method to use (insertion, bubble, selection) and then ask him to enter integers to sort.
I think I have everything correct except for the array: I want the size of the array to be big as the number of integers the user enters, but I do not seem to be doing it correctly.
In the sorting class, where the insertionSort method is, should I have the input parameter named like that (through out the algorithm) or should I make a generic name like "arr"?
Where can I improve and correct 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());
}
}
Upvotes: 3
Views: 1194
Reputation: 324
I would suggest dropping the Sorter class altogether and add the functionality of the Sorter class as methods into DriverSorter. I say this because the way you are implementing Sorter 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;
}
}
Keeping 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's
// super useful.
public void printArray(int[] input)
{
System.out.println(this.vector.toString());
}
}
Upvotes: 2
Reputation: 201487
The first thing I see is
int size = input.length;
int input[] = new int[size];
Which is so illegal I stopped reading. Either hard-code the size, or prompt the user for it.
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];
Upvotes: 1