From 7762a06726f07eaffca38c9895f602ea6f8693ad Mon Sep 17 00:00:00 2001 From: tushar Date: Sat, 31 Oct 2020 23:41:22 +0530 Subject: [PATCH] Added a sorting algorithm in java --- InsertionSort.java | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 InsertionSort.java diff --git a/InsertionSort.java b/InsertionSort.java new file mode 100644 index 0000000..c4786c8 --- /dev/null +++ b/InsertionSort.java @@ -0,0 +1,28 @@ +class InsertionSort { + static int arr[] = { 12, 11, 13, 5, 6 }; + + + 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; + } + for (int i = 0; i < arr.length; ++i) + System.out.print(arr[i] + " "); + } + + public static void main(String args[]) + { + + InsertionSort ob = new InsertionSort(); + ob.sort(arr); + } +} \ No newline at end of file