Jump to content

  • Log In with Google      Sign In   
  • Create Account


Sorting some numbers...kind of


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
15 replies to this topic

#1 diventurer   Members   -  Reputation: 828

Like
0Likes
Like

Posted 13 February 2012 - 04:57 AM

Hey

std::list <unsigned int> Numbers;
for (unsigned int i = 0; i < 100; i++) {
	 Numbers.push_back(rand() % 9500);
}

Okay, so we have that piece of code. Right?

What I want to do now is to output all those numbers from lowest to highest.
Like, (31, 52, 60, 79) and not (85, 20, 852, 521)

So now you may be thinking: "Oh, that's just to swap the numbers and sort them like that."

But the thing is, I don't want to swap the numbers inside the list...
I want to simply just get the lowest number in the list like...
OutputNumbersFromLowestToHighest(Numbers);

And then it'd give me the answer. And again, I don't want anything in the Numbers list to change, I just want it read.


Thanks in advance Posted Image

Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 8012

Like
0Likes
Like

Posted 13 February 2012 - 05:04 AM

Pass the list to the function by value, or pass it by reference and make an internal copy of it, and then sort it. You get the values in order, and the list is unchanged.

#3 diventurer   Members   -  Reputation: 828

Like
0Likes
Like

Posted 13 February 2012 - 05:26 AM

Thanks for your reply, but I think you misunderstood.
I don't want the indexes to change at all. I want to simply output the numbers directly in order.
Because, when I use the sorted list, the indexes aren't the same... Get me?
I need each index to have their specific number.
I have a feeling recursion might be useful for this, but I can't come up with anything.

#4 L. Spiro   Crossbones+   -  Reputation: 13214

Like
1Likes
Like

Posted 13 February 2012 - 05:39 AM

Why not make a sorted copy of the list using a set (or a map if you want to print its index as well as the values in order)?


L. Spiro
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#5 diventurer   Members   -  Reputation: 828

Like
0Likes
Like

Posted 13 February 2012 - 05:48 AM

1. I love your name. YogurtEmperor.

2. If I understand correctly, you mean I should basically have a 2D list?
So I could get the index and number. I guess that makes sense.

Thanks. If anyone has anything else to add, do tell Posted Image

#6 Brother Bob   Moderators   -  Reputation: 8012

Like
0Likes
Like

Posted 13 February 2012 - 06:45 AM

Thanks for your reply, but I think you misunderstood.
I don't want the indexes to change at all. I want to simply output the numbers directly in order.
Because, when I use the sorted list, the indexes aren't the same... Get me?
I need each index to have their specific number.
I have a feeling recursion might be useful for this, but I can't come up with anything.

I still don't see how sorting a copy won't work. If all you want is to output the numbers in order, how can you not sort a copy of the list and output it? The values come out in order, and the list is unchanged. Those are the only two requirements I can find in your posts.

#7 luca-deltodesco   Members   -  Reputation: 637

Like
1Likes
Like

Posted 13 February 2012 - 06:47 AM


Thanks for your reply, but I think you misunderstood.
I don't want the indexes to change at all. I want to simply output the numbers directly in order.
Because, when I use the sorted list, the indexes aren't the same... Get me?
I need each index to have their specific number.
I have a feeling recursion might be useful for this, but I can't come up with anything.

I still don't see how sorting a copy won't work. If all you want is to output the numbers in order, how can you not sort a copy of the list and output it? The values come out in order, and the list is unchanged. Those are the only two requirements I can find in your posts.


He wants for instance:

[1, 6, 2, 4]

to be 'outputted' like:

[1 (index 0), 2 (index 2), 4 (index 3), 6 (index 1)]

#8 cignox1   Members   -  Reputation: 723

Like
0Likes
Like

Posted 13 February 2012 - 06:59 AM

What about something like this:

int getNext(int n)
{
int candidate = inf;
foreach(value in list)
{
if(value <= n) continue;
if(value < candidate)
candidate = value;

}

return candidate;
}

And then:

int val = inf;
for(int i = 0; i < list.length(); ++i)
{

val = gentNext(val);
if(val == inf)
break; //no more numbers in list

print val;
}

Could most probably use some more work, but should work...

#9 thok   Members   -  Reputation: 684

Like
1Likes
Like

Posted 13 February 2012 - 07:09 AM

2. If I understand correctly, you mean I should basically have a 2D list?
So I could get the index and number. I guess that makes sense.


No. 'copy list' != 'make 2D list'.

As two others already mentioned, the easiest way to solve this problem is to make a copy of the list, sort the copy (leaving the original unchanged), and print each item.

#10 diventurer   Members   -  Reputation: 828

Like
0Likes
Like

Posted 13 February 2012 - 07:11 AM

That's correct, luca-deltodesco.

Thanks thok! :)

#11 Brother Bob   Moderators   -  Reputation: 8012

Like
1Likes
Like

Posted 13 February 2012 - 07:18 AM

If you want to output the original indices as well, then that is a different problem from just outputting the values you talked about from the beginning.

Different, but not much so really. Store a copy of the list which contains pairs of original values and indices. Then you can sort the list by the value field, and output both the value and the index.

#12 diventurer   Members   -  Reputation: 828

Like
0Likes
Like

Posted 13 February 2012 - 07:20 AM

Yeah, I know. I asked the question in a kind of stupid way.

I think with the current amount of help, I'll be able to make it work :)

Thanks everyone.

#13 rip-off   Moderators   -  Reputation: 8111

Like
0Likes
Like

Posted 13 February 2012 - 08:02 AM

What about something like this...

You could get something like that to work for distinct values, but data sets with duplicate values are a bit trickier.

Even if you got such a technique to work, it would be O(n2). Sorting the list can be done in O(n log n), output can be in O(n), which would mean that the sort then display algorithm would be O(n log n) overall. Of course, if we were talking about the performance we could also talk about the allocation overhead and cache pressure of using a linked list over a contiguous data sequence like a dynamic array.

#14 Álvaro   Crossbones+   -  Reputation: 12895

Like
1Likes
Like

Posted 13 February 2012 - 08:52 AM

Sort an array of indices:

#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdlib>

class indirect_compare {
  std::vector<int> const &v;

public:
  indirect_compare(std::vector<int> const &v) : v(v) {
  }

  bool operator()(int a, int b) {
	return v[a] < v[b];
  }
};

int main() {
  std::vector<int> values;
  std::vector<int> indices;

  for (int i=0; i<4; ++i) {
	values.push_back(std::rand());
	indices.push_back(i);
  }

  std::sort(indices.begin(), indices.end(), indirect_compare(values));

  std::cout << "Original list:\n";
  for (int i=0; i<4; ++i)
	std::cout << values[i] << '\n';
  std::cout << '\n';

  std::cout << "After sorting:\n";
  for (int i=0; i<4; ++i)
	std::cout << values[indices[i]] << " (index " << indices[i] << ")\n";
  std::cout << '\n';
}


#15 diventurer   Members   -  Reputation: 828

Like
0Likes
Like

Posted 13 February 2012 - 08:57 AM

Thank you for the reply. But I've already solved it Posted Image

Edit: Forgot that it might be pointed towards other people.

#16 cignox1   Members   -  Reputation: 723

Like
0Likes
Like

Posted 14 February 2012 - 01:28 AM


What about something like this...

You could get something like that to work for distinct values, but data sets with duplicate values are a bit trickier.

Even if you got such a technique to work, it would be O(n2). Sorting the list can be done in O(n log n), output can be in O(n), which would mean that the sort then display algorithm would be O(n log n) overall. Of course, if we were talking about the performance we could also talk about the allocation overhead and cache pressure of using a linked list over a contiguous data sequence like a dynamic array.


Yes, of course it was not a serious alternative to sorting a list, but an option as the op seemed to dislike any sorting.
Good point with the duplicates, though, I have not thought to that :-)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS