Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //PROGRAM CODE 9.7 // function for bubble sort for array A having n elements
- #include<iostream.h>
- #include<conio.h>
- #define max 10
- void bubblesort(int A[max], int n)
- {
- int i,j,temp;
- for( i = 1; i<n ; i++) // number of passes
- {
- for(j = 0; j<n-i ; j++) // j varies from 0 to n-i
- {
- if( A[j] > A[j+1] )// compare two successive numbers
- {
- temp = A[j]; // swap A[j] with A[j+1]
- A[j] = A[j+1];
- A[j+1] = temp;
- }
- }
- }
- }
- void main()
- {
- int i,size,A[20];
- clrscr();
- cout<<"Enter the size of the array: ";
- cin>>size;
- cout<<"\n\t Enter elememts";
- for (i=0;i<size;i++)
- {
- cin>>A[i];
- }
- bubblesort(A,size);
- cout<<"\n\t After selction sort Array elememts are = \n\t";
- for (i=0;i<size;i++)
- {
- cout<<A[i];
- }
- getch();
- }
- /*********************OUTPUT *******************************
- Enter the size of the array: 8
- Enter elememts 2 1 9 7 4 0 3 5
- After selction sort Array elememts are =
- 01234579
- ***********************************************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement