Bubble Sort The bubble sort is the oldest and simplest sort in use unfortunately, it is also the slowest.
Ex : Pass: 0
# include<stdio.h> void bubble sort(int A[20],int n); void main() { int A[30],n,i; printf(“Enter n value \n”); scanf(“%d”, &n); printf(“Enter the elements:\n”); for(i=0;i<n; i++) { scanf(“%d”,&A[i]); } bubble sort[A,n]; printf(“The following are the elements in sorting orfer:\n”); for(i=0;i<n;i++) { printf(“%5d”,a[i]); } } void bubble sort(int A[20],int n) { int pass, j,temp; for(pass=0;pass<n-1;pass++) { for(j=0;j<n-pass-1;j++) { if(a[j]>a[j+1]) { temp=A[j]; A[j]=A[j+1]; A[j+1]=temp; } } } } Enter n value: 10 Enter the elements : 5 4 1 6 7 9 2 3 8 The following are the elements in the sorting order : 1 2 3 4 5 6 7 8 9 10
Traditionally, the time for a sorting algorithm is measured in terms of the number of comparisons specially, There are n-1 comparisons during the first pass, which phases the largest element in the last position, these are n-2 comparisons in second step, which places the second largest element in the next-to-last position, and so on. Thus f(n)=(n-1) + (n-2) +(n-3) + -------------- +2+1 =n(n-1)/2=n2/2-n/2=0(n2)
You liked the article?
Like : 0
Vote for difficulty
Current difficulty (Avg): Medium
1/15
TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.
Stay Updated
Get stories of change makers and innovators from the startup ecosystem in your inbox