Words of Wisdom:

"when your think your fooling them, their fooling you." - Uncivilbanks

C Programs - Experiment – 1 (a)

  • Date Submitted: 03/22/2013 06:29 AM
  • Flesch-Kincaid Score: 76.9 
  • Words: 2944
  • Essay Grade: no grades
  • Report this Essay
Experiment – 1 (a)
Aim – Write a program to implement Bubble Sort on an array.
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int a[10],i,j,n,temp,k;
clrscr();
printf("Enter limit: ");
scanf("%d",&n);
printf("Enter array elements: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<=n-1;i++)
{
for(j=0;j<n-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
/* for(k=0;k<n;k++)
{
printf("%d ",a[k]);
}
printf("\n");                       */
}
printf("Sorted array: ");
for(i=0;i<n;i++)
printf("%d ",a[i]);
getch();
}

Output:
Enter limit: 5
Enter array elements: 1 52 3 2 4
Sorted array: 1 2 3 4 52

Experiment – 1 (b)
Aim – Write a program to implement Selection Sort on an array.
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int a[10],i,j,n,temp,k;
clrscr();
printf("Enter limit :");
scanf("%d",&n);
printf("Enter array elements: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
/* for(k=0;k<n;k++)
{
printf("%d ",a[k]);
}
printf("\n");                 */
}
printf("Sorted array: ");
for(i=0;i<n;i++)
printf("%d ",a[i]);
getch();
}

Output:
Enter limit: 7
Enter array elements: 1 52 32 2 4 7 8
Sorted array: 1 2 3 4 7 8 32 52

Experiment – 2 (a)
Aim – Write a program to implement Linear Search on an array.
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int a[10],i,n,num,flag=0;
clrscr();
printf("Enter limit: ");
scanf("%d",&n);
printf("Enter array elements: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter number to be searched: ");
scanf("%d",&num);
for(i=0;i<n;i++)
{
if(a[i]==num)
{
flag=1;...

Comments

Express your owns thoughts and ideas on this essay by writing a grade and/or critique.

  1. No comments