answersLogoWhite

0

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

12y ago

What else can I help you with?

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)


Java program to arrange any numbers in ascending order using scanner classes?

To arrange numbers in ascending order using Java, you can utilize the Scanner class to read input from the user and then sort the numbers using an array. Here's a simple example: import java.util.Arrays; import java.util.Scanner; public class AscendingOrder { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print(&quot;Enter the number of elements: &quot;); int n = scanner.nextInt(); int[] numbers = new int[n]; System.out.println(&quot;Enter the numbers:&quot;); for (int i = 0; i &lt; n; i++) { numbers[i] = scanner.nextInt(); } Arrays.sort(numbers); System.out.println(&quot;Numbers in ascending order: &quot; + Arrays.toString(numbers)); scanner.close(); } } This program collects a specified number of integers from the user, sorts them using Arrays.sort(), and then displays the sorted list.

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)


Java program to arrange any numbers in ascending order using scanner classes?

To arrange numbers in ascending order using Java, you can utilize the Scanner class to read input from the user and then sort the numbers using an array. Here's a simple example: import java.util.Arrays; import java.util.Scanner; public class AscendingOrder { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print(&quot;Enter the number of elements: &quot;); int n = scanner.nextInt(); int[] numbers = new int[n]; System.out.println(&quot;Enter the numbers:&quot;); for (int i = 0; i &lt; n; i++) { numbers[i] = scanner.nextInt(); } Arrays.sort(numbers); System.out.println(&quot;Numbers in ascending order: &quot; + Arrays.toString(numbers)); scanner.close(); } } This program collects a specified number of integers from the user, sorts them using Arrays.sort(), and then displays the sorted list.


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


What is the prime factor of 150 using ascending order?

2 x 3 x 5 x 5 = 150


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.


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.