#1
  1. <!--REMOTE_ADDR-->
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Location
    Cali4nia
    Posts
    162
    Rep Power
    12

    Question Quick Question about sorting an array?


    I want to sort an array with bubblesort or any other way, but i dont want the array to be changed instead the function should takes an array as a parrameter and then return the index of the smallest number in it.

    for example -

    int i[5] = 3,4,5,1,2;
    int sortfunction(i[])

    returns the value 3 as 1 is the smallest number in the array and has an index of 3.

    I think this question is pretty simple, but i cant think anything right now as I have had my mind blocked. Had been coding since last 5 hours. I need this function to insert in a program.

    Thanx For Help
    ~Gaurav
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    It is very simple. Assign the first element of the array to an int variable, assign the index position 0 to another int variable Then look at the next element in the array. If it's smaller assign the the number and the index value to their respective variables. When you get to the end of the array you'll have the value and its index position. Return the index position.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    109
    Rep Power
    12
    Why not use the built in qsort function? All you have to do is pass it a function pointer to your comparison function.
  6. #4
  7. Doggie
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2003
    Location
    Seattle, WA
    Posts
    751
    Rep Power
    13
    Originally posted by Nem
    Why not use the built in qsort function?
    Because it doesn't need to be sorted. As 7stud showed, it can be done with a single pass over the array.
  8. #5
  9. <!--REMOTE_ADDR-->
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Location
    Cali4nia
    Posts
    162
    Rep Power
    12
    wow, the solution was great! thanx a lot.
    - Gaurav

IMN logo majestic logo threadwatch logo seochat tools logo