Sorting an array into increasing values by passing pointers as function arguments

This is a discussion on Sorting an array into increasing values by passing pointers as function arguments within the C Programming forums, part of the General Programming Boards category; I'd like to be able to add code to my program which sorts the elements of an array into increasing ...

  1. #1
    Unregistered
    Guest

    Sorting an array into increasing values by passing pointers as function arguments

    I'd like to be able to add code to my program which sorts the elements of an array into increasing value. Can this be done in the following way (is it called bubble sorting?)?

    for (i=0;i<4;i++)
    for (p=(i+1);p<4;p++)
    if pointer1[i]>pointer1[p]
    temp=pointer1[i];
    pointer1[i]=pointer1[p];
    pointer1[p]=temp;

    What I mean to ask is, can pointers be used in arrays? Or does that defeat the objective of using pointers?

    Thanks

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Can't think of what this would be called but I don't think it is a bubble sort that you've got there (maybe it is, I can't remember the particular behavior of all the sorts I had to study in school: bubble, selection, insertion, radix, merge, quick sort, etc...). Yes you can do this. Say you have an array of 4 integers to sort. With some modifications, your code could be put into a function like so:

    Code:
    void MySort( int* pointer)
    {
        int i, p, temp;
    
        for( i = 0; i < 3; i++ ) // Needs to be one less than size of array
            for( p = i+1; p < 4; p++ )
                if( pointer[i] > pointer[p] ) // Or "if( *(pointer+i) > *(pointer+p) )
                {
                    temp = pointer[i];          // Or "temp = *(pointer+i);"
                    pointer[i] = pointer[p];   // Or "*(pointer+i) = *(pointer+p);"
                    pointer[p] = temp;         // Or "*(pointer+p) = temp;"
                }
    
    }
    You can call it like so:

    Code:
    int Array[4] = { 18, 12, 14, 16 };
    MySort( Array ); // Array will now be 12, 14, 16, 18 after call
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    8

    Thumbs up Reply

    Just tried it and it works a treat!

    Thanks very much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing a string array to a function using pointers
    By asofaihp in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2009, 11:31 AM
  2. Replies: 9
    Last Post: 10-15-2008, 03:08 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM
  5. Array of function pointers?
    By The V. in forum C++ Programming
    Replies: 3
    Last Post: 10-16-2001, 08:37 PM

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