File tree 1 file changed +49
-0
lines changed
1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ Copyright (C) Deepali Srivastava - All Rights Reserved
3
+ This code is part of DSA course available on CourseGalaxy.com
4
+ */
5
+
6
+ import java .util .Scanner ;
7
+
8
+ public class InsertionSort
9
+ {
10
+ private InsertionSort (){} //this class is not for instantiation
11
+
12
+ public static void sort (int [] a , int n )
13
+ {
14
+ int i ,j ,temp ;
15
+ for (i =1 ; i <n ; i ++)
16
+ {
17
+ temp =a [i ];
18
+
19
+ for (j =i -1 ; j >=0 && a [j ]>temp ; j --)
20
+ a [j +1 ]=a [j ];
21
+
22
+ a [j +1 ]=temp ;
23
+ }
24
+ }
25
+
26
+ public static void main (String [] args )
27
+ {
28
+ int i ,n ;
29
+ int [] a = new int [20 ];
30
+ Scanner scan = new Scanner (System .in );
31
+
32
+ System .out .print ("Enter the number of elements : " );
33
+ n = scan .nextInt ();
34
+
35
+ for (i =0 ; i <n ; i ++)
36
+ {
37
+ System .out .print ("Enter element " + (i +1 ) + " : " );
38
+ a [i ] = scan .nextInt ();
39
+ }
40
+
41
+ sort (a ,n );
42
+
43
+ System .out .println ("Sorted array is : " );
44
+ for (i =0 ; i <n ; i ++)
45
+ System .out .print (a [i ] + " " );
46
+ System .out .println ();
47
+ scan .close ();
48
+ }
49
+ }
You can’t perform that action at this time.
0 commit comments