/*
Bubble Sort Algorithm
Created By: Pirate
*/
#include<stdio.h>
#include<conio.h>
void bubble(int items[],int n);
void main(){
int items[500],n,i;
printf("*** Bubble Sort ***\n");
printf("Enter the number of elements: ");
scanf("%d",&n);
printf("\nEnter the elements: \n");
for(i = 0; i < n; i++){
scanf("%d",&items[i]);
}
printf("\n\nBefore Sorting:\n");
for(i = 0; i < n; i++){
printf("%d\t",items[i]);
}
bubble(items,n);
printf("\n\nAfter Sorting:\n");
for(i = 0; i < n; i++){
printf("%d\t",items[i]);
}
getch();
}
void bubble(int items[],int n){
int i,j,temp;
for(i = 0; i < n; i++){
for(j = 0; j < n-1; j++){
if(items[j] > items[j+1]){
temp = items[j];
items[j] = items[j+1];
items[j+1] = temp;
}
}
}
}
Bubble Sort Algorithm
Created By: Pirate
*/
#include<stdio.h>
#include<conio.h>
void bubble(int items[],int n);
void main(){
int items[500],n,i;
printf("*** Bubble Sort ***\n");
printf("Enter the number of elements: ");
scanf("%d",&n);
printf("\nEnter the elements: \n");
for(i = 0; i < n; i++){
scanf("%d",&items[i]);
}
printf("\n\nBefore Sorting:\n");
for(i = 0; i < n; i++){
printf("%d\t",items[i]);
}
bubble(items,n);
printf("\n\nAfter Sorting:\n");
for(i = 0; i < n; i++){
printf("%d\t",items[i]);
}
getch();
}
void bubble(int items[],int n){
int i,j,temp;
for(i = 0; i < n; i++){
for(j = 0; j < n-1; j++){
if(items[j] > items[j+1]){
temp = items[j];
items[j] = items[j+1];
items[j+1] = temp;
}
}
}
}
Output :
0 comments:
Post a Comment