Wednesday, October 3, 2018

CPL 11 - BUBBLE SORT

11. Develop a program to sort the given set of N numbers using Bubble sort.

ALGORITHM:

Step 1: [read the value of N]
           Read N

Step 2: [Read N elements in to the array]
For i=0 to N,for j=0 to n-i-1

Step 3: Check for bubble sort technique
   A[j]>a[j+1]
Go to step 4 other wise repeat step 3

Step 4: temp=A[j];
A[j]=A[j+1]
A[j+1]=temp(SWAPPING OF ELEMENTS)

Step 5: Print the sorted array


Step 6: Stop

FLOWCHART:



PROGRAM:

#include<stdio.h>

void main()
{
   int a[20],n,i,j,temp;
   printf("Enter no. of elements\n");
   scanf("%d",&n);
   printf("Enter array elements\n");
   for(i=0;i<n;i++)
   {
      scanf("%d",&a[i]);
   }

   for(i=0;i<(n-1);i++)
   {
      for(j=0;j<(n-i-1);j++)
      {
        if(a[j]>a[j+1])
        {
           temp=a[j];
           a[j]=a[j+1];
           a[j+1]=temp;
        }
      }
   }

   printf("Sorted array is\n");
   for(i=0;i<n;i++)
   {
      printf("%d\n",a[i]);
   }

}

OUTPUT:

No comments:

Post a Comment