Sorting the numbers

This is a discussion on Sorting the numbers within the C Programming forums, part of the General Programming Boards category; Hello!! Its been two weeks since I started to study programming. I need to program that asks the user to ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    8

    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.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Qsort is overkill for sorting a very small number of items. Not the fastest sorter either.

    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;
        }
      }
    }

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    8
    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;
    	
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,163
    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 Originally Posted by Adak
    What is the return when cmpvalue2 is greater than cmpvalue1? Will qsort() handle that ok?
    Yes, though there are potential problems due to overflow.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comparing numbers to a list of numbers held in a text file
    By jmajeremy in forum C++ Programming
    Replies: 3
    Last Post: 11-06-2006, 07:56 AM
  2. Sorting numbers in a file
    By pxleyes in forum C Programming
    Replies: 19
    Last Post: 04-15-2004, 07:17 AM
  3. Sorting numbers in descending order
    By Yunasnk in forum C++ Programming
    Replies: 2
    Last Post: 11-23-2003, 05:55 PM
  4. Line Numbers in VI and/or Visual C++ :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 02-10-2002, 10:54 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21