-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path00_bubbleSort.c
More file actions
43 lines (36 loc) · 944 Bytes
/
00_bubbleSort.c
File metadata and controls
43 lines (36 loc) · 944 Bytes
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
#include <stdio.h>
#define N 10
int arr[] = {5,2,6,0,11,7,6,7,4,2};
//int arr[] = {0,2,2,4,5,6,6,7,7,11};
void bubbleSort(int *arr, int size)
{
int tmp, j;
int tausch=1;//da in C fehlt boolean typ, nutzen wir hier int(1=true,0=false)
while(tausch){
tausch=0;
for(j = 0; j < size - 1; j++)
{
if (arr[j + 1] < arr[j])
{
tmp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = tmp;
tausch=1;
}
}
size--;//die Länge von Array reduziert sich, da ein Element schon seine richtige stelle hat
}
}
int main()
{
int i;
printf("\nUnsorted array is: ");
for(i = 0; i < N; i++)
printf("%d ", arr[i]);
bubbleSort(arr,N);
printf("\nSorted array is: ");
for(i = 0; i < N; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}