Skip to content

Commit

Permalink
Add bubble sort algorithm. Add .gitignore
Browse files Browse the repository at this point in the history
  • Loading branch information
mvoitko committed Oct 2, 2020
1 parent 65c2716 commit 7ddfb65
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
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;
}

0 comments on commit 7ddfb65

Please sign in to comment.