Design Analysis Of Algorithms

by : -  Hardik  H. Kothadia        C.E.        R.K.  UNIVERSITY
CONT......         8 00 00 4 99 53
___________________________________________________________________________

Tutorial -1
__________________________________________________________

Q-1: write a program to implement sorting with BUBBLESORT METHOD.
ANS :
#include <stdio.h>
#include <conio.h>
Void main()
{
Int i,j,n,a[20],temp;
Clrscr();
Printf(“enter the number of elements:”);
Scanf(“%d”,&n);
Printf(“enter  the elements of array:”);
Scanf(“%d”,&a[i]);
For(i=0;i<n;i++)
{
Scanf(“%d”,&a[i]);
}
For(i=0;i<n-i;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;
}
}
}
Printf (“the ascending order is:\n”);
For (i=0;i<n;i++)
{
Printf(“%d \n”,a[j]);
}
getch();
}
__________________________________________________________________________

Q-2: write a program to implement sorting with INSERTIONSORT  METHOD.
ANS :
#include<stdio.h>
#include<conio.h>

void main()
{
int A[20], n, Temp, i, j;

clrscr();

printf("\n\n Enter number of elements..: ");

scanf("%d", &n);

printf("\n\t Enter the array.:");

for(i=0; i<n; i++)
{
scanf("\n%d", &A[i]);
}
for(i=1; i<n; i++)
{
Temp = A[i];
j = i-1;

while(Temp<A[j] && j>=0)
{
A[j+1] = A[j];
j = j-1;
}
A[j+1] = Temp;
}
printf("\n\tTHE ASCENDING ORDER LIST IS...:\n");

for(i=0; i<n; i++)
{
printf("\n\t%d", A[i]);
}
getch();
}

____________________________________________________________________________

Q-3: write a program to implement sorting with SELECTIONSORT  METHOD.
ANS :
#include <stdio>
#include<conio.h>
#define MAX 5

void SelSort(int X[],int start,int stop)
{
int begin=start;
int small=begin;
int tmp;

if(start<stop)
{
for(int i=begin+1;i<=stop;i++)
{
if(X[i] < X[small])
small=i;
}

tmp=X[begin];
X[begin]=X[small];
X[small]=tmp;
SelSort(X,start+1,stop);
}
}

int main()
{
int abc[MAX]={5,4,3,2,1};
printf("UNSORTED ARRAY:\n");

for(int i=0;i<5;i++)
{
printf("%d  ",abc[i]);
SelSort(abc,0,MAX-1);
printf("\nSORTED ARRAY:\n");
}

for(int i=0;i<5;i++)
{
printf("%d  ",abc[i]);
}
return 0;
}
___________________________________________________________________