Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Bubble Sort : C++ #18

Merged
merged 2 commits into from
Mar 15, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions Sorting/bubblesort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/* Bubble sort is a sorting algorithm that swaps adjacent values if one is greater than another
as it iterates through an array or list.

The algorithm operates as follows:
1. Begin iteration of an array/list
2. Compare first element and second element
3. If first element is greater than second element, swap elements.
4. Iterate to next element and repeat until iteration is complete
5. Loop 1-4 until array is sorted

This algorithm will use the standard library function std::swap; however, a custom swap function can be created if necessary.
*/

#include <iostream>

/*
Uses bubble sort to sort a passed array of integers with a given size

Parameters:
arr: c array[int], array that will be sorted
arr_size: int, size of the array
*/
void bubble_sort(int arr[], int arr_size){
for (int i = (arr_size - 1); i > 0; --i){
for (int j = 0; j < i; ++j){
if (arr[j] > arr[j+1]){
std::swap(arr[j], arr[j+1]);
}
}
}
}


// Main to demonstrate the sorting algorithm's functionality
int main()
{
const int SIZE = 10;
int arr[SIZE] = {7, 1, 15, 4, 2, 0, 19, -5, 6, 2};

bubble_sort(arr, SIZE);

// Range-based for loop
for (auto e : arr)
std::cout << e << " ";

std::cout << std::endl;

return 0;
}