From 29bd32dfaef677168eb65fc9cf6306ac76a77dd9 Mon Sep 17 00:00:00 2001 From: Subhashini014 Date: Sat, 14 Oct 2023 12:51:18 +0530 Subject: [PATCH 1/2] Created a java solution for Insertion Sort --- JAVA/Insertionsort.java | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 JAVA/Insertionsort.java diff --git a/JAVA/Insertionsort.java b/JAVA/Insertionsort.java new file mode 100644 index 0000000..45b8045 --- /dev/null +++ b/JAVA/Insertionsort.java @@ -0,0 +1,32 @@ +import java.util.Scanner; + +public class Insertionsort{ + +public static void insertionsort(int arr[],int n){ + for(int i=0;i0 && arr[j-1]>arr[j])// It checks the comparision of numbers until the ith position and it makes sorted for every iteration. + { + int temp=arr[j-1]; + arr[j-1]=arr[j]; + arr[j]=temp; + j--; + } + } + System.out.println("After using InsertionSort"); + for(int i=0;i Date: Sat, 14 Oct 2023 13:52:10 +0530 Subject: [PATCH 2/2] Created a java solution for Selection Sort #27 --- JAVA/SelectionSort.java | 0 SelectionSort.java | 51 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 JAVA/SelectionSort.java create mode 100644 SelectionSort.java diff --git a/JAVA/SelectionSort.java b/JAVA/SelectionSort.java new file mode 100644 index 0000000..e69de29 diff --git a/SelectionSort.java b/SelectionSort.java new file mode 100644 index 0000000..e8273dd --- /dev/null +++ b/SelectionSort.java @@ -0,0 +1,51 @@ +import java.util.Scanner; + +public class SelectionSort { + + public static void selectionSort(int arr[], int n) { + for (int i = 0; i <= n - 2; i++) { + // Find the index of the minimum element + int minIndex = i; + + for (int j = i + 1; j < n; j++) { + if (arr[j] < arr[minIndex]) { + minIndex = j; + } + } + + // Swap the minimum element with the current element + int temp = arr[i]; + arr[i] = arr[minIndex]; + arr[minIndex] = temp; + + // Print the array after each swap + System.out.print("After " + i + " Comparison: "); + for (int k = 0; k < n; k++) { + System.out.print(arr[k] + " "); + } + System.out.println(); + } + System.out.println("After using Selection Sort:"); + for(int i=0;i