-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path37.c
More file actions
43 lines (43 loc) · 913 Bytes
/
37.c
File metadata and controls
43 lines (43 loc) · 913 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>
int main()
{
int arr[100], n, i, j, temp;
// Input array elements until -1 is entered
n = 0;
printf("Enter array elements (enter -1 to stop):\n");
while(1)
{
int num;
scanf("%d", &num);
if (num == -1) break;
arr[n++] = num;
}
// Print original array
printf("Original array: ");
for(i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
// Bubble Sort
for(i = 0; i < n - 1; i++)
{
for(j = 0; j < n - i - 1; j++)
{
if(arr[j] > arr[j + 1])
{ // Swap if current element is greater
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
// Print sorted array
printf("Sorted array (ascending order): ");
for(i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}