-
Notifications
You must be signed in to change notification settings - Fork 0
/
BubbleSort.c
58 lines (50 loc) · 1.19 KB
/
BubbleSort.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
53
54
55
56
57
58
/*
BUBBLE SORT IN C
WITH DYNAMIC ARRAY
*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
// pointer for dynamic array
int* arr;
int noOfInput;
printf("Enter the no of elements to sort: ");
scanf("%d",&noOfInput);
// creating dynamic array
arr = (int*) malloc(sizeof(int)*noOfInput);
//taking inputs
printf("\nEnter the elements of the array: \n");
int i;
for ( i=0; i<noOfInput; i++ )
{
printf("\n Enter the %d element: ",i+1);
scanf("%d",&arr[i]);
}
// displaying the elements before sorting:
printf("\nElements before sorting is: ");
for(i=0; i<noOfInput; i++)
{
printf("%d ",arr[i]);
}
// now doing the bubble sort
int j,temp;
for(i=0; i<noOfInput; i++)
{
for(j=0; j<noOfInput-i-1; j++)
{
if(arr[j]>arr[j+1])
{
temp = arr[j+1];
arr[j+1] = arr[j];
arr[j] = temp;
}
}
}
// displaying the elements after sorting:
printf("\nElements after sorting is: ");
for(i=0; i<noOfInput; i++)
{
printf("%d ",arr[i]);
}
}