Skip to content

Commit 5b28a1c

Browse files
committed
Add sorting algorithms file
1 parent 45a2fc3 commit 5b28a1c

File tree

6 files changed

+211
-0
lines changed

6 files changed

+211
-0
lines changed

BubbleShort.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
// Bubble Sort algorithm
6+
int BubbleShort(int a[], int n) {
7+
int i, j, temp;
8+
for (i = 0; i < n - 1; i++) {
9+
for (j = 0; j < n - i - 1; j++) {
10+
if (a[j] > a[j + 1]) {
11+
temp = a[j];
12+
a[j] = a[j + 1];
13+
a[j + 1] = temp;
14+
}
15+
}
16+
}
17+
return -1;
18+
}
19+
20+
21+
int main(){
22+
int arr[] = {64, 34, 25, 12, 22, 11, 90};
23+
int n = sizeof(arr)/sizeof(arr[0]);
24+
BubbleShort(arr, n);
25+
cout << "Sorted array: \n";
26+
for (int i=0; i<n; i++)
27+
cout << arr[i] << " ";
28+
return 0;
29+
}

InsertionSort.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
// Insertion Sort algorithm
6+
int InsertionSort(int *arr, int size){
7+
int i, j, key;
8+
for (i = 1; i < size; i++){
9+
key = arr[i];
10+
j = i - 1;
11+
while (j >= 0 && arr[j] > key){
12+
arr[j + 1] = arr[j];
13+
j = j - 1;
14+
}
15+
arr[j + 1] = key;
16+
}
17+
return *arr;
18+
}
19+
20+
21+
int main(){
22+
int arr[] = {64, 25, 12, 22, 11};
23+
int size = sizeof(arr) / sizeof(arr[0]);
24+
InsertionSort(arr, size);
25+
cout << "Sorted array: ";
26+
int i;
27+
for (i = 0; i < size; i++)
28+
cout << arr[i] << " ";
29+
30+
return 0;
31+
}

MergeSort.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include<iostream>
2+
#include<vector>
3+
#define MAX 100000
4+
using namespace std;
5+
6+
//You are given two sorted arrays A and B. You want to merge these arrays and return a sorted vector will elements from both arrays.
7+
void merge(vector<int> &a, vector<int> &b, vector<int> &toreturn)
8+
{
9+
int apointer = 0, bpointer = 0; //point to the first element in the vectors
10+
11+
//That hasn't been taken yet
12+
while(apointer<a.size() && bpointer<b.size())
13+
{
14+
if(a[apointer]<=b[bpointer]){
15+
toreturn.push_back(a[apointer++]);
16+
}
17+
else{ //b[bpointer] < a[apointer]
18+
// inv += (a.size() - apointer);
19+
toreturn.push_back(b[bpointer++]);
20+
}
21+
}
22+
while(apointer<a.size()) toreturn.push_back(a[apointer++]);
23+
while(bpointer<b.size()) toreturn.push_back(b[bpointer++]);
24+
}
25+
26+
void ms(int arr[], int s, int e){
27+
// cout<<s<<" "<<e<<endl;
28+
if(s==e){// there is only one or no element
29+
return;
30+
}
31+
int mid = (s+e)/2;
32+
ms(arr, s, mid);
33+
ms(arr, mid+1, e);
34+
35+
// Now arr[] has two sorted parts inside it.
36+
// We have to take them out, merge the two parts and
37+
// then resassign the values to arr[]
38+
vector<int> a,b;
39+
for(int i=s; i<=mid; i++) a.push_back(arr[i]);
40+
for(int i=mid+1; i<=e; i++) b.push_back(arr[i]);
41+
42+
vector<int> temp;
43+
merge(a,b, temp);
44+
for(int i=0; i<temp.size(); i++)
45+
arr[s+i] = temp[i];
46+
47+
}
48+
49+
50+
51+
int main()
52+
{
53+
int n;
54+
cin>>n;
55+
int arr[n];
56+
for(int i=0; i<n; i++) cin>>arr[i];
57+
ms(arr, 0, n-1);
58+
for(int i=0; i<n; i++) cout<<arr[i]<<" ";
59+
cout<<endl;
60+
}

SelectionSort.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int main(){
5+
6+
int n;
7+
cout<<"Enter the size of array:";
8+
cin>>n;
9+
int arr[n];
10+
cout<<"Enter the elements of array:";
11+
for(int i=0;i<n;i++){
12+
cin>>arr[i];
13+
}
14+
for(int i=0;i<n-1;i++){
15+
for(int j=i+1;j<n;j++){
16+
if(arr[j]<arr[i]){
17+
int temp=arr[j];
18+
arr[j]=arr[i];
19+
arr[i]=temp;
20+
}
21+
}
22+
}
23+
for(int i=0;i<n;i++){
24+
cout<<arr[i]<<" ";
25+
}
26+
cout<<endl;
27+
return 0;
28+
}

contributor.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,6 @@ Open Email Generator
7979
- [Alex](https://github.com/jokowhoiam)
8080
- [Faezal](https://github.com/Blazes05)
8181
- [Taufik](https://github.com/Taufikrhrp)
82+
- [Mahiuddin](https://github.com/mahiuddin-dev)
8283

8384
<!-- prettier-ignore-end -->

quick_sort.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
void swap(int* a, int* b)
6+
{
7+
int t = *a;
8+
*a = *b;
9+
*b = t;
10+
}
11+
12+
13+
int partition (int arr[], int low, int top)
14+
{
15+
int pivot = arr[top];
16+
int i = (low - 1);
17+
18+
for (int j = low; j <= top - 1; j++)
19+
{
20+
21+
if (arr[j] < pivot)
22+
{
23+
i++;
24+
swap(&arr[i], &arr[j]);
25+
}
26+
}
27+
swap(&arr[i + 1], &arr[top]);
28+
return (i + 1);
29+
}
30+
31+
32+
void quickSort(int arr[], int low, int top)
33+
{
34+
if (low < top)
35+
{
36+
37+
int pi = partition(arr, low, top);
38+
39+
40+
quickSort(arr, low, pi - 1);
41+
quickSort(arr, pi + 1, top);
42+
}
43+
}
44+
45+
46+
void printArray(int arr[], int size)
47+
{
48+
int i;
49+
for (i = 0; i < size; i++)
50+
cout << arr[i] << " ";
51+
cout << endl;
52+
}
53+
int main()
54+
{
55+
int arr[] = {10, 7, 8, 9, 1, 5};
56+
int n = sizeof(arr) / sizeof(arr[0]);
57+
quickSort(arr, 0, n - 1);
58+
cout << "Sorted array: \n";
59+
printArray(arr, n);
60+
return 0;
61+
}
62+

0 commit comments

Comments
 (0)