Showing posts with label Bubble sort. Show all posts
Showing posts with label Bubble sort. Show all posts

Tuesday, 29 September 2015

Write a C rogram To Perform Bubble Sort using Insertion Of Numbers

1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  #include<stdlib.h>  
4:  #define MAX 5  
5:  void main()  
6:  {  
7:   int i,hold,pass,a[MAX];  
8:   clrscr();  
9:   printf("Enter the numbers:\n");  
10:   for(i=0;i<=MAX-1;i++)  
11:   {  
12:   scanf("%d",&a[i]);  
13:   }  
14:   printf("\n");  
15:   printf("Array in sorted order is:\n");  
16:   for (pass=1;pass<MAX;pass++)  
17:   {  
18:   for (i=0;i<=MAX-pass;i++)  
19:   {  
20:    if (a[i]>a[i+1])   
21:    {   
22:    hold=a[i];  
23:    a[i]=a[i+1];  
24:    a[i+1]=hold;  
25:    }  
26:   }  
27:   }  
28:   for(i=0;i<=MAX-1;i++)  
29:   {  
30:   printf("%d\t",a[i]);  
31:   }  
32:   getch();  
33:  }  

Wednesday, 12 August 2015

Write a C Program To Implement Bubble Sort

/* bubble sort implementation */
#include<stdio.h>
#include<conio.h>
void main()
{
    int i,n,temp,j,arr[25];
    clrscr();
    printf("Enter the number of elements in the Array: ");
    scanf("%d",&n);
    printf("\nEnter the elements:\n\n");
    for(i=0 ; i<n ; i++)
    {
        printf(" Array[%d] = ",i);
        scanf("%d",&arr[i]);
    }   
    for(i=0 ; i<n ; i++)
    {
        for(j=0 ; j<n-i-1 ; j++)
        {
            if(arr[j]>arr[j+1]) //Swapping Condition is Checked
            {
                temp=arr[j];
                arr[j]=arr[j+1];
                arr[j+1]=temp;
            }
        }
    }
        printf("\nThe Sorted Array is:\n\n");
        for(i=0 ; i<n ; i++)
{
printf(" %4d",arr[i]);
}
getch();
}