Skip to content

Commit ac6bd98

Browse files
imkakaMadhavBahl
authored andcommitted
Day33 (#229)
* Add @imkaka as a contributor * day4 * Insertion Sort
1 parent e87a474 commit ac6bd98

File tree

3 files changed

+94
-1
lines changed

3 files changed

+94
-1
lines changed

day33/C++/insertionSort

12.9 KB
Binary file not shown.

day33/C++/insertionSort.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* @author : imkaka
3+
* @date : 4/2/2019
4+
*/
5+
6+
#include<iostream>
7+
8+
using namespace std;
9+
10+
void insertionSort(int arr[], int size){
11+
int key , j;
12+
for(int i = 1; i < size; ++i){
13+
14+
key = arr[i];
15+
j = i-1;
16+
17+
while (j >= 0 && arr[j] > key)
18+
{
19+
arr[j+1] = arr[j];
20+
j = j-1;
21+
}
22+
arr[j+1] = key;
23+
}
24+
}
25+
26+
void print(int* arr, int size){
27+
for(int i = 0; i < size; ++i)
28+
cout << arr[i] << " ";
29+
}
30+
int main(){
31+
32+
int arr[] = {10, 12, 0, 36, -4, 2, 3, -36, 20};
33+
int size = sizeof(arr) / sizeof(arr[0]);
34+
35+
cout << "Before Sorting: " << endl;
36+
print(arr,size);
37+
cout << endl;
38+
39+
insertionSort(arr, size);
40+
41+
cout << "After Sorting: " << endl;
42+
print(arr,size);
43+
return 0;
44+
}

day33/README.md

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,55 @@ function insertionSort(arr){
3535
console.log ( insertionSort ([1, 5, 2, 7, 3, 4, 8, 9, 6]));
3636
```
3737

38+
### [C++ Solution](./C++/insertionSort.cpp)
39+
40+
```cpp
41+
/*
42+
* @author : imkaka
43+
* @date : 4/2/2019
44+
*/
45+
46+
#include<iostream>
47+
48+
using namespace std;
49+
50+
void insertionSort(int arr[], int size){
51+
int key , j;
52+
for(int i = 1; i < size; ++i){
53+
54+
key = arr[i];
55+
j = i-1;
56+
57+
while (j >= 0 && arr[j] > key)
58+
{
59+
arr[j+1] = arr[j];
60+
j = j-1;
61+
}
62+
arr[j+1] = key;
63+
}
64+
}
65+
66+
void print(int* arr, int size){
67+
for(int i = 0; i < size; ++i)
68+
cout << arr[i] << " ";
69+
}
70+
int main(){
71+
72+
int arr[] = {10, 12, 0, 36, -4, 2, 3, -36, 20};
73+
int size = sizeof(arr) / sizeof(arr[0]);
74+
75+
cout << "Before Sorting: " << endl;
76+
print(arr,size);
77+
cout << endl;
78+
79+
insertionSort(arr, size);
80+
81+
cout << "After Sorting: " << endl;
82+
print(arr,size);
83+
return 0;
84+
}
85+
```
86+
3887
## Java Implementation
3988
4089
### [Solution](./Java/insertionSort.java)
@@ -70,4 +119,4 @@ public class insertionSort {
70119
sort(a);
71120
}
72121
}
73-
```
122+
```

0 commit comments

Comments
 (0)