• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Diventurer

Sorting some numbers...kind of

15 posts in this topic

Hey

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

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...
[CODE]OutputNumbersFromLowestToHighest(Numbers);[/CODE]

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 [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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
1

Share this post


Link to post
Share on other sites
1. I love your name. YogurtEmperor.

2. If I understand correctly, you mean I should [u][b]basically[/b][/u] 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 [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
0

Share this post


Link to post
Share on other sites
[quote name='Segun24' timestamp='1329132415' post='4912549']
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.
[/quote]
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.
0

Share this post


Link to post
Share on other sites
[quote name='Brother Bob' timestamp='1329137132' post='4912564']
[quote name='Segun24' timestamp='1329132415' post='4912549']
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.
[/quote]
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.
[/quote]

He wants for instance:

[1, 6, 2, 4]

to be 'outputted' like:

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

Share this post


Link to post
Share on other sites
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...
0

Share this post


Link to post
Share on other sites
[quote name='Segun24' timestamp='1329133736' post='4912553']

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

[/quote]

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.
1

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
[quote]
What about something like this...
[/quote]
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(n[size=3][sup]2[/sup][/size]). 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.
0

Share this post


Link to post
Share on other sites
Sort an array of indices:

[code]#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';
}
[/code]
1

Share this post


Link to post
Share on other sites
Thank you for the reply. But I've already solved it [img]http://public.gamedev.net//public/style_emoticons/default/happy.png[/img]

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

Share this post


Link to post
Share on other sites
[quote name='rip-off' timestamp='1329141751' post='4912598']
[quote]
What about something like this...
[/quote]
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(n[size=3][sup]2[/sup][/size]). 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.
[/quote]

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 :-)
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0