Click here to Skip to main content
15,901,001 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to create a function in my program that sorts
any number of array elements using for loops,
I need to use for loops and no sort algorithms


i need the function to sort the numbers, than for a median function to call on the sort function to return the sorted array so I can calculate the median

can anybody help me?
Posted

I suppose this is homework :-)

michael Boothby wrote:
I need to use for loops and no sort algorithms


You need a sort alogorithm, but must implement it yourself, I'd do some research as knowing the various alogrithms available is handy. Bubble sort is easiest but is slow for large lists.

Personally, for a homework question like this, I'd create a destination array the same size as your original, then iterate over the orignal and insert into your destination array keeping the destination array sorted. That way your source array stays in tact, this can be done very quickly in C/C++. Then get the middle value.
 
Share this answer
 
michael Boothby wrote:
I need to use for loops and no sort algorithms

You do need a sort algorithm (on the other hand, for loop is optional).
Start reading, as already suggested, about the bubble-sort algorithm, that is simple and intuitive, albeit not a performance champion.
:)
 
Share this answer
 
It seems that you are looking for someone that do your homeworks for you... Sort arrays of numbers using for loops is lergely described on every book about programming basics, it's called bubble sort
 
Share this answer
 
The other option is Quick Sort. Its faster but a little more complicated to program.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900