Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Jeff Higgins Newsgroups: comp.lang.java.programmer Subject: Re: Help with java insertion sort program Date: Sat, 09 Aug 2014 23:34:52 -0400 Organization: A noiseless patient Spider Lines: 124 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sun, 10 Aug 2014 03:35:15 +0000 (UTC) Injection-Info: mx05.eternal-september.org; posting-host="b896cb427f8c4d19d14880347aa9d6ce"; logging-data="31978"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+jiTCyl59cw6yIQEMCDStJEXWmU5TVExg=" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Icedove/24.7.0 In-Reply-To: Cancel-Lock: sha1:8XkUotHrzF8AWAZux0p65fX15GI= Xref: csiph.com comp.lang.java.programmer:29609 On 08/09/2014 10:28 PM, Jeff Higgins wrote: > On 08/09/2014 10:11 PM, Scott Dunning wrote: >> Hello I trying write a sorting program that will ask a user what type of >> sorting method to use (insertion, bubble, selection) and then ask them >> to enter integers to sort. > > I guessing that since you are asking for help here that working > the problem out from first principles on your own is not a requirement. > > I'll suggest then that Google is your friend. > Algorithms 4th ed. Sedgewick Wayne > 2.1 Elementary Sorts > Oops. Skip that since you've already done that homework. Instead, Google java.io.Console. Change public void printArray(int[] input) { to public static void printArray(int[] input) { and try: public class DriverSort { public static void main(String[] args) { int input[] = new int[] { 5, 7, 6, 3, 1 }; Sorter.insertionSort(input); Sorter.printArray(input); } } >> I thought I had everything correct but apparantly not. Haha. This is >> the output and I am not sure where I am going wrong. >> >> Before Sorting: >> [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] >> >> After Sorting: >> [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] >> [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] >> >> >> Do I seem to be putting things in the correct spot? >> >> 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"); >> choice = scan.nextInt(); >> >> System.out.println("Please enter the number for a sorting >> method or enter 0 to quit: "); >> int size = scan.nextInt(); >> int input[] = new int[size]; >> >> >> 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(Arrays.toString(input)); >> >> } >> } >> >> >> >