diff options
Diffstat (limited to 'algorithms/bubble_sort.cc')
-rw-r--r-- | algorithms/bubble_sort.cc | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/algorithms/bubble_sort.cc b/algorithms/bubble_sort.cc new file mode 100644 index 0000000..ccf7b35 --- /dev/null +++ b/algorithms/bubble_sort.cc @@ -0,0 +1,45 @@ +#include <iostream> + +#define N 4 +#define TRUE 1 +#define FALSE 0 + + +void +bubbleSort(int *A, int n) +{ + int i, t, change, pos, newpos; + pos = n - 1; + newpos = 0; + do { + change = FALSE; + for (i=0; i<pos; i++) { + if (A[i]>A[i+1]) { + t = A[i+1]; + A[i+1] = A[i]; + A[i] = t; + newpos = i - 1; + change = TRUE; + } + } + pos = newpos; + } while (change); +} + + +int main (int argc, char * const argv[]) { + int A[N] = {3,1,1,2}; + + for(unsigned int i=0; i<N; i++) { + std::cout << A[i]; + } + std::cout << std::endl << "sortiert:" << std::endl; + bubbleSort(A, N); + for(unsigned int i=0; i<N; i++) { + std::cout << A[i]; + } + std::cout << std::endl; + + return 0; +} + |