Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #29605

Help with java insertion sort program

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From Scott Dunning <scottwd01@gmail.com>
Newsgroups comp.lang.java.programmer
Subject Help with java insertion sort program
Date Sat, 09 Aug 2014 19:11:10 -0700
Organization A noiseless patient Spider
Lines 95
Message-ID <ls6k6e$v0r$1@dont-email.me> (permalink)
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
Injection-Date Sun, 10 Aug 2014 02:08:14 +0000 (UTC)
Injection-Info mx05.eternal-september.org; posting-host="11828741c91d0da908948c6ecdf164c7"; logging-data="31771"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+ksStU0pwfzfT33K3A2E49p6qjMh4dQ0Q="
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:24.0) Gecko/20100101 Thunderbird/24.6.0
Cancel-Lock sha1:5qCJIXzvtymIEWLhespFKg+pEkY=
Xref csiph.com comp.lang.java.programmer:29605

Show key headers only | View raw


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 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));
			
		}
	}	

	

Back to comp.lang.java.programmer | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Help with java insertion sort program Scott Dunning <scottwd01@gmail.com> - 2014-08-09 19:11 -0700
  Re: Help with java insertion sort program Jeff Higgins <jeff@invalid.invalid> - 2014-08-09 22:28 -0400
    Re: Help with java insertion sort program Jeff Higgins <jeff@invalid.invalid> - 2014-08-09 23:34 -0400
      Re: Help with java insertion sort program Scott Dunning <scottwd01@gmail.com> - 2014-08-09 21:11 -0700
        Re: Help with java insertion sort program Jeff Higgins <jeff@invalid.invalid> - 2014-08-10 00:30 -0400
          Re: Help with java insertion sort program Jeff Higgins <jeff@invalid.invalid> - 2014-08-10 00:45 -0400
            Re: Help with java insertion sort program Jeff Higgins <jeff@invalid.invalid> - 2014-08-10 05:58 -0400
          Re: Help with java insertion sort program Jeff Higgins <jeff@invalid.invalid> - 2014-08-10 05:54 -0400
  Re: Help with java insertion sort program Mike Amling <mamling@chaff.us> - 2014-08-10 06:09 -0500
    Re: Help with java insertion sort program Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-08-10 07:43 -0400
      Re: Help with java insertion sort program Mike Amling <mamling@chaff.us> - 2014-08-13 23:51 -0500
        Re: Help with java insertion sort program Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-08-14 08:07 -0400
      Re: Help with java insertion sort program Mike Amling <mamling@chaff.us> - 2014-08-14 00:46 -0500
    Re: Help with java insertion sort program Arne Vajhøj <arne@vajhoej.dk> - 2014-08-19 16:50 -0400
  Re: Help with java insertion sort program Roedy Green <see_website@mindprod.com.invalid> - 2014-08-12 03:00 -0700
    Re: Help with java insertion sort program Mike Amling <mamling@chaff.us> - 2014-08-13 23:51 -0500
      Re: Help with java insertion sort program Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2014-08-14 14:30 +0300

csiph-web