Sunday, August 19, 2012

A Bubble Sort Program in C

This program just demonstrates what a Bubble Sorting program looks like:
Compiler used: Turbo C
Language: C
 
#include<stdio.h>
#include<conio.h>

void main(){
clrscr();
int arr[]={10,20,30,3,6,23,4}, i=0, swap=0, sort=0, j=0;
for(i=0;i<6;i++){
    sort=1;
    for(j=1;j<7;j++){
        if(arr[j]<arr[j-1]){
        swap=arr[j];
        arr[j]=arr[j-1];
        arr[j-1]=swap;
        sort = 0;
        }//end of if statement
    }
    if(sort==1){
    break;
    }
}
for(i=0;i<7;i++){
    printf("%d ",arr[i]);
}

getch();
}

PS: Many improvements are possible in the program. You are welcome to suggest them in comments! :)

No comments:

Post a Comment