Search This Blog

Saturday, September 10, 2011

A Program for Selection Sort Algorithm...!!

/*Selection sorting selects the initially the first element as minimum valued and compare it with the all elements*/
***************************************************************************
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define FNAME1 "best case"
#define FNAME2 "avg case"
#define FNAME3 "wrost case"

FILE *f1,*f2,*f3;
void main()
{
 int i,j,ch,n,k,min,minpos,a[10000];
 int count=0;
 clrscr();
 printf("1-> Best case\t2-> Average case\t 3-> Worst case\n\n");
 printf("Enter your choice:");
 scanf("%d",&ch);
 printf("Enter the size of elements of file\t:");
 scanf("%d",&n);


Friday, September 9, 2011

Insertion Sort Program..!!

/*This Program performs the Insertion Sort Algorithm using FILE Structer*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
#define FNAME1 "best case"
#define FNAME2 "avg case"
#define FNAME3 "worst case"


FILE *f1,*f2,*f3;  //file pointer
void main()
{
 int i,j,ch,n,k,w,a[10000],c,temp;
 int cnt=0;
 clock_t start,end;
 clrscr();
 printf("1-> Best case\n2-> Average case\n 3-> Worst case\n\n");
 printf("Enter chice:");
 scanf("%d",&ch);
  printf("\nEnter size of elements in file\t:");
  scanf("%d",&n);
 switch(ch)
 {
  case 1:
   f1=fopen(FNAME1,"w");//here file will be created in TC, juss check it..!!
   for(i=0;i<n;i++)
   {
    a[i]=i;
    printf("%d ",a[i]);
    fprintf(f1,"%d\n",a[i]);
   }
   fclose(f1);

   i=0;
   f1=fopen(FNAME1,"r");
   while(!feof(f1))
   {
    fscanf(f1,"%d\n",&a[i]);
    printf("%d",a[i]);
    i++;
   }
   fclose(f1);


Friday, July 8, 2011

Sorting Algorithm_BUBBLE

/*Here is the program for bubble sorting..Enjoy Friends...!!!*/
#include<conio.h>
#include<stdio.h>
#define n 5
void main()
{
 int i,j,a[100],temp,count=0;
 clrscr();
 printf("Enter the elements\n");
 for(i=0;i<n;i++)
 {
  scanf("%d\n",&a[i]);
 }
 for(i=0;i<n;i++)
 {
  for(j=i;j<n;j++)
  {
   if(a[i]>a[j])
   {
    temp=a[i];
    a[i]=a[j];
    a[j]=temp;
    count++;                            //for counting complexity..
   }
  }
 }
 printf("\nAfter Sorting\n");
 for(i=0;i<n;i++)
 {
  printf("%d\n",a[i]);
 }
 printf("The no. of swapping required is:%d",count);
 getch();
}