So does that mean that you aren't interested in putting in any more effort on your own? Instead, you are just going to wait for someone else to do all the work and give you the solution?I haven't given up yet I am watching this thread very closely I am interested to see how do you solve the problem first in your head and then how do you make flow chart for that.
I've already described a linear-time algorithm to solve the problem. It'll be interesting to see how much similarity there is between yours and mine.The solution which I will present does not require sorting or comparisons.
It is simple and fast. I cannot think of any other solution that would be faster.
Can anyone guess what is the solution?
I don't need to guess, post #31 is faster. The gauntlet is down.The solution which I will present does not require sorting or comparisons.
It is simple and fast. I cannot think of any other solution that would be faster.
Can anyone guess what is the solution?
Perhaps you could walk through a toy example for that algorithm to illustrate how it works.I don't need to guess, post #31 is faster. The gauntlet is down.
My algorithm worked in simulation with MPLab 8.92. If there is a need to explain it, I will do that later. Basically the act of entering a number creates a marker that can be efficiently (btfsc/s) tested by subsequent numbers for duplication.Perhaps you could walk through a toy example for that algorithm to illustrate how it works.
For instance, let's say I have an array with four values: {2, 19, 4, 19}
Your algorithm says that I need four registers and then I use the values in the array as indices into the array, right. So what do I do when the value is 19?
I just "store" the index. It's quicker for later access. How many instructions does that code decompile to?If memory/storage capacity isnt a problem and the range of number is finite, then the fastest and easiest is to store each number representing its own index: For example
Numbers = [2, 6, 199, 3201, 4.....]
data = []
for (i = 0; i < sizeof(Numbers) - 1; i++)
x = Numbers;
if x > sizeof(data);
resize(data, x);
data[x] = data[x]++;
Of course the array have to be resize each time x is greater then its length.
That's it? I call that sorting since you are taking balls that are not in particular order and putting them into sequentially numbered bags.@WBahn has described the same solution in post #84.
I thought about this when we described the problem as balls in a bag.
Take balls out of the bag one at a time.
Put each ball in a bag and write down on the outside of the bag the number marked on the ball.
This process is the same as the hash process described in a previous post.
How many bags would we need?
If there are 100 balls, then we will need a maximum of 100 bags.
This is how the hash function works. For example, the hash function when given a value "12345", for example, will return a numerical hash index which is going to be the number of our bag. So the solution is simply that the hash index is the same as the number on the ball. In other words, the hash function hash(x) = x.
Our bags will be numbered sequentially from 0 to the max number.
Suppose that we are restricted to numbers from 0-65535. Then we will need 65536 bags. Period. End of story.
Suppose we are told that the numbers are in the range 1 to 100. Then we need 100 bags, regardless of the total number of balls. You take one ball at a time and put it into its proper bag marked with the same number on the outside of the bag. Given boundaries on the input we can place restrictions on the model.
The point here is, you take a problem that may appear abstract mathematically and find a real world working solution to the problem, such as putting numbered balls into numbered bags.
Here is the engineering problem solving methodology.
1. Define the problem. List the specifications, limitations and restrictions.
2. Create a working solution after researching all possibilities.
3. Draw a flowchart.
4. Implement the solution.
5. Test the solution
What is the function count() returning?If you still cannot do this on your own, here is a code snippet of the solution:
for (i = 0; i < n; i++ ) ++count( x(i) );
I feel I am trying to solve a more difficult question than my ability. I made many efforts to solve it but it is not so easy as you say, first solve the problem inside the brainBut all of this is WAY beyond where the TS is at.
https://en.wikipedia.org/wiki/Sorting_algorithmIn computer science, a sorting algorithm is an algorithm that puts elements of a list in a certain order.
True, putting numbered balls into its respective containers would qualify as sorting in the human perspective.We seem to have distinctly different understandings of what "sort" means:
Which is why I am trying to walk you incrementally to a workable solution.I feel I am trying to solve a more difficult question than my ability. I made many efforts to solve it but it is not so easy as you say, first solve the problem inside the brain
#include <stdio.h>
int main(void)
{
int data[11] = {5, 3, 7, 1, 6, 3, 9, 12 , 21, 13, 16};
sizeOfData = 11
for ( int i = 0; i < sizeOfData; i++)
printf( "data = %d \n", data[i]);
return 0;
}