Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions Insertionsort.java
Original file line number Diff line number Diff line change
@@ -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<n;i++){
arr[i] = sc.nextInt();
}
sort(arr);
System.out.println("Sorted array");
System.out.println(Arrays.toString(arr));

}
}