Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Assignment #3
- import java.util.Scanner;
- public class Solver
- {
- public static void main(String args[])
- {
- System.out.println();
- try (Scanner input = new Scanner(System.in))
- {
- int size = input.nextInt();
- int a[] = new int [size];
- for (int i = 0; i < size; i++)
- {
- a[i] = input.nextInt();
- }
- size = insertion_sort(a, size);
- System.out.printf("\n-----\nNew Size = %d\n", size);
- for (int i = 0; i < size; i++)
- {
- System.out.printf("%d ", a[i]);
- }
- System.out.println("\n-----\n");
- }
- }
- public static int insertion_sort(int a[], int size)
- {
- for (int i = size - 2; i >= 0; i--)
- {
- int temp = a[i];
- int j = i + 1;
- boolean flag = false;
- while (j < size && (temp >= a[j] || flag == true))
- {
- a[j - 1] = a[j];
- if (a[j] == temp)
- {
- flag = true;
- }
- j++;
- }
- if (flag == true)size--;
- else a[j - 1] = temp;
- }
- System.out.println("\n-----\nDone, The Array is sorted\n-----");
- return size;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment