Skip to content

Commit 74f6976

Browse files
authored
Merge pull request larissalages#45 from kartikeysingh6/master
I've added 3 new sorting algorithms (Selection,Insertion,Bubble) in C++ language.
2 parents 8070823 + 2b73a76 commit 74f6976

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
void BubSort(int a[],int n){
5+
cout<<"Bubble Sort: ";
6+
for(int i=0;i<n-1;i++){ //total N-1 paas
7+
int temp;
8+
for(int j=0;j<n-i-1;j++){ //elements to be traversed are reduced on every pass
9+
if(a[j]>a[j+1]){
10+
temp=a[j];
11+
a[j]=a[j+1];
12+
a[j+1]=temp;}
13+
}
14+
}
15+
}
16+
17+
int main(){
18+
int arr[]={10,-4,7,3,1,0,2,6,99};
19+
int n=sizeof(arr)/sizeof(arr[0]);
20+
BubSort(arr,n);
21+
22+
for(int i=0;i<n;i++)
23+
cout<<arr[i]<<" ";
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
void InsSort(int arr[],int n){
5+
cout<<"Insertion Sort: ";
6+
for(int i=1;i<n;i++){
7+
int key=arr[i];
8+
int j=i-1;
9+
while(j>=0 && arr[j]>key){
10+
arr[j+1]=arr[j];
11+
j-=1;
12+
}
13+
arr[j+1]=key;
14+
}
15+
}
16+
17+
int main(){
18+
int arr[]={10,-4,7,3,1,0,2,6,99};
19+
int n=sizeof(arr)/sizeof(arr[0]);
20+
Insort(arr,n);
21+
22+
for(int i=0;i<n;i++)
23+
cout<<arr[i]<<" ";
24+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
void SelSort(int A[],int n){
5+
cout<<"Selection Sort: ";
6+
for(int i=0;i<n-1;i++){
7+
int i_min=i;
8+
for(int j=i+1;j<n;j++){
9+
if(A[j]<A[i_min])
10+
i_min=j;
11+
}
12+
int temp=A[i_min];
13+
A[i_min]=A[i];
14+
A[i]=temp;
15+
}
16+
}
17+
18+
int main(){
19+
int arr[]={10,-4,7,3,1,0,2,6,99};
20+
int n=sizeof(arr)/sizeof(arr[0]);
21+
SelSort(arr,n);
22+
23+
for(int i=0;i<n;i++)
24+
cout<<arr[i]<<" ";
25+
}

readme.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ Code | Test
2929
### C++
3030
Code | Test
3131
------------ | -------------
32+
[Selection Sort](https://github.com/kartikeysingh6/code_problems/blob/master/classical_algorithms/c%2B%2B/selectionsort.cpp) | Missing tests
33+
[Insertion Sort](https://github.com/kartikeysingh6/code_problems/blob/master/classical_algorithms/c++/insertionsort.cpp) | Missing tests
34+
[Bubble Sort](https://github.com/kartikeysingh6/code_problems/blob/master/classical_algorithms/c++/bubblesort.cpp) | Missing tests
3235
[Merge Sort](https://github.com/larissalages/code_problems/blob/master/classical_algorithms/c%2B%2B/mergesort.cpp) | Missing tests
3336

3437
# LeetCode

0 commit comments

Comments
 (0)