Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- void Bubblesort (int data[ ],int n)
- { int tmp,i,j;
- printf("\ninitial array is ");
- for (i=0; i<n; i++)
- {
- printf("%d ",data[i]);
- }
- for (i=0; i<n-1; i++)
- {
- for (j=0; j<n-i-1; j++)
- if (data[j] > data[j+1])
- {
- tmp = data[j];
- data[j] = data[j+1];
- data[j+1] = tmp;
- }
- printf("\nThe array after iteration %d is ",i+1);
- for (j=0; j<n; j++)
- {
- printf("%d ",data[j]);
- }
- }
- printf("\nThe final array is ");
- for (i=0; i<n; i++)
- {
- printf("%d ",data[i]);
- }
- }
- int main()
- {
- int x[100],n,i;
- printf("Enter the no of element");
- scanf("%d",&n);
- printf("\nEnter element");
- for(i=0;i<n;i++)
- scanf("%d",&x[i]);
- Bubblesort(x,n);
- return 0;
- }
- OUTPUT:
- Enter the no of element5
- Enter element43 36 60 12 7
- initial array is 43 36 60 12 7
- The array after iteration 1 is 36 43 12 7 60
- The array after iteration 2 is 36 12 7 43 60
- The array after iteration 3 is 12 7 36 43 60
- The array after iteration 4 is 7 12 36 43 60
- The final array is 7 12 36 43 60 Press any key to continue
Advertisement
Add Comment
Please, Sign In to add comment