Question:

C programming: arrays in ascending order?

by  |  earlier

0 LIKES UnLike

say I have the array [5,7,1,9,2], I want it to be in ascending order so obviously the array would then be [1, 2, 5, 7, 9]. how can I do this using the bubblesort algorithm?

 Tags:

   Report

2 ANSWERS


  1. hm. simply implementing the bubble algo... surely an already done code is out there, you have just to pass your array in.

    like

    bubblesort(myarray,NUM);

    if myarray is

    int myarray[NUM]; or something like this


  2. Just use one of the stdlib sort routines.  Here's how to do it with qsort.

    #include <stdlib.h>

    int myArray[5] = {5,7,1,9,2};

    int ascendCompare(const void *a, const void *b) {

       int a_val = *((int *)a);

       int b_val = *((int *)b);

       return a_val - b_val;

    }

    int main(int argc, char*argv[]) {

       qsort(myArray, 5, sizeof(int), ascendCompare);

    }

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.