-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionSort.java
More file actions
68 lines (67 loc) · 1.83 KB
/
InsertionSort.java
File metadata and controls
68 lines (67 loc) · 1.83 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Program: Insertion Sort Algorithm
// Topic: Sorting Algorithms (Arrays)
// Description: Implements the Insertion Sort technique to arrange an array of integers in ascending order.
// Takes user input for array elements, then iteratively inserts each element into its correct position while printing the array after each iteration.
package Sorting;
import java.util.*;
/**
*
* @author Samim
*/
public class InsertionSort
{
static int data[]=new int[5];
static int i;
static int loc;
static int temp;
static void dataPrint()
{
System.out.println("run()executed :i = "+i+" data =");
for(int j=0;j<data.length;j++)
{
System.out.print(data[j]);
if(j<data.length-1)
{
System.out.print(", ");
}
}
System.out.println("""
""");
System.out.println("Elements Of Array");
}
static void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter The Elements Of Array :");
for(int i=0;i<5;i++)
{
data[i]=sc.nextInt();
}
}
public static void main(String args[])
{
input();
System.out.println("Sorting (Re)started");
i=0;
dataPrint();
for(i++ ;i<data.length;i++)
{
temp=data[i];
loc=i;
for(;loc>=0;loc--)
{
if(loc==0||data[loc-1]<=temp)
{
data[loc]=temp;
break;
}
else
{
data[loc]=data[loc-1];
}
}
dataPrint();
}
System.out.println("Sorting Completed");
}
}