Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2015
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. //PROGRAM CODE 9.7 // function for bubble sort for array A having n elements
  2.  
  3. #include<iostream.h>
  4. #include<conio.h>
  5. #define max 10
  6. void bubblesort(int A[max], int n)
  7. {
  8. int i,j,temp;
  9. for( i = 1; i<n ; i++) // number of passes
  10. {
  11. for(j = 0; j<n-i ; j++) // j varies from 0 to n-i
  12. {
  13. if( A[j] > A[j+1] )// compare two successive numbers
  14. {
  15. temp = A[j]; // swap A[j] with A[j+1]
  16. A[j] = A[j+1];
  17. A[j+1] = temp;
  18. }
  19. }
  20. }
  21. }
  22.  
  23.  
  24. void main()
  25. {
  26. int i,size,A[20];
  27. clrscr();
  28. cout<<"Enter the size of the array: ";
  29. cin>>size;
  30. cout<<"\n\t Enter elememts";
  31. for (i=0;i<size;i++)
  32. {
  33. cin>>A[i];
  34. }
  35. bubblesort(A,size);
  36. cout<<"\n\t After selction sort Array elememts are = \n\t";
  37. for (i=0;i<size;i++)
  38. {
  39. cout<<A[i];
  40. }
  41.  
  42. getch();
  43. }
  44.  
  45. /*********************OUTPUT *******************************
  46. Enter the size of the array: 8
  47.  
  48. Enter elememts 2 1 9 7 4 0 3 5
  49.  
  50. After selction sort Array elememts are =
  51. 01234579
  52.  
  53. ***********************************************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement