homework

I have homework but i dont now how do it please help me

The task is to sort a given sequence of numbers in a non-decreasing order.
Entrance
The first one at the input is the length of the sequence 1 ≤ N ≤ 10,000. Next, the string in the form of N numbers between 0 and 2,000,000 is given.
Entrance
At the output, write the given string in the non-decreasing order.
Hello Tosislawek,

When you look at the requirements you can deduce what is needed for the program.
The task is to sort a given sequence of numbers in a non-decreasing order.
This tells you that you will be entering some numbers and storing these numbers in something like a C style array, a C++ std::array, a std::vector or maybe a list.

This next part
The first one at the input is the length of the sequence 1 ≤ N ≤ 10,000. Next, the string in the form of N numbers between 0 and 2,000,000 is given.
says the program will start be getting input from the user. It also says that you will likely be using some type of an array to store these numbers in. The first number is the portion of the array that will be used, i.e., how much of the whole that will be used. The second part is actually entering the numbers into the array.

1 ≤ N ≤ 10,000 This part tells you that the array's size is 10,000. Where as the first number entered is the part of the 10,000 that is actually used.

What I would start with is to define the variables that you will need and work on the inputs. A simple print or display function could come next and refined later if needed. Once you have something to work with work on the sorting and if you setup the print function right it is easily called to print the sorted array.

Hope that helps,

Andy
Its a little big but 2M will fit...
you will have to convert strings to integers and beat it into working code (this is c++ish pseudocode), but this is the core of it...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int * b = new int(2000001); //make space for every possible value
int dx;
for(dx = 0; dx <2000001; dx++) //initialize counts to zero
 b[dx] = 0;

for(all your values) //this is the first value in your list
   b[value]++;     //count every number as you see them.  so if you have 5 copys of 3, b[3] = 5

for(dx = 0; dx <2000001; dx++) //spit out the sorted list.
{
      while(b[dx])  //if a number exists, write it.  
   {
       cout << dx << endl;
       b[dx]--;
   }
}



Topic archived. No new replies allowed.