summaryrefslogtreecommitdiff
path: root/algorithms/bubble_sort.cc
blob: ccf7b3548e8a536a54efe6395b7dbd134c3cef5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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;
}