Skip to content

Commit a6eb2b6

Browse files
Update insertion_sort.java
1 parent 44c419c commit a6eb2b6

1 file changed

Lines changed: 5 additions & 5 deletions

File tree

Sorting/insertion_sort.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
// Java program for implementation of Insertion Sort
22
class InsertionSort {
3-
3+
//Function to sort array using insertion sort
44
void sort(int arr[])
55
{
6-
int n = arr.length;
6+
int n = arr.length;//checking the length of array
77
for (int i = 1; i < n; ++i) {
88
int m = arr[i];
99
int j = i - 1;
10-
10+
// Move elements of arr[0..i-1], that are greater than key, to one position ahead of their current position
1111
while (j >= 0 && arr[j] > m) {
1212
arr[j + 1] = arr[j];
1313
j = j - 1;
1414
}
1515
arr[j + 1] = m;
1616
}
1717
}
18-
18+
// function to print the array
1919
static void printArray(int arr[])
2020
{
2121
int n = arr.length;
@@ -25,7 +25,7 @@ static void printArray(int arr[])
2525
System.out.println();
2626
}
2727

28-
28+
// main method where we take input and call the function from above
2929
public static void main(String args[])
3030
{
3131
int arr[] = { 34, 1, 130, 15, 6 };

0 commit comments

Comments
 (0)