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

Add bubble sort algorithm. Add .gitignore #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 35 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Created by .ignore support plugin (hsz.mobi)
### C++ template
# Prerequisites
*.d

# Compiled Object files
*.slo
*.lo
*.o
*.obj

# Precompiled Headers
*.gch
*.pch

# Compiled Dynamic libraries
*.so
*.dylib
*.dll

# Fortran module files
*.mod
*.smod

# Compiled Static libraries
*.lai
*.la
*.a
*.lib

# Executables
*.exe
*.out
*.app

46 changes: 46 additions & 0 deletions sorting-algorithms/bubble_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}


void bubbleSort(int[] a)
/*
Bubble sort, sometimes referred to as sinking sort, is a simple sorting
algorithm that repeatedly steps through the list, compares adjacent elements,
and swaps them if they are in the wrong order. The pass through the list is
repeated until the list is sorted.
*/
{
int n = a.length;
for (int j = 0; j < n - 1; ++j){111
for (int i = 0; i < n - j - 1; ++i)
if (a[i] > a[i+1])
swap(a, i, i+1);
}
}


void printArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
std::cout << arr[i] << " ";
std::cout << std::endl;
}


int main()
{
// a main function that can be used to test our code

int arr[] = {1, 5, 6, 3, 2};

bubbleSort(arr);

std::cout << "Sorted array:" << std::endl;
printArray(arr, n);
return 0;
}