From a94c7bd8829510193f4be5b58b7473648417d48f Mon Sep 17 00:00:00 2001 From: Umang Bariya <89646142+Umang-Bariya@users.noreply.github.com> Date: Tue, 4 Oct 2022 22:12:28 +0530 Subject: [PATCH] Implement Insertion-Sort in Java This program doing Insertion sort using java with user Input. --- Insertionsort.java | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Insertionsort.java diff --git a/Insertionsort.java b/Insertionsort.java new file mode 100644 index 0000000..7ca1817 --- /dev/null +++ b/Insertionsort.java @@ -0,0 +1,34 @@ +import java.util.Arrays; +import java.util.Scanner; + +public class InsertionSort{ + public static void sort(int arr[]) + { + int n = arr.length; + for (int i = 1; i < n; ++i) { + int key = arr[i]; + int j = i - 1; + while (j >= 0 && arr[j] > key) { + arr[j + 1] = arr[j]; + j = j - 1; + } + arr[j + 1] = key; + } + } + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int n; + System.out.print("Enter Size of Array:"); + n = sc.nextInt(); + int arr[] = new int[n]; + System.out.print("Enter Array:"); + for(int i=0;i