forked from Synergise-IIT-Bhubaneswar/Beta-Algo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Description addition of the bubble sort algorithm in the c++ repository ## Have you read the <a href = "https://github.com/Rj-coder-iitian/Beta-Algo/blob/main/CONTRIBUTING.md">Contributing Guidelines on Pull Requests? </a> Yes ## Please answer the following questions for yourself before submitting an issue. - [ ] I've read the contribution guidelines. - [ ] I've checked the issue list before deciding what to submit. Related Issues or Pull Requests link to the <a href = "Synergise-IIT-Bhubaneswar#3"> issue </a>
- Loading branch information
Showing
1 changed file
with
71 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,71 @@ | ||
// 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 | ||
int temp = (*a)[j - 1]; | ||
(*a)[j - 1] = (*a)[j]; | ||
(*a)[j] = temp; | ||
|
||
} | ||
} | ||
|
||
} | ||
|
||
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; | ||
} |