-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubble sort.c
More file actions
41 lines (38 loc) · 880 Bytes
/
bubble sort.c
File metadata and controls
41 lines (38 loc) · 880 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
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
int main()
{
int *array;
int n;
int tmp;
int i;
int noSwap;
printf("enter the number of elements: ");
scanf("%d", &n);
array = (int*)malloc(n * sizeof(int));
printf("enter array of elements:\n");
for(i = 0; i < n; i++)
scanf(" %d", &array[i]);
for(i = n - 1; i >= 0; i--)
{
noSwap = 1;
for(int j = 1; j < i; j++)
{
if(array[j] > array[j + 1])
{
tmp = array[j];
array[j] = array[j + 1];
array[j + 1] = tmp;
noSwap = 0;
}
}
if(noSwap == 1)
break;
}
printf("Sorted array:");
for(i = 0; i < n; i++)
printf(" %d", array[i]);
free(array);
return 0;
}