import java.util.*;

public class Sort {

    public static void main(String[] args) {
        //driver method
        int[] array = new int[10];
        System.out.println("Please enter ten integers");
        insertionSort(array);


    }

    public static void insertionSort(int[] array) {

        Scanner kybd = new Scanner(System.in);

        int min, temp, n = array.length;
        for (int i = 0; i < n; i++) {
            array[i] = kybd.nextInt();
        }

        for (int pass = 0; pass < n; pass++) {
            for (int i = 1; i < n; i++) {
                min = pass;		//  initialise subscript min
                if (array[i] < array[min]) {
                    min = i;	//  min so far
                    temp = array[pass];  	//  swap a[pass] & a[min]
                    array[pass] = array[min];
                    array[min] = temp;
                }	//  end of each pass

            }

            for (int j = 0; j < n; j++) {
                System.out.print(array[j] + " ");
            }
            System.out.print("\n");

        }


    }
}

Hey, having a real problem with this code. I'd like it to read in ten integers from the user and sort them from lowest to highest. I'd like most of the code to be unchanged- I don't think there is a massive problem- it's just getting a little confused?!?!

What exactly is the problem? Are you getting any errors? and unexpected output? Provide some details...

It compiles fine. The problem is, it sorts the integers, then decides to unsort them. The program seems to get confused as it does as I want- then undoes the process. The code seems to be conflicting :/

Cheers for any help - much appreciated!

How did you determine that?

I assume then, that this is not the real program? If not, then show the method, and how you are using it, from the real program.

This article has been dead for over six months. Start a new discussion instead.