-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionSort.java
More file actions
49 lines (39 loc) · 919 Bytes
/
Copy pathInsertionSort.java
File metadata and controls
49 lines (39 loc) · 919 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
Copyright (C) Deepali Srivastava - All Rights Reserved
This code is part of DSA course available on CourseGalaxy.com
*/
import java.util.Scanner;
public class InsertionSort
{
private InsertionSort(){} //this class is not for instantiation
public static void sort(int[] a, int n)
{
int i,j,temp;
for(i=1; i<n; i++)
{
temp=a[i];
for(j=i-1; j>=0 && a[j]>temp; j--)
a[j+1]=a[j];
a[j+1]=temp;
}
}
public static void main(String[] args)
{
int i,n;
int[] a = new int[20];
Scanner scan = new Scanner(System.in);
System.out.print("Enter the number of elements : ");
n = scan.nextInt();
for(i=0; i<n; i++)
{
System.out.print("Enter element " + (i+1) + " : ");
a[i] = scan.nextInt();
}
sort(a,n);
System.out.println("Sorted array is : ");
for(i=0; i<n; i++)
System.out.print(a[i] + " ");
System.out.println();
scan.close();
}
}