-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from AyushhS/bubble_sort,merge_sort,quick_sort…
…,selection_sort_in_cpp addition of bubble sort, merge sort, quick sort and selection sort in C++
- Loading branch information
Showing
4 changed files
with
350 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// BUBBLE SORT IN C++ | ||
|
||
/* | ||
Working of the bubble sort algorithm - | ||
this sorting algorithm works by comparision of elements. It basically compares 2 adjacent elements and checks if they are sorted or not. for 2 elements there are only 2 possiblities - a[i + 1] > a[i] or a[i] < a[i + 1]. | ||
if the first case is true, it swaps the 2 elements so that the 2 elements is sorted. | ||
this is done for all the elements in the array, till the largest element of the array reaches its designated position, that is, the largest element reaches the last position of the array (bubbles to the end of the array, hence bubble sort). | ||
thus, this process is repeated till all the elements are in their desired positions. | ||
*/ | ||
|
||
/* | ||
Time complexity of the algorithm - O(n^2) | ||
Space complexiy - O(1) | ||
*/ | ||
|
||
#include <iostream> | ||
#include <vector> //for vector arrays | ||
using namespace std; | ||
|
||
//bubble sort function | ||
void bubblesort(vector <int>* a) { | ||
|
||
int n = (*a).size(); | ||
|
||
//main loop for all the elements | ||
for (int i = 0; i < n; i++) { | ||
|
||
//internal loop for doing the sorting | ||
// NOTE - (j < n - i), since after i elements being sorted at the end, there is no need to reach those elements. | ||
for (int j = 1; j < n - i; j++) { | ||
if ((*a)[j - 1] > (*a)[j]) { | ||
|
||
//swap | ||
swap((*a)[j], (*a)[j - 1]); | ||
} | ||
} | ||
|
||
} | ||
|
||
return; | ||
} | ||
|
||
// Driver Code | ||
int main() { | ||
|
||
//input parameters | ||
int n; | ||
cout << "Enter size of array = "; | ||
cin >> n; | ||
vector <int> a(n); | ||
cout << "Enter elements of array = "; | ||
for (int i = 0; i < n; i++) { | ||
cin >> a[i]; | ||
} | ||
|
||
//function call | ||
bubblesort(&a); | ||
|
||
//output | ||
cout << "Sorted array (using bubble sort) = "; | ||
for (int i = 0; i < n; i++) { | ||
cout << a[i] << " "; | ||
} | ||
cout << "\n"; | ||
|
||
return 0; | ||
} | ||
|
||
/* | ||
Example I/O - | ||
Enter size of array = 10 | ||
Enter elements of array = 2 3 8 19 0 1 2 5 6 22 9 | ||
Sorted array (using bubble sort) = 0 1 2 2 3 5 6 8 19 22 | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
//MERGE SORT IN C++ | ||
|
||
/* | ||
Working of the merge sort algorithm - | ||
It basically works on the principle of recursion. In this, we divide the array into 2 equal parts, and run the mergesort() function recursively on both the divided arrays, which become sorted throught mergesort() function. now, with a merge function, we combine the 2 arrays to form the full array. | ||
the merge() function works by | ||
*/ | ||
|
||
//Time complexity - O(nlogn) | ||
//Space complexity - O(n) [since 2 new arrays are created for sorting and merging] | ||
|
||
#include <iostream> | ||
#include <vector> //for vector arrays | ||
using namespace std; | ||
|
||
//merge function | ||
vector <int> merge(vector <int>* ls, vector <int>* rs) { | ||
|
||
//variables initialization | ||
vector <int> res; | ||
vector<int>::iterator lb = ls->begin(), rb = rs->begin(); | ||
|
||
//placing the values from left and right arrays | ||
while (lb != ls->end() && rb != rs->end()) { | ||
if (*lb > *rb) { | ||
res.push_back(*rb); | ||
rb++; | ||
} | ||
else { | ||
res.push_back(*lb); | ||
lb++; | ||
} | ||
} | ||
|
||
//placing the remaning values | ||
while (lb != ls->end()) { | ||
res.push_back(*lb); | ||
lb++; | ||
} | ||
while (rb != rs->end()) { | ||
res.push_back(*rb); | ||
rb++; | ||
} | ||
|
||
return res; | ||
} | ||
|
||
//merge sort function | ||
vector <int> mergesort(vector <int>* arr) { | ||
|
||
//variables initialization | ||
vector <int> res; | ||
int m = ((*arr).size() + 1) / 2; | ||
vector <int> ls; | ||
vector <int> rs; | ||
|
||
//breaking condition | ||
if ((*arr).size() <= 1) { | ||
return *arr; | ||
} | ||
|
||
//splitting into left and right arrays | ||
for (int i = 0; i < m; i++) { | ||
ls.push_back((*arr)[i]); | ||
} | ||
for (int i = m; i < (*arr).size(); i++) { | ||
rs.push_back((*arr)[i]); | ||
} | ||
|
||
//recursive calls | ||
ls = mergesort(&ls); | ||
rs = mergesort(&rs); | ||
res = merge(&ls, &rs); | ||
|
||
return res; | ||
} | ||
|
||
//Driver code | ||
int main() { | ||
|
||
//input parameters | ||
int n; | ||
cout << "Enter size of array = "; | ||
cin >> n; | ||
vector <int> a(n); | ||
cout << "Enter elements of array = "; | ||
for (int i = 0; i < n; i++) { | ||
cin >> a[i]; | ||
} | ||
|
||
//function call | ||
a = mergesort(&a); | ||
|
||
//output | ||
cout << "Sorted array (using merge sort) = "; | ||
for (int i = 0; i < n; i++) { | ||
cout << a[i] << " "; | ||
} | ||
cout << "\n"; | ||
|
||
return 0; | ||
} | ||
|
||
/* | ||
Example I/O - | ||
Enter size of array = 10 | ||
Enter elements of array = 4 10 2 0 9 1 3 8 55 20 | ||
Sorted array (using merge sort) = 0 1 2 3 4 8 9 10 20 55 | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
//QUICK SORT IN C++ | ||
|
||
/* | ||
Working of quick sort algorithm - | ||
It works similiar to merge sort, and is a divide and conquer algorithm, and uses recursion as its main working principle. just like merge sort, it divides the array, but not long its center, but along its "pivot", which can be anything in the array. In this code, the pivot is the last element of the array. | ||
the split() function takes the pivot and places all the elements smaller than the pivot on the left side of it, and the others on its right side, and returns the position of the pivot. | ||
taking the pivot as the baseline, the array is split and the quicksort() function is applied recursively. | ||
*/ | ||
|
||
/* | ||
Time complexity - O(n^2) [worst case] || O(nlogn) [best case] | ||
Space complexity - O(1) | ||
*/ | ||
|
||
#include <iostream> | ||
#include <vector> //for vector arrays | ||
using namespace std; | ||
|
||
//split function | ||
int split(vector <int>* a, int l, int r) { | ||
|
||
//initialization of useful variables | ||
int pivot = (*a)[r]; | ||
int iter = l - 1; //for keeping track of smaller elements | ||
|
||
//main loop | ||
for (int j = l; j < r; j++) { | ||
|
||
if ((*a)[j] <= pivot) { | ||
iter++; | ||
|
||
//placing smaller elements on the left side, and ignoring the rest | ||
swap((*a)[iter], (*a)[j]); | ||
} | ||
} | ||
|
||
//placing pivot on the elements ignored previously | ||
swap((*a)[iter + 1], (*a)[r]); | ||
|
||
//returning pivot's location | ||
return iter + 1; | ||
} | ||
|
||
//quick sort function | ||
void quicksort(vector <int>* a, int l, int r) { | ||
|
||
//breaking condition | ||
if (l >= r) { | ||
return; | ||
} | ||
|
||
//recursive calls | ||
int p = split(a, l, r); | ||
quicksort(a, l, p - 1); | ||
quicksort(a, p, r); | ||
} | ||
|
||
int main() { | ||
|
||
//input parameters | ||
int n; | ||
cout << "Enter size of array = "; | ||
cin >> n; | ||
vector <int> a(n); | ||
cout << "Enter elements of array = "; | ||
for (int i = 0; i < n; i++) { | ||
cin >> a[i]; | ||
} | ||
|
||
//function call | ||
quicksort(&a, 0, n - 1); | ||
|
||
//output | ||
cout << "Sorted array (using bubble sort) = "; | ||
for (int i = 0; i < n; i++) { | ||
cout << a[i] << " "; | ||
} | ||
cout << "\n"; | ||
|
||
return 0; | ||
} | ||
|
||
/* | ||
Example I/O - | ||
Enter size of array = 10 | ||
Enter elements of array = 8 9 5 3 10 11 21 88 7 0 2 | ||
Sorted array (using bubble sort) = 0 3 5 7 8 9 10 11 21 88 | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
//SELECTION SORT IN C++ | ||
|
||
/* | ||
Working of the selection sort algorithm - | ||
this algorithm works by finding out the minimum element of the array, and placing it at the front of the array, and repeating it for every element in the array. It is similiar to the working of bubble sort, but here instead of placing the highest element to the extreme right, we do the opposite. | ||
*/ | ||
|
||
/* | ||
Time complexity - O(n^2) | ||
Space complexity - O(1) | ||
*/ | ||
|
||
#include <iostream> | ||
#include <vector> //for vector arrays | ||
using namespace std; | ||
|
||
//Selection sort function | ||
void selectionsort(vector <int>* a) { | ||
|
||
//variables initialization | ||
int n = a->size(); | ||
|
||
//main loop for all the elements | ||
for (int i = 0; i < n; i++) { | ||
|
||
//finding minimum element | ||
int min = (*a)[i]; | ||
int minpos = i; | ||
for (int j = i + 1; j < n; j++) { //loop for finding minimum element | ||
if (min > (*a)[j]) { | ||
min = (*a)[j]; | ||
minpos = j; | ||
} | ||
} | ||
|
||
//updating final array and swapping minimum and current element | ||
swap((*a)[i], (*a)[minpos]); | ||
} | ||
} | ||
|
||
int main() { | ||
|
||
//input parameters | ||
int n; | ||
cout << "Enter size of array = "; | ||
cin >> n; | ||
vector <int> a(n); | ||
cout << "Enter elements of array = "; | ||
for (int i = 0; i < n; i++) { | ||
cin >> a[i]; | ||
} | ||
|
||
//function call | ||
selectionsort(&a); | ||
|
||
//output | ||
cout << "Sorted array (using bubble sort) = "; | ||
for (int i = 0; i < n; i++) { | ||
cout << a[i] << " "; | ||
} | ||
cout << "\n"; | ||
|
||
return 0; | ||
} | ||
|
||
/* | ||
Example I/O - | ||
Enter size of array = 10 | ||
Enter elements of array = 9 10 2 4 8 1 90 22 11 3 6 | ||
Sorted array (using bubble sort) = 1 2 3 4 8 9 10 11 22 90 | ||
*/ |