Bubble Sort

Bubble Sort in C Programming

#include
#include
int arr[20];
int n;
void get();
void BubbleSort();
void Show();

void main(){
clrscr();
printf(“- This Program Explains Sorting Using Bubble Sort Algorithm -\n\n”);
get();
BubbleSort();
Show();
getch();
}

void get(){
int i;
while(1){
printf(“Enter the size of the elements :\n”);
scanf(“%d”,&n);
if(n<=20)
break;
else
printf(“Sorry the maximum no of elements is 20\n\n”);
}
printf(“\n”);
printf(“———————-\n”);
printf(“Enter the values \n”);
printf(“———————-\n\n”);
for(i=0;i<n;i++)
scanf(“%d”,&arr[i]);
}

void BubbleSort(){
int i,j;
for(i=1;i<n;i++){

for(j=0;j<n-i;j++){

if(arr[j]>arr[j+1]){      //for descending use “<”

int t;
t=arr[j];
arr[j]=arr[j+1];
arr[j+1]=t;

}

}

}

}

void Show(){
int i;
printf(“\n”);
printf(“———————–\n”);
printf(“Sorted Array Elements\n”);
printf(“———————–\n”);
for(i=0;i<n;i++){
printf(“%d\n”,arr[i]);
}
}

Comments

comments

Leave a Reply

Your email address will not be published. Required fields are marked *