From a9e6a0b54f84ae70b0c88392d91013bcccbb0876 Mon Sep 17 00:00:00 2001 From: Coding Point <72379281+Coding-Point@users.noreply.github.com> Date: Thu, 8 Oct 2020 12:12:37 +0530 Subject: [PATCH] Create insertionsort --- Sorting/insertionsort | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Sorting/insertionsort diff --git a/Sorting/insertionsort b/Sorting/insertionsort new file mode 100644 index 00000000..49ea318a --- /dev/null +++ b/Sorting/insertionsort @@ -0,0 +1,28 @@ +public class insertionsort { + + public static void main(String[] args) { + // TODO Auto-generated method stub + int[] arr = { 10, 99,-1, 100, 50, 29 }; + + insrtionsort(arr); + + } + + public static void insrtionsort(int[] arr) { + + for (int i = 1; i < arr.length; i++) { + for (int j = i; j > 0; j--) { + if (arr[j] < arr[j - 1]) { + + int temp = arr[j]; + arr[j] = arr[j - 1]; + arr[j - 1] = temp; + } + } + } + for (int k = 0; k <= 4; k++) { + System.out.print(arr[k] + " "); + + } + } +}