11. C PROGRAMMING LAB | Check Now

C PROGRAMMING LAB –11] Develop a program to sort the given set of N numbers using Bubble sort


Algorithm

  • Step-1: Start
  • Step-2: Read un-sorted array of n elements into a[i], where i is the index value
  • Step-3: Initialize index i=0
  • Step-4: Check for the i if it is less than n. if ture, than goto step-5. Other wise goto step output array
  • Step-5: Initialize the index J to zero
  • Step-6: Check if j is less than (n-i-1). If it is true than goto step-7, otherwise increment j and goto step-4
  • Step-7: Check if a[j] is greater than a[j+1](i.e-adjacent elements are compared) inside the for loop. if true swap the elements using temporary variables, otherwise goto step-6
  • Step-8: Using the for loop output the sorted array elements
  • Step-9: Stop
See also  Machine Learning AO* Algorithm | Read Now

C PROGRAMMING -Flow Chart

C PROGRAMMING-BUBBLE SORTING FLOW CHART- VTULOOP

Program -11 source code

#include<stdio.h>
int main()
{
int i,j,n,temp;
int a[20];
printf("enter the value of n");
scanf("%d",&n);
printf("Enter the numbers in unsorted order:\n");
for(i=0;i<n;i++)
scanf("%d", &a[i]);
 // bubble sort logic
for(i=0;i<n;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("The sorted array is\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
}

Output

Enter the value of n

4

Enter the numbers one by one in unsorted order:

2

1

3

5

4

The sorted array is

1

2

3

4

5

C PROGRAMMING -Viva Questions

1] Why the name bubble sort?

2] What are the different types of sorting techniques?

3] Explain the logic of bubble sort with an example

4] What is nested for loop?

Leave a Reply

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

WhatsApp Icon Join For Job Alerts