Skip to content

Commit cf16150

Browse files
imkakaMadhavBahl
authored andcommitted
Day32 (#222)
* Add @imkaka as a contributor * day4 * Day 32 Selection Sort
1 parent ac6bd98 commit cf16150

File tree

3 files changed

+95
-1
lines changed

3 files changed

+95
-1
lines changed

day32/C++/selectionSort

12.9 KB
Binary file not shown.

day32/C++/selectionSort.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 : 2/1/2019
4+
*
5+
*/
6+
7+
#include<iostream>
8+
9+
using namespace std;
10+
11+
void selectionSort(int arr[], int size){
12+
13+
for(int i = 0; i < size; ++i){
14+
for(int j = i+1; j < size; ++j){
15+
if(arr[i] > arr[j]){
16+
int temp = arr[i];
17+
arr[i] = arr[j];
18+
arr[j] = temp;
19+
}
20+
}
21+
}
22+
}
23+
24+
void print(int arr[], int size){
25+
for(int x = 0; x < size; ++x){
26+
cout << arr[x] << " ";
27+
}
28+
cout << endl;
29+
}
30+
int main(){
31+
32+
int arr[] = {20, 46, 2, 43, 10, -29, 10, 0, 12};
33+
int size = sizeof(arr)/sizeof(arr[0]);
34+
cout << "Before Sorting " << endl;
35+
print(arr, size);
36+
37+
selectionSort(arr, size);
38+
39+
cout << endl;
40+
cout << "After Sorting " << endl;
41+
print(arr, size);
42+
43+
return 0;
44+
}

day32/README.md

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,56 @@ function selectionSort(arr){
4949
console.log ( selectionSort ([1, 5, 2, 7, 3, 4, 8, 9, 6]));
5050
```
5151

52+
### [C++ Implementation](./C++/selectionSort.cpp)
53+
54+
```cpp
55+
/*
56+
* @author : imkaka
57+
* @date : 2/1/2019
58+
*
59+
*/
60+
61+
#include<iostream>
62+
63+
using namespace std;
64+
65+
void selectionSort(int arr[], int size){
66+
67+
for(int i = 0; i < size; ++i){
68+
for(int j = i+1; j < size; ++j){
69+
if(arr[i] > arr[j]){
70+
int temp = arr[i];
71+
arr[i] = arr[j];
72+
arr[j] = temp;
73+
}
74+
}
75+
}
76+
}
77+
78+
void print(int arr[], int size){
79+
for(int x = 0; x < size; ++x){
80+
cout << arr[x] << " ";
81+
}
82+
cout << endl;
83+
}
84+
int main(){
85+
86+
int arr[] = {20, 46, 2, 43, 10, -29, 10, 0, 12};
87+
int size = sizeof(arr)/sizeof(arr[0]);
88+
cout << "Before Sorting " << endl;
89+
print(arr, size);
90+
91+
selectionSort(arr, size);
92+
93+
cout << endl;
94+
cout << "After Sorting " << endl;
95+
print(arr, size);
96+
97+
return 0;
98+
}
99+
```
100+
101+
52102
## Java Implementation
53103
54104
### [Solution](./Java/selectionSort.java)
@@ -85,4 +135,4 @@ public class selectionSort {
85135
System.out.print(arr[i]+" ");
86136
}
87137
}
88-
```
138+
```

0 commit comments

Comments
 (0)