4 Replies Latest reply: May 9, 2008 10:28 PM by 807601 RSS

    Insertion sort algorithm problems?

    807601
      I'm writing a program that is supposed to sort an array and i'm using the insertion sort algorithm.
      the problem i'm having is that its not printing the rearanged array, i have it printing the unordered array but i can't get it to print the ordered one.

      Here is my code:
      anyone tell me what i have wrong with it?
            public class Hw7_2
      {
         public static void main(String[] args)
         {
            int[] list = {11, 5, 2, 8, 1, 3, 10, 7, 4, 6, 9, 8};
            int length = list.length;
            printArray(list);
      
      
         }
      
      public static void insertionSort(int[] list, int length) {
          int firstOutOfOrder, location, temp;
          
          for(firstOutOfOrder = 1; firstOutOfOrder < length; firstOutOfOrder++) { 
              if(list[firstOutOfOrder] < list[firstOutOfOrder - 1]) { 
                  temp = list[firstOutOfOrder];
                  location = firstOutOfOrder;
                  
                  do { 
                      list[location] = list[location-1];
                      location--;
                  }
                  while (location > 0 && list[location-1] > temp);
                  
                  list[location] = temp;
              }
          }
      }
         public static void printArray(int[] temp)
         {
            for (int i=0; i<temp.length; i++)
              System.out.print(temp[i] + " ");
            System.out.println("");
         }
      }