# Sorting the numbers

• 09-29-2010
minyoungan
Sorting the numbers
Hello!! Its been two weeks since I started to study programming. I need to program that asks the user to input three numbers, and then outputs the numbers from the largest to the smallest. For example, if the input is 23 5 15, the output is 23 15 5.

Should I use bubble sort or qsort? I dont know where to begin. I need some friendly advices.
• 09-29-2010
Qsort is overkill for sorting a very small number of items. Not the fastest sorter either. :p

Here's an easy sorter (not quite bubble sort, but like it, and just a tad faster). To use it, you should have an array of int's with your numbers in a[0], a[1], and a[2], and #define SIZE 3
just below the include file listing in your program

Code:

```for(i=0; i<SIZE-1;i++) {   for(j=i+1;j<SIZE; j++) {     if(a[i] > a[j]) {       temp = a[i];       a[i] = a[j];       a[j] = temp;     }   } }```
• 09-29-2010
minyoungan
Whats the error in the code below?

Code:

``` #include <stdio.h> #include <stdlib.h> #define MAX 3 int intcmp(const void *v1, const void *v2);   main() {     int i, arr[MAX];           //Asks the user to input three interger numbers         printf("Enter %d integer values; press Enter after each.\n", MAX);         for (i = 0; i < MAX; i++)         scanf("%d", &arr[i]);                 //Sort the array into ascending order.     qsort(arr, MAX, sizeof(arr[1]), intcmp);         //Display the sorted array.     for (i = 0; i < MAX; i++)         printf("\n%d", i, arr[i]);         } int intcmp(const void *v1, const void *v2) {         int cmpvalue1, cmpvalue2;         cmpvalue1 = *(int*)v1;         cmpvalue2 = *(int*)v2;         return cmpvalue1 - cmpvalue2;         }```
• 09-29-2010
laserlight
Quote:

Originally Posted by minyoungan
Whats the error in the code below?

Instead of just dumping code and asking what is the error, tell us why you think there is an error in the code, e.g., what is the actual output versus what is the expected output. Furthermore, remember to compile with warnings turned on, e.g., my compiler informed me that this line is problematic, and it is:
Code:

`printf("\n%d", i, arr[i]);`
Quote: