summaryrefslogtreecommitdiff
path: root/algorithms/bubble_sort.cc
diff options
context:
space:
mode:
authorPatrick Simianer <p@simianer.de>2014-06-15 03:24:33 +0200
committerPatrick Simianer <p@simianer.de>2014-06-15 03:24:33 +0200
commitcf3a29feb5887344b6633ead1b4b6d5657a15a4b (patch)
treef1149508f7305a48dba0226699dfafdd68d81969 /algorithms/bubble_sort.cc
parent5ddc763ab9953eebdaf78af4eb72288d7955b310 (diff)
old stuff: algorithms
Diffstat (limited to 'algorithms/bubble_sort.cc')
-rw-r--r--algorithms/bubble_sort.cc45
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;
+}
+