template<class T>
void exch( T& x, T& y )
{
T tmp=x;
x=y;
y=tmp;
}
template<class T>
void bubble_sort( T A[], size_t size )
{
size_t last_exch, left, right;
while( size )
{
for( left=0, right=1, last_exch=left; right<size; ++left, ++right)
if( A[right]<A[left] )
exch( A[left], A[last_exch=right] );
size = last_exch;
}
}
Bubble Sort:
Bubble Sort works by repeatedly stepping through the array to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order.
A C-language implementation for bubble sort is as follows:
void bubblesort (int a[], int size)
{
int j, temp;
while (size != 1) {
for(j = 1; j < size; j++)
if ( a[j-1] > a[j] )
{
temp = a[j-1];
a[j-1] = a[j];
a[j] = temp;
}
size--;
}
}
This function sorts an array of integers in ascending order. It can be called from main() function as: bubblesort (array, arraylength);
Note that Bubble Sort is one of the least efficient sorting algorithms, even when optimised. Due to its quadratic nature it is not at all suitable for sorting large sets, but even for small sets an Insert Sort will generally give better performance. Bubble Sort is typically cited as an example of how not to write an algorithm.
The following code demonstrates the optimal Bubble sort algorithm.
#include
#include
#include
template
void bubblesort (std::vector
if (v.size() < 2) return; // sets of less than 2 do not need sorted
Compare cmp; // construct a comparison predicate
auto last = v.begin() + (v.size() - 1); // refer to last element of the unsorted set
while (v.begin() != last) { // repeat until unsorted set is empty
auto adj1 = v.begin(); // adjacent objects (1 and 2)
auto adj2 = adj1 + 1;
auto temp = v.begin(); // keeps track of where the last swap occurred
while (adj1 != last) { // repeat for all elements of the current unsorted set
if (!cmp (*adj1, *adj2)) { // compare the adjacent objects
std::swap (*adj1, *adj2); // objects are out of sequence, swap them
temp = adj1; // the last swap occurred here
}
++adj1; // advance adjacent objects by 1
++adj2;
}
last = temp; // the unsorted set ends at the last swap
}
}
template
std::ostream& operator<< (std::ostream& os, const std::vector
os << "{";
for (auto val : vct) os << val << ", ";
os << "\b\b}"; // backspace over trailing comma
return os;
}
int main () {
std::vector
std::cout << "Unsorted: " << data << std::endl;
bubblesort
std::cout << " Ascend: " << data << std::endl;
bubblesort
std::cout << " Descend: " << data << std::endl;
}
The standard library sort algorithm automatically uses MSD radix to sort strings: std::vector<std::string> vs = {"a", "b", "c" "d", "ab"}; std::sort(vs.begin(), vs.end()); After sorting, the order will be: {"a", "ab", "b", "c", "d"}
how to write a program that counts automorphic number from 1 to 999
There's only one type of sort in C++; std::sort. If you want other types you'll need to write your own.
Don't write, it is already written, google for 'cpp'.
Divide it by 1000.
The standard library sort algorithm automatically uses MSD radix to sort strings: std::vector<std::string> vs = {"a", "b", "c" "d", "ab"}; std::sort(vs.begin(), vs.end()); After sorting, the order will be: {"a", "ab", "b", "c", "d"}
how to write a program that counts automorphic number from 1 to 999
By learning how to program on C+.
There's only one type of sort in C++; std::sort. If you want other types you'll need to write your own.
Don't write, it is already written, google for 'cpp'.
Divide it by 1000.
No.
C++ is useful when you want to write a program which you can use for your home use. For instance, you have a lot of different files. You can use C++ to sort them out using file extensions. Or you can write a program which will keep track of programs and music you download and so on.
You don't write an algorithm for a C++ program, unless you are documenting the C++ program after-the-fact. The normal procedure is to write the algorithm first, in a language independent fashion, and then translate that stated algorithm into C++ code, or into whatever language you wish.
there is no solution of this problem...........that's it..........
Its limited only by available memory.
It depends on the particular IDE. Visual Studio uses <Ctrl>F5 to start a program in non-debug mode, and F5 to start a program in debug mode.