sorting arrays
The language you use probably already has functions for sorting arrays that is both efficient and easy to use.
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.
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.
[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.
[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.
[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
And while we are at it here are some more search algorithms:
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.
- 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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement