-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubbleSort.c
More file actions
50 lines (40 loc) · 760 Bytes
/
bubbleSort.c
File metadata and controls
50 lines (40 loc) · 760 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
44
45
46
47
48
49
50
/*
* Bubble Sort Algorithm in Language C
*
* Author: Elton Fonseca
*/
#include <stdio.h>
#define MAX_SIZE 10
int swap(int *array, int position);
void show(int *array);
void sort(int *array);
int main(void)
{
int array[MAX_SIZE] = {5, 8, 9, 4, 7, 1, 6, 10, 3, 2};
show(array);
sort(array);
show(array);
}
void show(int *array)
{
for(int i = 0; i < MAX_SIZE; i++)
printf("%d ", array[i]);
printf("\n");
}
int swap(int *array, int position)
{
int current = array[position];
array[position] = array[position + 1];
array[position + 1] = current;
}
void sort(int *array)
{
for(int i = 0; i < MAX_SIZE; i++)
{
for(int j = 0; j < MAX_SIZE - i - 1; j++)
{
if(array[j] > array[j + 1])
swap(array, j);
}
}
}