Question: Hello this is my method for a String Array insertion sort. It is returning bogus results to the console. Mainly just one array element over and over. Any help what needs to be changed is greatly appreciated.Thank you.

```
 public static void insertionSort(String[] a, int count) {
      int i, j;
      String Value;
      for (i = 1; i < count; i++) {
            Value = a[i];
            j = i - 1 ;
            while (j >= 0 && a[j].compareTo(Value)> 0) {
                  a[j+1] = a[j];
                  j=j-1;
            }
            a[i+1] = Value;

      }
       }

```

Answer: 
The problem is `a[j+1] = a[j];` 

Your original a[j+1] would be lost, you just simply replace it, without moving it / storing it, it is just simply replaced by a[j] and get lost...

I would modify your code like the following:
Find the position of current value to be inserted, swap it with the element at that position, then swap back that element into correct position

```js
 public static void insertionSort(String[] a, int count) {
      int i, j;
      String Value;
      for (i = 1; i < count; i++) {
            Value = a[i];
            j = i - 1 ;
            int p = i;
            while (j >= 0 && a[j].compareTo(Value)> 0) {    
                p = j--;
            }
            // p now is correct position to be inserted
            swap(a[p], a[i]);
            // Now loop the original a[p] back to a[p+1]
            for(int z = i; z > p; z--){
                 swap(a[z], a[z-1]);
            }
      }
 }
```
check the code works fine

```
 public static void insertionSort(int array[]) {
        int n = array.length;
        for (int j = 1; j < n; j++) {
            int key = array[j];
            int i = j-1;
            while ( (i > -1) && ( array [i] > key ) ) {
                array [i+1] = array [i];
                i--;
            }
            array[i+1] = key;
            printNumbers(array);
        }

```

and to print the number i use function printNumbers(array)

```
 private static void printNumbers(int[] input) {

        for (int i = 0; i < input.length; i++) {
            System.out.print(input[i] + ", ");
        }
        System.out.println("\n");
    }

```