-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add bubble sort algorithm. Add .gitignore
- Loading branch information
Showing
2 changed files
with
81 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,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 | ||
|
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,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; | ||
} |