0% found this document useful (0 votes)
51 views2 pages

Bubble Sort

This program uses a bubble sort algorithm to sort an array of integers entered by the user. It prompts the user to enter the number of elements and then the values for each element. Those elements are stored in an array. The bubble() function implements the bubble sort algorithm to sort the array. Finally, the sorted array is printed out.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views2 pages

Bubble Sort

This program uses a bubble sort algorithm to sort an array of integers entered by the user. It prompts the user to enter the number of elements and then the values for each element. Those elements are stored in an array. The bubble() function implements the bubble sort algorithm to sort the array. Finally, the sorted array is printed out.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

1. include<stdio.

h>  
2. #include<conio.h>  
3.   
4.   void bubble(int a[],int n)  
5.   {  
6.         int i,j,t;  
7.          for(i=n-2;i>=0;i--)  
8.          {  
9.             for(j=0;j<=i;j++)  
10.   
11.                   {  
12.                     if(a[j]>a[j+1])  
13.                                     {  
14.                                       t=a[j];  
15.                                      a[j]=a[j+1];  
16.                                      a[j+1]=t;  
17.                                     }  
18.                    }  
19.          
20.   
21.            }//end for 1.  
22.   
23.   }//end function.  
24.   
25.   
26.   void main()  
27.   {  
28.   
29.       int a[100],n,i;  
30.   
31.       clrscr();  
32.   
33.       printf("\n\n Enter integer value for total no.s of elements to be sorted: ");  
34.       scanf("%d",&n);  
35.   
36.       for( i=0;i<=n-1;i++)  
37.             { printf("\n\n Enter integer value for element no.%d : ",i+1);  
38.               scanf("%d",&a[i]);  
39.             }  
40.   
41.        bubble(a,n);  
42.   
43.        printf("\n\n Finally sorted array is: ");  
44.        for( i=0;i<=n-1;i++)  
45.        printf("%3d",a[i]);  
46.   
47.   } //end program.  
48.   
49. /* 
50.  
51. --------SAMPLE OUTPUT---------------------- 
52.  
53.  
54. Enter integer value for total no.s of elements to be sorted: 6 
55.  
56.  
57. Enter integer value for element no.1 : 89 
58.  
59.  
60. Enter integer value for element no.2 : -4 
61.  
62.  
63. Enter integer value for element no.3 : -67 
64.  
65.  
66. Enter integer value for element no.4 : 5 
67.  
68.  
69. Enter integer value for element no.5 : 78 
70.  
71.  
72. Enter integer value for element no.6 : 11 
73.  
74.  
75. Finally sorted array is: -67 -4 5 11 78 89 
76.  
77. ------------------------------------------ 
78.  
79. */  

You might also like