answersLogoWhite

0

Program for bubble sort using swap?

Updated: 8/17/2019
User Avatar

Wiki User

14y ago

Best Answer

#include
#include
int *bubble(int a[],int n);
int *y;
int j,i;
void main()
{
int value[]={10,2,3,5,9,6,2,3,6,8};
int n;
clrscr();
n=sizeof(value)/sizeof(int);
y=bubble(value,n);
for(i=0;i{
printf("%d\n",*y++);
}
getch();
}
int *bubble(int a[],int n)
{
int t;
for(i=0;i{
for(j=0;j{
if(a[j]<=a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
return a;
}

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Program for bubble sort using swap?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Bubble sort program in c using for loop?

/* ellipses (...) used to represent tabs for clarity */int array[N]; /* N must be contant literal in this example */bubblesort (int *a, int n) {... int swap = 1;... while (swap) {... ... int i;... ... int temp;... ... swap = 0;... ... for (i=0; i < n-2; i++) {... ... ... if (a[i] > a[i+1]) {... ... ... ... swap = 1;... ... ... ... temp = a[i];... ... ... ... a[i] = a[i+1];... ... ... ... a[i+1] = temp;... ... ... }... ... }... ... n--;... }}/* fill the array with some data */bubblesort (a, N);


Write a program to swap two numbers using function?

swap (int *a, int *b) { *a ^= *b; *b ^= *a; *a ^= *b; }


What is the purpose of the flag variable in a bubble sort?

to eliminate unnecessary swaps to eliminate unnecessary comparisons to stop as soon as the list is sorted to sort an array of unknown size


How do you write a program in C plus plus plus plus How do you write a program in C to swap two variables without using the third oneo swap two variables without using the third one?

To swap two variables without using a third variable, use exclusive or manipulation... a ^= b; b ^= a; a ^= b;


What are the advantages for bubble sort?

Bubble sort has no practical applications other than that it is often cited as an example of how not to write an algorithm. Insert sort is the best algorithm for sorting small lists of items and is often used in conjunction with quick sort to sort larger lists. Like insert sort, bubble sort is simple to implement and is a stable sort (equal items remain in the same order they were input). However, insert sort uses copy or move operations rather than swaps (which is actually three operations per swap) and is therefore quicker. The only time a bubble sort will work quicker than insert sort is when the array is already sorted, which renders the entire algorithm redundant. A modified algorithm that specifically tests if an array is sorted or not would be more efficient than a single-pass bubble sort.


How would you write a program to swap the values of character variable using call by address?

void swap (int* a, int* b) { if (!a !b) return; // can't swap a pointer to null *a^=*b^=*a^=*b; }


How do you program c plus plus to arrange 3 numbers in ascending order using swap?

void sort (int&amp; a, int&amp; b, int&amp; c) { if (a&gt;b) std::swap (a, b); if (b&gt;c) std::swap (b, c); else return; if (a&gt;b) std::swap (a, b); } Note that this is based upon a bubble sort algorithm. Although usually inefficient as a general sorting algorithm, given that we know there are only three elements means we can implement it reasonably efficiently without any additional space complexity. There will always be 2 or 3 comparisons but at most there will be 3 swaps. The only improvement we could really make is to implement a type of selection sort: void sort (int&amp; a, int&amp; b, int&amp; c) { if (a&gt;b &amp;&amp; a&gt;c) std::swap (a, c); else if (b&gt;c) std::swap (b, c); if (a&gt;b) std::swap (a, b); } Here we either make 3 or 4 comparisons but only 2 swaps at most. The assumption here is that a comparison is a quicker operation than a swap thus the selection sort method is more efficient. However, unless you were to sort millions of sets of three one after the other, you are unlikely to see any measurable difference in performance.


Algorithm of a programme of bubble sort?

The algorithm for bubble sort, also know as pair exchange...Set a swap flag falseLoop for the first N-1 elementsCompare each element with the following elementIf the two elements are in the correct order, continue to next loop iterationOtherwise, swap the two elements and set the swap flag trueAt loop end, if the swap flag is true, repeat starting at step 1Otherwise, sort completedBubble sort is so named because out of order elements "bubble" to the end of the array, moving one step per inner loop iteration.As stated above, the algorithm is slow because it takes a while for a significantly out of order element to reach its final point when it needs to come closer to the beginning of the array. It can be improved by introducing a "distance" parameter, initially set to one half of the array size, and using that distance in step 3 to choose the second element. After the algorithm is completed for that distance, the distance is halved, and we iterate the entire algorithm until the distance is only one. (This variation is more formally known as merge exchange, but it still retains the "bubble" characteristic.)


How do you write a program in C to swap two variables without using the third one using XOR?

a=a^b; b=a^b; a=a^b;


How do you write a program in c to swap two values by using functions?

#include using namespace std; void swap(int &amp;a, int &amp;b); int main() { int x=5,y=3; cout


Bubble sorting in c plus plus?

bubble_sort (int N, int *A) { int i; swap = 1; while (swap) { swap = 0; for (i=0; i&lt;N-1; ++i) { if (A[i] &gt; A[i+1]) { swap = 1; A[i] ^= A[i+1]; A[i+1] ^= A[i]; A[i] ^= A[i+1]; } } } }


What is the minimum number of comparisons in bubble sort?

A bubble sort may have a range from O(n-1) for a pre-sorted array, to O(n2-n) for a poorly implemented bubble sort algorithm. Given 20 elements, a best case scenario is 19 comparisons, and the worst case is 380 comparisons.