-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertion_sort.java
More file actions
52 lines (41 loc) · 1.3 KB
/
Insertion_sort.java
File metadata and controls
52 lines (41 loc) · 1.3 KB
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
50
51
52
// Insertion sort Program
package insertion_sort;
public class Insertion_sort {
// this function is for insertion sort
public static void Insertion_Sort (int [] Array)
{
for (int i = 1; i < Array.length; i++)
{
int temp = Array[i];
int x;
for (x = i; x > 0 && temp < Array[x - 1]; x--)
{
Array[x] = Array[x - 1];
}
Array[x] = temp;
// This statment print the array
Display(Array);
}
}
public static void Display(int [] array)
{
for (int i = 0; i < array.length; i++)
{
System.out.print(array[i] + " ");
}
System.out.println("");
}
public static void main(String[] args) {
int [] array = {5,17,8,9,3,6,1,13};
// before sortin of array
System.out.print("Before sorting Array : ");
Display(array);
System.out.println("");
// Calling sort function
Insertion_Sort(array);
// Printing array after sorting
System.out.println("");
System.out.print("After sorting Array : ");
Display(array);
}
}