answersLogoWhite

0


Best Answer

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;

}

}

User Avatar

Wiki User

10y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

9y ago

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);

This answer is:
User Avatar

User Avatar

Wiki User

7y ago

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 // for std::greater

template>

void bubblesort (std::vector& v) {

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& vct){

os << "{";

for (auto val : vct) os << val << ", ";

os << "\b\b}"; // backspace over trailing comma

return os;

}

int main () {

std::vector data {5, 6, 4, 7, 3, 8, 2, 9, 1};

std::cout << "Unsorted: " << data << std::endl;

bubblesort (data); // default sort uses std::less

std::cout << " Ascend: " << data << std::endl;

bubblesort> (data);

std::cout << " Descend: " << data << std::endl;

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write a C plus plus program that uses Bubble Sort?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you write a c plus plus program to sort a vector of strings using MSD radix sort?

The standard library sort algorithm automatically uses MSD radix to sort strings: std::vector&lt;std::string&gt; vs = {"a", "b", "c" "d", "ab"}; std::sort(vs.begin(), vs.end()); After sorting, the order will be: {"a", "ab", "b", "c", "d"}


A program c plus plus on automorphic numbers or not?

how to write a program that counts automorphic number from 1 to 999


How do you write a C plus plus program that will display the first 10 positive prime numbers?

By learning how to program on C+.


Types of sort in c plus plus?

There's only one type of sort in C++; std::sort. If you want other types you'll need to write your own.


Write a program in c plus plus to implement macro processor?

Don't write, it is already written, google for 'cpp'.


How do you write program to convert meter to kilometer in c plus plus?

Divide it by 1000.


Do I need to write a program to find a substring in a given string in c plus plus?

No.


What are the home application of c plus plus programming language?

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.


How do you write an Algorithm for a C plus plus Program?

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.


Write a program in c plus plus to compute first of non-terminal?

there is no solution of this problem...........that's it..........


How many classes can we write in a single c plus plus program?

Its limited only by available memory.


What is the sort cut for running c plus plus program?

It depends on the particular IDE. Visual Studio uses &lt;Ctrl&gt;F5 to start a program in non-debug mode, and F5 to start a program in debug mode.