1

My objective is to create a working insertion sort that can handle strings and integers using the array given to us in the main method. For this example I called it list.

public class insort{

public static void main(String[]list){
  sort(list);
  printsort(list);
}//main

public static void sort(String[]list){
  for (int index = 1; index < list.length; index++){
    int key = list[index];
    int position = index;
    while (position > 0 && key.compareTo(list[position-1]) < 0){
        list[position] = list[position-1];
        position--;
    }//while
  list[position] = key;
 }//for
}//sort

public static void printsort(String[]list){
  while ( i < list.length){
    System.out.print(i);
  }//while
 }//printsort
}//insort
5
  • 1
    and what do you have problems with? what's the output of your code? Commented Nov 2, 2015 at 13:24
  • @eis hi, unfortunately, I am unable to complie the code as I am comparing strings to integers. I am unsure how to continue. Commented Nov 2, 2015 at 13:25
  • 1
    int key = list[index]; // This will return a String. Not an int. Compiler will show the error. Commented Nov 2, 2015 at 13:26
  • Perhaps the duplicate of stackoverflow.com/questions/17432738/… Commented Nov 2, 2015 at 13:32
  • @ThanigaiArasu I appreciate your comments and link. Commented Nov 2, 2015 at 14:26

2 Answers 2

1

Some of the things to note for successful compilation:

  1. The 'i' in the 'printsort' method is not declared and initialized. You will need to something like int i=0 before you start the while loop.

  2. Also the while loop variable needs to be updated inside the loop so as to meet an end condition. The end condition will help you stop the loop from going into an infinite loop. So you will need an i++ after your System.out.print().

  3. In the System.out.print() you are passing the variable i which will print the numbers from 0 to length-1 instead of the Strings in your list array. So you will need something like System.out.print(list[i]).

  4. In the 'sort' method you have int key = list[index], the RHS will return a String object instead of an int that you have on the LHS. So String key = list[index] is what you need there.

I haven't checked the code for proper working of insertion sort. Just pointed out some syntactic mistakes that will help you compile the code.

All the best :)

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! I really appreciate this.
0

You mixed couple things. It would be much easier to sort int or Integer objects, than String objects (especially that you need to be sure that String[] list contains only numerics), however if you want to sort String objects you need to change couple things:

  1. you try to assign a reference to String object to primitive variable int:

    int key = list[index];
    

    You can change it for example to:

    int key = Integer.valueOf(list[index]);
    

    or change list from String[] to int[].

  2. you try to use compareTo() method on primitive variable int:

    int key = list[index];
    (key.compareTo(list[position-1])) < 0
    

    Primitive variables doesn't have methods. You can solve the 1 and 2 problems with usage of Integer object instead of int variable:

    Integer key = Integer.valueOf(list[index]);
    

    or, if you want to stay with int variables, try with:

    key < list[position-1]
    

    instead of:

    (key.compareTo(list[position-1])) < 0
    
  3. your while loop is wrong:

    while ( i < list.length){
        System.out.print(i);
    }//while
    

    try with:

    for(String element : list){
        System.out.println(element);
    }
    

    to print every element of list.

also you changed a main method, from main(String[] args) to main(String[] list), and you try to use sort on String[] list. You should create new String[] array, and try with it, for example:

String[] list = {"4","6","1","8"};
sort(list);

1 Comment

Thank you. This helped me understand sorting much better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.