Skip to content

Commit 4d87ae2

Browse files
authored
Create Anurupa
java code
1 parent 94df794 commit 4d87ae2

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

Diff for: Java/Anurupa

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Java program for implementation of Insertion Sort
2+
public class InsertionSort {
3+
/*Function to sort array using insertion sort*/
4+
void sort(int arr[])
5+
{
6+
int n = arr.length;
7+
for (int i = 1; i < n; ++i) {
8+
int key = arr[i];
9+
int j = i - 1;
10+
11+
/* Move elements of arr[0..i-1], that are
12+
greater than key, to one position ahead
13+
of their current position */
14+
while (j >= 0 && arr[j] > key) {
15+
arr[j + 1] = arr[j];
16+
j = j - 1;
17+
}
18+
arr[j + 1] = key;
19+
}
20+
}
21+
22+
/* A utility function to print array of size n*/
23+
static void printArray(int arr[])
24+
{
25+
int n = arr.length;
26+
for (int i = 0; i < n; ++i)
27+
System.out.print(arr[i] + " ");
28+
29+
System.out.println();
30+
}
31+
32+
// Driver method
33+
public static void main(String args[])
34+
{
35+
int arr[] = { 12, 11, 13, 5, 6 };
36+
37+
InsertionSort ob = new InsertionSort();
38+
ob.sort(arr);
39+
40+
printArray(arr);
41+
}
42+
};
43+
44+

0 commit comments

Comments
 (0)