-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathbubble_sort.c
52 lines (45 loc) · 1010 Bytes
/
bubble_sort.c
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
51
52
#include <stdio.h>
#include <stdlib.h>
void bubble_sort(int *, int);
void print(int *, int);
void bubble_sort(int *input_numbers, int size)
{
int index_1, index_2;
for(index_1 = 0; index_1 < size-1; index_1++)
{
for(index_2 = 0; index_2 < size-1; index_2++)
{
if(input_numbers[index_2] > input_numbers[index_2+1])
{
(input_numbers[index_2] ^= input_numbers[index_2+1], \
input_numbers[index_2+1] ^= input_numbers[index_2], \
input_numbers[index_2] ^= input_numbers[index_2+1]);
}
}
print(input_numbers, size);
}
}
void print(int *array, int size)
{
int i;
for(i = 0; i < size; i++)
printf("%d ", array[i]);
printf("\n");
}
int main()
{
int *input_numbers, i = 0;
int size;
scanf("%u", &size);
if(size < 1)
{
printf("\nEnter a valid size\n");
exit(-1);
}
input_numbers = (int *)malloc(sizeof(int) * size);
while(i != size)
scanf("%d", &input_numbers[i++]);
bubble_sort(input_numbers, size);
printf("\nSorted Array\n");
print(input_numbers, size);
}