Design a C program to sort a one dimensional array in ascending order.
#include <stdio.h>
#include <stdlib.h>
void print_array(int* data, size_t size)
{
size_t i;
for(i = 0; i < size; ++i)
{
printf("%d ", data[i]);
}
printf("\n");
}
int comp(const void* a, const void* b)
{
int a1 = *((const int*)a);
int b1 = *((const int*)b);
if(a1 < b1) return -1;
if(a1 > b1) return 1;
return 0;
}
int main()
{
int arr[] = {23, 56, 1, 19, 72, 54, 38, 83, 6, 74, 72};
size_t size = sizeof(arr) / sizeof(arr[0]);
printf("Source array: "); print_array(arr, size);
qsort(arr, size, sizeof(int), comp);
printf("Result array: "); print_array(arr, size);
return 0;
}
Comments
Leave a comment