Sorting Integers

This is a discussion on Sorting Integers within the C Programming forums, part of the General Programming Boards category; I need to know what is the command(&how it works) to sort numbers in order (e.g. 1, 2 ,3, 6, ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    14

    Post Sorting Integers

    I need to know what is the command(&how it works) to sort numbers in order (e.g. 1, 2 ,3, 6, 10 ,34 ,35, 63)
    Anything would be appreciated.

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    10

    Lightbulb

    Working in 'C'...why do u want one single command to sort....

    implement any of the sorting algo.

    linear sorting, quick sorting etc.

    Thats the best way to learn.
    "There is no alternative to consitency..."

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    14

    Question Sorting Integers

    I just need the code to sort numbers in order that's all. I am not an expert and how do I need which code is for which.. help anyone?

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    10
    Here is simple code for sorting

    Code:
    #include<stdio.h>
    
    int main()
    {
    
       int a[8] = {1,22,5,4,9,6,7};
       int temp =0,i=0 ,j=0;
       for (i=0; i<7; i++) {
         for( j=i; j<7; j++) {
           if( a[i] > a[j]) {
              temp = a[i];
              a[i] = a[j];
              a[j] = temp;
          }
        }
      }
    
       printf("\n sorted array is ");
       for ( i=0; i<7; i++)
         printf("\n a[%d] = %d\n ", i, a[i]);
    
       return 0;
    }
    N'joy
    "There is no alternative to consitency..."

  5. #5
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Or use the qsort function:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int intcmp(const void *v1, const void *v2) { return(*(int *)v1- *(int *)v2); }
    
    int main()
    {
       int i, a[10] = { 9, 8, 7, -5, 99, -1, 0, 1, -9, 34 };
    
       qsort((char *)a, sizeof(a)/sizeof(*a), sizeof(*a), intcmp);
    
       for(i=0; i<10; i++) printf(" %d",a[i]);
    
       return 0;
    }

  6. #6
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Look, you'll need an algorithim to sort an array, you can find a lot of algorithims, an easy one to understand is Bubble sort, isn't so fast, but it's easy:

    http://www-ee.eng.hawaii.edu/Courses...on2.1.2.2.html

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    10
    Before using qsort please check whether you have the libraray for that otherwise your program won't compile.

    On unix it usually come as Fortran Library routine.

    ( library named as libF77.a ).

    "There is no alternative to consitency..."

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by gotyatech
    Before using qsort please check whether you have the libraray for that otherwise your program won't compile.

    On unix it usually come as Fortran Library routine.

    ( library named as libF77.a ).

    qsort is an ANSI standard function, prototyped in stdlib.h
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  3. Sorting an array of integers
    By supermeew in forum C Programming
    Replies: 4
    Last Post: 05-02-2006, 05:58 AM
  4. Replies: 6
    Last Post: 08-04-2003, 11:57 AM
  5. Sorting integers
    By Munkey01 in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2003, 07:36 AM

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