0
import java.util.Scanner;
import java.util.Arrays;

public class Main {

    public static void main(String[] args) {
        int i, temp, n, j ,num, array[], counter, k;
        Scanner input = new Scanner(System.in);
        num = input.nextInt();
        array = new int[num];
        n = array.length;  
        temp = 0;  
        for (counter = 0; counter < num; counter++)
        array[counter] = input.nextInt();

        for (i = 1; i < array.length; i++) {
            for (j = i; j > 0; j--) {
                if (array[j - 1] > array[j]) {
                    temp = array[j];
                    array[j] = array[j - 1];
                    array[j - 1] = temp;
                }
                for(k=0; k<n; k++)System.out.print(array[k]+ " ");
                System.out.print("\n");
            }
        }
    }
}

The code above is for insertion sorting an array. For example,

INPUT:

8
8 4 3 7 6 5 2 1

EXPECTED OUTPUT:

1 4 3 7 6 5 2 8
1 2 3 7 6 5 4 8
1 2 3 4 6 5 7 8
1 2 3 4 5 6 7 8

ACTUAL OUTPUT:

1 4 3 7 6 5 2 8 
1 2 3 7 6 5 4 8 
1 2 3 7 6 5 4 8 
1 2 3 4 6 5 7 8 
1 2 3 4 5 6 7 8 
1 2 3 4 5 6 7 8 
1 2 3 4 5 6 7 8 

The code works in some sense but I would like to remove all the duplicates. In other words, only print if the swap happens. Anyone can help? Thank you so much.

EDIT: When an already-sorted array is passed to the argument, the output should be nothing instead of printing the already-sorted array. For example,

INPUT:

5
1 2 3 4 5

OUTPUT:

3
  • 1
    You could move the print statement inside the if-statement. Or you could make another array to which you only add the arrays that were sorted in the if-statement, then print that one. Commented Oct 25, 2022 at 9:19
  • 1
    You say "only print if the swap happens". Then move the print into the if where the swap happens ;) Commented Oct 25, 2022 at 9:28
  • "The code works in some sense" - The algorithm you posted will not give that desired output. Try one of the other algorithms taught in class lol. It's the one that scans for the minimum element and then swaps :) Best of luck. Commented Oct 25, 2022 at 10:46

2 Answers 2

1

You can just add a flag variable inside the if statement and print only when flagged. Also try to always indent the code correctly, unformatted code can be very hard to read and debug, you can use an IDE like IntelliJ and it will handle it for you.

public class Main {
    public static void main(String[] args) {
        int i, temp, n, j, num, array[], counter, k;
        Scanner input = new Scanner(System.in);
        num = input.nextInt();
        array = new int[num];
        n = array.length;
        temp = 0;
        for (counter = 0; counter < num; counter++)
            array[counter] = input.nextInt();

        for (i = 1; i < array.length; i++) {
            boolean flag = false;
            for (j = i; j > 0; j--) {
                if (array[j - 1] > array[j]) {
                    temp = array[j];
                    array[j] = array[j - 1];
                    array[j - 1] = temp;
                    flag = true;
                }
            }
            if (flag) {
                for (k = 0; k < n; k++) System.out.println(array[k] + " ");
            }
        }
    }
}

also unlike C's standard library Java has a printLine function println(Object o) to which you can pass any object, and it will print on a new line so you don't have to add \n manually

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

2 Comments

Bro you have no idea how much this means to me. Thank you so much. There's no way I'm gonna figure this out myself cuz this is my second week learning Java.
Glad it helped, flag variables are one of the many little tricks in programming, better get used to them, there are also some other tricks you might use, and Java has the whole part of Object Oriented programing and Functional programming you might wanna check out if you truly wanna understand the language and work with it. Good luck further!
0

Please move the printing logic outside of the for loop like below

Also, to sort the array you're checking consecutive indices and doing the swap The output should look like this

[4, 8, 3, 7, 6, 5, 2, 1]
[3, 4, 8, 7, 6, 5, 2, 1]
[3, 4, 7, 8, 6, 5, 2, 1]
[3, 4, 6, 7, 8, 5, 2, 1]
[3, 4, 5, 6, 7, 8, 2, 1]
[2, 3, 4, 5, 6, 7, 8, 1]
[1, 2, 3, 4, 5, 6, 7, 8]

Code

 public class Main {
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            int num = input.nextInt();
            int[] array = new int[num];
            for (int counter = 0; counter < num; counter++) {
                array[counter] = input.nextInt();
            }
    
            int temp;
            //Sort the array in ascending order
            for (int i = 1; i < array.length; i++) {
                for (int j = i; j > 0; j--) {
                    if (array[j - 1] > array[j]) {
                        temp = array[j];
                        array[j] = array[j - 1];
                        array[j - 1] = temp;
                    }
    
                }
                System.out.println(Arrays.toString(array));
            }
        }
    }

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.