August 12th, 2003, 03:03 AM
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 = 3,4,5,1,2;
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
August 12th, 2003, 04:28 AM
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.
August 12th, 2003, 02:05 PM
Why not use the built in qsort function? All you have to do is pass it a function pointer to your comparison function.
August 12th, 2003, 03:19 PM
Because it doesn't need to be sorted. As 7stud showed, it can be done with a single pass over the array.
August 14th, 2003, 03:12 AM
wow, the solution was great! thanx a lot.