-
Notifications
You must be signed in to change notification settings - Fork 283
/
Copy pathInsertionSort
103 lines (84 loc) · 3.72 KB
/
InsertionSort
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
##Insertion Sort Algorithm
In this article, we will discuss the Insertion sort Algorithm. The working procedure of insertion sort is also simple. This article will be very helpful and interesting to students as they might face insertion sort as a question in their examinations. So, it is important to discuss the topic.
Insertion sort works similar to the sorting of playing cards in hands. It is assumed that the first card is already sorted in the card game, and then we select an unsorted card. If the selected unsorted card is greater than the first card, it will be placed at the right side; otherwise, it will be placed at the left side. Similarly, all unsorted cards are taken and put in their exact place.
The same approach is applied in insertion sort. The idea behind the insertion sort is that first take one element, iterate it through the sorted array. Although it is simple to use, it is not appropriate for large data sets as the time complexity of insertion sort in the average case and worst case is O(n2), where n is the number of items. Insertion sort is less efficient than the other sorting algorithms like heap sort, quick sort, merge sort, etc.
Insertion sort has various advantages such as -
Simple implementation
Efficient for small data sets
Adaptive, i.e., it is appropriate for data sets that are already substantially sorted.
Now, let's see the algorithm of insertion sort.
##Algorithm
The simple steps of achieving the insertion sort are listed as follows -
Step 1 - If the element is the first element, assume that it is already sorted. Return 1.
Step2 - Pick the next element, and store it separately in a key.
Step3 - Now, compare the key with all elements in the sorted array.
Step 4 - If the element in the sorted array is smaller than the current element, then move to the next element. Else, shift greater elements in the array towards the right.
Step 5 - Insert the value.
Step 6 - Repeat until the array is sorted.
## Insertion Sort using Java
public class Insert
{
void insert(int a[]) /* function to sort an aay with insertion sort */
{
int i, j, temp;
int n = a.length;
for (i = 1; i < n; i++) {
temp = a[i];
j = i - 1;
while(j>=0 && temp <= a[j]) /* Move the elements greater than temp to one position ahead from their current position*/
{
a[j+1] = a[j];
j = j-1;
}
a[j+1] = temp;
}
}
void printArr(int a[]) /* function to print the array */
{
int i;
int n = a.length;
for (i = 0; i < n; i++)
System.out.print(a[i] + " ");
}
public static void main(String[] args) {
int a[] = { 92, 50, 5, 20, 11, 22 };
Insert i1 = new Insert();
System.out.println("\nBefore sorting array elements are - ");
i1.printArr(a);
i1.insert(a);
System.out.println("\n\nAfter sorting array elements are - ");
i1.printArr(a);
System.out.println();
}
}
Output:
Insertion Sort Algorithm
Program: Write a program to implement insertion sort in PHP.
<?php
$a = array( 92, 50, 5, 20, 11, 22 );
function printArray($a)
{
for($i = 0; $i < 6; $i++)
{
print_r($a[$i]);
echo " ";
}
}
echo "Before sorting array elements are - <br>";
printArray($a);
for ($i = 1; $i < 6; $i++)
{
$temp = $a[$i];
$j = $i - 1;
while($j >= 0 && $temp <= $a[$j]) /* Move the elements greater than temp to one position ahead from their current position*/
{
$a[$j+1] = $a[$j];
$j = $j-1;
}
$a[$j+1] = $temp;
}
echo "<br> After sorting array elements are - <br>";
printArray($a);
?>
Output:
Insertion Sort Algorithm