Jump to content
  • Advertisement
Sign in to follow this  
phil67rpg

sorting arrays

This topic is 2505 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
The language you use probably already has functions for sorting arrays that is both efficient and easy to use.

Share this post


Link to post
Share on other sites
You could use other datastructures in the c++ standard library like vectors, which gives you dynamic 'arrays' and functions like sorting.

But if you really want to use arrays you can look into bubblesort. It's not quite an efficient sorting algorithm, but it is quite easy to implement.

Share this post


Link to post
Share on other sites
[font=courier new,courier,monospace]const static int size = 42;[/font]
[font=courier new,courier,monospace]int array[size];[/font]
[font=courier new,courier,monospace]std::sort( array, array+size );[/font]

[edit] An array is just a pointer, and a pointer satisfies the requirements of the "random access iterator" concept, so they can be used with many of the [font=courier new,courier,monospace]std[/font] algorithms.

Share this post


Link to post
Share on other sites

[font=courier new,courier,monospace]const static int size = 42;[/font]
[font=courier new,courier,monospace]int array[size];[/font]
[font=courier new,courier,monospace]std::sort( array, array+size );[/font]

[edit] An array is just a pointer, and a pointer satisfies the requirements of the "random access iterator" concept, so they can be used with many of the [font=courier new,courier,monospace]std[/font] algorithms.

Might as well link to iterators and random access iterator while you're at it :P

Share this post


Link to post
Share on other sites
And while we are at it here are some more search algorithms:

  • Insertion
  • Merge
  • Quick
  • Heap

    There are more sort methods however and there is a comparison of them here. The selection of your sorts depends on what you want to do, but as stated before std::sort will usually do the job.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!