Jump to content

Sorting algorithms/Bubble sort: Difference between revisions

Line 377:
 
=={{header|C++}}==
<lang cpp>#include <algorithm>
{{works with|g++|4.0.2}}
<lang cpp>#include <iostream>
#include <algorithmiterator>
 
// Sorts anything that provides random access iterators.
template< typename ARRAY_TYPE, typename INDEX_TYPE >
template <typename RAIterator> void bubble_sort(ARRAY_TYPERAIterator array[]begin, INDEX_TYPE size)
RAIterator end) {
{
using std::swap;
bool done = false;
while (begin != end--) {
whilefor (auto i = begin; i != doneend; ++i) {
if (*(i + 1) < *i) {
{
doneswap(*i, =*(i true+ 1));
{ }
for (INDEX_TYPE i = 0 ; i < size-1 ; i++)
{
if (array[i] > array[i+1])
{
done = false;
std::swap(array[i], array[i+1]);
}
}
size--;
}
}
}
 
int main() {
template< typename TYPE >
int dataa[] = { 10100, 92, 856, 7200, 6-52, 53, 499, 333, 2177, 1 -199};
void print(TYPE val)
bubble_sort(arraystd::begin(a), 10std::end(a));
{
copy(std::begin(a), std::end(a), std::ostream_iterator<int>(std::cout, " "));
std::cout << val << " \n";
}
 
int main()
{
int array[] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
bubble_sort(array, 10);
std::for_each (&array[0], &array[10], print<int>);
std::cout << std::endl;
//But in real life...
int data[] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
std::sort(data, data+10);
std::for_each (data, data+10, print<int>);
std::cout << std::endl;
}</lang>
Output:
<pre>
-199 -52 2 3 33 56 99 100 177 200
</pre>
 
=={{header|C sharp|C#}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.