Computer Science Canada Sorting numbers as they are entered? |
Author: | Krocker [ Sun Jan 12, 2014 3:59 pm ] | ||
Post subject: | Sorting numbers as they are entered? | ||
hi, so i have to create a program which creates and array with a user specified size and intially fill it with 0. Then ask the users to enter a number to fill each element. However, each time the uer inputs a number, the number must be put into the array in order. I kinda got it working. If i enter a number that is less then the numbers already entered, it works only if there is one other number in the array, else it replaces the other arrays that are greater then it. ex. Array currently: 5 7 0 0 0 input: 4 Array Now: 4 5 0 0 0
|
Author: | Insectoid [ Sun Jan 12, 2014 4:29 pm ] | ||
Post subject: | RE:Sorting numbers as they are entered? | ||
When you insert a new entry, your code only shifts the entry that the new entry is replacing. You need an iterative or recursive solution to shift all following elements. For example, in pseudocode:
Alternatively, you can use a linked list, which allows very easy inserts mid-list. |
Author: | Krocker [ Sun Jan 12, 2014 9:08 pm ] | ||
Post subject: | Re: Sorting numbers as they are entered? | ||
ok.. what im not sure about is the position p, what is that? So this is what i came u with according to what u said, not sure if its right though cuz its still not working:
and this is whats being fed into the method: array = sorting(array,val,0) Help anyone? im totally lost for this one. |