Skip to content

Commit b0fbd24

Browse files
Insertion Sort fom gfg is added
1 parent f5717ee commit b0fbd24

File tree

1 file changed

+243
-0
lines changed

1 file changed

+243
-0
lines changed
Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
---
2+
id: insertion-sort
3+
title: Insertion Sort
4+
sidebar_label: Insertion-Sort
5+
tags:
6+
- Sorting
7+
- Algorithms
8+
description: "This tutorial covers the solution to the Insertion Sort problem from the GeeksforGeeks."
9+
---
10+
## Problem Description
11+
12+
The task is to complete the `insert()` function which is used to implement Insertion Sort.
13+
14+
## Examples
15+
16+
**Example 1:**
17+
18+
```
19+
Input:
20+
N = 5
21+
arr[] = { 4, 1, 3, 9, 7}
22+
Output:
23+
1 3 4 7 9
24+
```
25+
26+
**Example 2:**
27+
28+
```
29+
Input:
30+
N = 10
31+
arr[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}
32+
Output:
33+
1 2 3 4 5 6 7 8 9 10
34+
```
35+
36+
## Your Task
37+
You don't have to read input or print anything. Your task is to complete the function insert() and insertionSort() where insert() takes the array, it's size and an index i and insertionSort() uses insert function to sort the array in ascending order using insertion sort algorithm.
38+
39+
Expected Time Complexity: O(N^2)
40+
41+
Expected Auxiliary Space: O(1)
42+
43+
## Constraints
44+
45+
* `1 ≤ N ≤ 10^3`
46+
47+
## Problem Explanation
48+
49+
The task is to traverse the array and sort the array using insertion sort.
50+
51+
## Code Implementation
52+
53+
### C++ Solution
54+
55+
```cpp
56+
// C++ program for insertion sort
57+
58+
#include <bits/stdc++.h>
59+
using namespace std;
60+
61+
// Function to sort an array using
62+
// insertion sort
63+
void insertionSort(int arr[], int n)
64+
{
65+
int i, key, j;
66+
for (i = 1; i < n; i++) {
67+
key = arr[i];
68+
j = i - 1;
69+
70+
// Move elements of arr[0..i-1],
71+
// that are greater than key,
72+
// to one position ahead of their
73+
// current position
74+
while (j >= 0 && arr[j] > key) {
75+
arr[j + 1] = arr[j];
76+
j = j - 1;
77+
}
78+
arr[j + 1] = key;
79+
}
80+
}
81+
82+
// A utility function to print an array
83+
// of size n
84+
void printArray(int arr[], int n)
85+
{
86+
int i;
87+
for (i = 0; i < n; i++)
88+
cout << arr[i] << " ";
89+
cout << endl;
90+
}
91+
92+
// Driver code
93+
int main()
94+
{
95+
int arr[] = { 12, 11, 13, 5, 6 };
96+
int N = sizeof(arr) / sizeof(arr[0]);
97+
98+
insertionSort(arr, N);
99+
printArray(arr, N);
100+
101+
return 0;
102+
}
103+
```
104+
105+
```java
106+
// Java program for implementation of Insertion Sort
107+
public class InsertionSort {
108+
/*Function to sort array using insertion sort*/
109+
void sort(int arr[])
110+
{
111+
int n = arr.length;
112+
for (int i = 1; i < n; ++i) {
113+
int key = arr[i];
114+
int j = i - 1;
115+
116+
/* Move elements of arr[0..i-1], that are
117+
greater than key, to one position ahead
118+
of their current position */
119+
while (j >= 0 && arr[j] > key) {
120+
arr[j + 1] = arr[j];
121+
j = j - 1;
122+
}
123+
arr[j + 1] = key;
124+
}
125+
}
126+
127+
/* A utility function to print array of size n*/
128+
static void printArray(int arr[])
129+
{
130+
int n = arr.length;
131+
for (int i = 0; i < n; ++i)
132+
System.out.print(arr[i] + " ");
133+
134+
System.out.println();
135+
}
136+
137+
// Driver method
138+
public static void main(String args[])
139+
{
140+
int arr[] = { 12, 11, 13, 5, 6 };
141+
142+
InsertionSort ob = new InsertionSort();
143+
ob.sort(arr);
144+
145+
printArray(arr);
146+
}
147+
};
148+
```
149+
150+
```python
151+
# Python program for implementation of Insertion Sort
152+
153+
# Function to do insertion sort
154+
def insertionSort(arr):
155+
156+
# Traverse through 1 to len(arr)
157+
for i in range(1, len(arr)):
158+
159+
key = arr[i]
160+
161+
# Move elements of arr[0..i-1], that are
162+
# greater than key, to one position ahead
163+
# of their current position
164+
j = i-1
165+
while j >= 0 and key < arr[j] :
166+
arr[j + 1] = arr[j]
167+
j -= 1
168+
arr[j + 1] = key
169+
170+
171+
# Driver code to test above
172+
arr = [12, 11, 13, 5, 6]
173+
insertionSort(arr)
174+
for i in range(len(arr)):
175+
print ("% d" % arr[i])
176+
177+
```
178+
179+
```javascript
180+
181+
<script>
182+
// Javascript program for insertion sort
183+
184+
// Function to sort an array using insertion sort
185+
function insertionSort(arr, n)
186+
{
187+
let i, key, j;
188+
for (i = 1; i < n; i++)
189+
{
190+
key = arr[i];
191+
j = i - 1;
192+
193+
/* Move elements of arr[0..i-1], that are
194+
greater than key, to one position ahead
195+
of their current position */
196+
while (j >= 0 && arr[j] > key)
197+
{
198+
arr[j + 1] = arr[j];
199+
j = j - 1;
200+
}
201+
arr[j + 1] = key;
202+
}
203+
}
204+
205+
// A utility function to print an array of size n
206+
function printArray(arr, n)
207+
{
208+
let i;
209+
for (i = 0; i < n; i++)
210+
document.write(arr[i] + " ");
211+
document.write("<br>");
212+
}
213+
214+
// Driver code
215+
let arr = [12, 11, 13, 5, 6 ];
216+
let n = arr.length;
217+
218+
insertionSort(arr, n);
219+
printArray(arr, n);
220+
221+
222+
</script>
223+
```
224+
225+
## Solution Logic:
226+
227+
1. We have to start with second element of the array as first element in the array is assumed to be sorted.
228+
229+
2. Compare second element with the first element and check if the second element is smaller then swap them.
230+
231+
3. Move to the third element and compare it with the second element, then the first element and swap as necessary to put it in the correct position among the first three elements.
232+
233+
4. Continue this process, comparing each element with the ones before it and swapping as needed to place it in the correct position among the sorted elements.
234+
235+
5. Repeat until the entire array is sorted.
236+
237+
## Time Complexity
238+
239+
* The time complexity is $O(N^2)$ as there are two nested loops
240+
241+
## Space Complexity
242+
243+
* The auxiliary space complexity is $O(1)$ due to the only extra memory used is for temporary variables while swapping two values in Array. The selection sort never makes more than $O(N)$ swaps and can be useful when memory writing is costly.

0 commit comments

Comments
 (0)