Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #29610
| From | Scott Dunning <scottwd01@gmail.com> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Help with java insertion sort program |
| Date | 2014-08-09 21:11 -0700 |
| Organization | A noiseless patient Spider |
| Message-ID | <ls6r8g$694$1@dont-email.me> (permalink) |
| References | <ls6k6e$v0r$1@dont-email.me> <ls6lcd$5nm$1@dont-email.me> <ls6p9j$v7a$1@dont-email.me> |
Ok so I realized that I was not filling the array with anything but now
I'm having issues filling it correctly. It's only filling the array
with three of the user inputs and I just cannot figure out why....
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];
for(int i = 0; i < size; i++) {
input[i] = 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);
break;
}
}
}
import java.util.Arrays;
public class Sorter {
public static int[] insertionSort(int[] arr) {
for (int i = 1; i < arr.length; i++) {
int valueToSort = arr[i];
int j = i;
while (j > 0 && arr[j - 1] > valueToSort) {
arr[j] = arr[j - 1];
j--;
}//end while loop.
// insert the element
arr[j] = valueToSort;
}//end for loop
return arr;
}//end insertionSort
public static void printArray(int[] arr) {
System.out.println(Arrays.toString(arr));
}
}
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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