answersLogoWhite

0


Best Answer

The most optimal form is as follows:

void bubble_sort( int* arr, size_t len )

{

do{

int n = 0; // used to record the last swap position

for( int i=1; i<len; ++i )

{

if( arr[i-1]>arr[i] )

{

// swap without a temporary...

arr[i-1]^=arr[i]^=arr[i-1]^=arr[i];

n = i;

}

}

len = n;

} while( len );

}

Note that while this form is the most optimal, nobody ever uses bubble sort in the real world, even for small sets of data (which is all it can really handle). Indeed, it has very little value even in the classroom. Its only true value is that it is an example of how not to write an algorithm. Insertion sort is a much better algorithm and is every bit as efficient, just as simple to understand and, paradoxically, much simpler to implement. It can even be used to sort smaller subsets of a much larger subset when combined with the more complex quicksort, heap sort or merge sort.

User Avatar

Wiki User

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

Wiki User

14y ago

int ctr,i;

do

{

ctr=0;

for(int i=1;i<=n-1;i++)

{

if(a[i]>a[i+1])

{

int t;

t=a[i];

a[i]=a[i+1];

a[i+1]=t;

ctr++;

}

}

}while(ctr>0);

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Cpp program for ascending order using bubble sort?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

A C program using dynamic memory allocation to sort n names in ascending order?

Writing a C program that uses dynamic memory allocation to sort names in ascending order is a typical computer science assignment. To write this program, you must be in UNIX.


How do you write a program to read set of numbers using by an array and display the ascending order of the given input numbers?

To write a C++ program to display the student details using class and array of object.


Ascending order program for java?

public class BubbleSortAscendingOrderDemo { public static void main(String a[]) { //Numbers which need to be sorted int numbers[] = {23,5,23,1,7,12,3,34,0}; //Displaying the numbers before sorting System.out.print("Before sorting, numbers are "); for(int i = 0; i &lt; numbers.length; i++) { System.out.print(numbers[i]+" "); } System.out.println(); //Sorting in ascending order using bubble sort bubbleSortInAscendingOrder(numbers); //Displaying the numbers after sorting System.out.print("Before sorting, numbers are "); for(int i = 0; i &lt; numbers.length; i++) { System.out.print(numbers[i]+" "); } }


Sort array in ascending descending order in python?

Using sorted(array,reverse=True)


Sort array in ascending descending order using function in c?

//C program for Arranging 5 Numbers in Ascending Order #include&lt;stdio.h&gt; #include&lt;conio.h&gt; void main() { int a[5],i,j,t; clrscr(); printf("Enter 5 nos.\n\n"); for (i=0;i&lt;5;i++) scanf("%d",&amp;a[i]); for (i=0;i&lt;5;i++) { for(j=i+1;j&lt;5;j++) { if(a[i]&gt;a[j]) { t=a[i]; a[i]=a[j]; a[j]=t; } } } printf("Ascending Order is:"); for(j=0;j&lt;5;j++) printf("\n%d",a[j]); getch(); }

Related questions

What will be the program to arrange numbers stored in array in ascending order using pointers?

sorry


How do you write Ascending order program using 8086 microprocessor?

One many find this answer on YouTube. One also may find out how to write ascending order programs using an 8086 microprocessor by looking at the owners manual.


A C program using dynamic memory allocation to sort n names in ascending order?

Writing a C program that uses dynamic memory allocation to sort names in ascending order is a typical computer science assignment. To write this program, you must be in UNIX.


How do you write a program to read set of numbers using by an array and display the ascending order of the given input numbers?

To write a C++ program to display the student details using class and array of object.


Ascending order program for java?

public class BubbleSortAscendingOrderDemo { public static void main(String a[]) { //Numbers which need to be sorted int numbers[] = {23,5,23,1,7,12,3,34,0}; //Displaying the numbers before sorting System.out.print("Before sorting, numbers are "); for(int i = 0; i &lt; numbers.length; i++) { System.out.print(numbers[i]+" "); } System.out.println(); //Sorting in ascending order using bubble sort bubbleSortInAscendingOrder(numbers); //Displaying the numbers after sorting System.out.print("Before sorting, numbers are "); for(int i = 0; i &lt; numbers.length; i++) { System.out.print(numbers[i]+" "); } }


What Program that lists telephone numbers in numeric order or street address?

In programming this could be achieved using a numeric bubble sort. Excel allows this type of sorting to be used.


Sort array in ascending descending order in python?

Using sorted(array,reverse=True)


What is the prime factor of 150 using ascending order?

2 x 3 x 5 x 5 = 150


Sort array in ascending descending order using function in c?

//C program for Arranging 5 Numbers in Ascending Order #include&lt;stdio.h&gt; #include&lt;conio.h&gt; void main() { int a[5],i,j,t; clrscr(); printf("Enter 5 nos.\n\n"); for (i=0;i&lt;5;i++) scanf("%d",&amp;a[i]); for (i=0;i&lt;5;i++) { for(j=i+1;j&lt;5;j++) { if(a[i]&gt;a[j]) { t=a[i]; a[i]=a[j]; a[j]=t; } } } printf("Ascending Order is:"); for(j=0;j&lt;5;j++) printf("\n%d",a[j]); getch(); }


How do you display 15 numbers in an ascending order using an array?

Sort the array then traverse the array, printing the element values as you go.


What does ascending order mean when using fractions?

"Ascending order" means each one is bigger or higher than the one before it. It doesn't matter whether they're fractions, whole numbers, mixed numbers, temperatures, costs, weights, volumes, decimals, etc.


To arrange data in a desired order?

# You can sort data in ascending order depending on the desired requirements # You can also sort data in descending order depending on the application package your using 2b hb 3b b h 3h