Home > Mobile >  How to sort an array and print the process without duplicates using Java
How to sort an array and print the process without duplicates using Java

Time:10-25

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:

CodePudding user response:

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]   " ");
            }
        }
    }
}

CodePudding user response:

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));
            }
        }
    }
  • Related