Answer to Question #198260 in C for Prasanna

Question #198260

Design a C program to sort a one dimensional array in ascending order.


1
Expert's answer
2021-05-27T06:25:09-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS