void bubble_sort(long [], long);
int main()
{
long array[100], n, c;
printf("Enter number of elements\n");
scanf("%ld", &n);
printf("Enter %ld integers\n", n);
for (c = 0; c < n; c++)
scanf("%ld", &array[c]);
bubble_sort(array, n);
printf("Sorted list in ascending order:\n");
for (c = 0; c < n; c++)
printf("%ld\n", array[c]);
return 0;
}
void bubble_sort(long list[], long n)
{
long c, d, t;
for (c = 0 ; c < n - 1; c++) {
for (d = 0 ; d < n - c - 1; d++) {
if (list[d] > list[d+1]) {
/* Swapping */
t = list[d];
list[d] = list[d+1];
list[d+1] = t;
}
}
}
}
The output of this C program will depend on the input provided by the user. Here's an example of how the program would execute with sample input:
mathematicaEnter number of elements
6
Enter 6 integers
45
12
78
32
61
19
Sorted list in ascending order:
12
19
32
45
61
78
2) insertion sort
/* Insertion sort ascending order */#include <stdio.h>
int main()
{
int n, array[1000], c, d, t, flag = 0;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 1 ; c <= n - 1; c++) {
t = array[c];
for (d = c - 1 ; d >= 0; d--) {
if (array[d] > t) {
array[d+1] = array[d];
flag = 1;
}
else
break;
}
if (flag)
array[d+1] = t;
}
printf("Sorted list in ascending order:\n");
for (c = 0; c <= n - 1; c++) {
printf("%d\n", array[c]);
}
return 0;
}
The output of this C program will depend on the input provided by the user. Here's an example of how the program would execute with sample input:
mathematicaEnter number of elements
6
Enter 6 integers
45
12
78
32
61
19
Sorted list in ascending order:
12
19
32
45
61
78
3) merge sort program in c
Here's an example of what the output might look like:
yamlEnter number of elements: 5
Enter array elements: 12 5 7 18 3
Sorted array is: 3 5 7 12 18
4) Quick sort program in c Here's an example of what the output might look like:
lessEnter the number of elements in the array:
5 Enter the elements of the array:arr[0]: 12
arr[1]: 5
arr[2]: 7
arr[3]: 18
arr[4]: 3 The sorted array is:
3 5 7 12 18
No comments:
Post a Comment