Skip to main content
GameDev.net gamedev.net
🔒 Locked

sorting arrays

Started by phil67rpg Feb 9, 2012 at 4:54 AM 6 replies 1.4k views
Original Post
phil67rpg
phil67rpg
what is the most simple sorting method using arrays.
Wooh
Wooh
The language you use probably already has functions for sorting arrays that is both efficient and easy to use.
phil67rpg
phil67rpg
[sup]I am using c++.[/sup]
M-E
M-E
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.
"Anyone who has never made a mistake has never tried anything new." - Albert Einstein
Hodgman
Hodgman
[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.
M-E
M-E
I didn't know you could just do that. Another thing learned.
"Anyone who has never made a mistake has never tried anything new." - Albert Einstein
Washu
Washu

[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
In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
NightCreature83
NightCreature83
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.