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.
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);
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.
To write a C++ program to display the student details using class and array of object.
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 < 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 < numbers.length; i++) { System.out.print(numbers[i]+" "); } }
Using sorted(array,reverse=True)
//C program for Arranging 5 Numbers in Ascending Order #include<stdio.h> #include<conio.h> void main() { int a[5],i,j,t; clrscr(); printf("Enter 5 nos.\n\n"); for (i=0;i<5;i++) scanf("%d",&a[i]); for (i=0;i<5;i++) { for(j=i+1;j<5;j++) { if(a[i]>a[j]) { t=a[i]; a[i]=a[j]; a[j]=t; } } } printf("Ascending Order is:"); for(j=0;j<5;j++) printf("\n%d",a[j]); getch(); }
sorry
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.
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.
To write a C++ program to display the student details using class and array of object.
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 < 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 < numbers.length; i++) { System.out.print(numbers[i]+" "); } }
In programming this could be achieved using a numeric bubble sort. Excel allows this type of sorting to be used.
Using sorted(array,reverse=True)
2 x 3 x 5 x 5 = 150
//C program for Arranging 5 Numbers in Ascending Order #include<stdio.h> #include<conio.h> void main() { int a[5],i,j,t; clrscr(); printf("Enter 5 nos.\n\n"); for (i=0;i<5;i++) scanf("%d",&a[i]); for (i=0;i<5;i++) { for(j=i+1;j<5;j++) { if(a[i]>a[j]) { t=a[i]; a[i]=a[j]; a[j]=t; } } } printf("Ascending Order is:"); for(j=0;j<5;j++) printf("\n%d",a[j]); getch(); }
Sort the array then traverse the array, printing the element values as you go.
"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.
# 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