0% found this document useful (0 votes)
38 views7 pages

Program 9: To Sort A Given Array Using SHELL SORT

The document contains source code for two programs: 1) A shell sort program that sorts an array using the shell sort algorithm. It takes an unsorted array as input, sorts it using shell sort, and outputs the sorted array. 2) A longest common subsequence program that finds the longest common subsequence between two strings. It uses dynamic programming to construct tables storing the length of the LCS and a pattern showing the LCS, then prints the result.

Uploaded by

lakshya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views7 pages

Program 9: To Sort A Given Array Using SHELL SORT

The document contains source code for two programs: 1) A shell sort program that sorts an array using the shell sort algorithm. It takes an unsorted array as input, sorts it using shell sort, and outputs the sorted array. 2) A longest common subsequence program that finds the longest common subsequence between two strings. It uses dynamic programming to construct tables storing the length of the LCS and a pattern showing the LCS, then prints the result.

Uploaded by

lakshya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Program 9

To sort a given array using SHELL SORT.


Source Code:
#include<iostream.h>
#include<conio.h>
void shell(int* a,int l)
{
int temp=0;
for(int i=l/2;i>=0; i--)
{
for(int j=0;j+i<=l;j++)
{
if(a[j]>a[j+i])
{
temp=a[j];
a[j]=a[j+i];
a[j+i]=temp;
}
}
}
}

void main()

{
clrscr();
int a[]={3,4,6,7,9,12,7,98,3,45,67,34,12,90,23};
cout<<"\Unsorted Array : ";
for(int i=0;i<15;i++)
{
cout<<a[i]<<" ";
}
shell(a,14);
cout<<"\Sorted Array using SHELL SORT : ";
for(int i=0;i<15;i++)
{
cout<<a[i]<<" ";
}
getch();
}

Output:

Program 10
Write a program to find longest common
subsequence of two strings.
Source Code:
#include<iostream.h>
#include<conio.h>
void lcss(char* a,int m,char* b,int n,int lcs[][7],char pat[][7])
{
for(int i=1;i<m;i++)
{
for(int j=1;j<n;j++)
{
if(a[i]==b[j])
{
lcs[i][j]=lcs[i-1][j-1]+1;
pat[i][j]='/';
}
else if(lcs[i-1][j]>lcs[i][j-1])
{
lcs[i][j]=lcs[i-1][j];
pat[i][j]='|';
}
else
{

lcs[i][j]=lcs[i][j-1];
pat[i][j]=' ';
}
}
}
}

void printPattern(char pat[][7],char* a, int i,int j)


{
if(i==0 || j==0)
{
return ;
}
if(pat[i][j]=='/' )
{
printPattern(pat,a,i-1,j-1);
cout<<a[i];
}
else if(pat[i][j]=='|')
{
printPattern(pat,a,i-1,j);
}
else if(pat[i][j]==' ')
{
printPattern(pat,a,i,j-1);
}

}
void main()
{
clrscr();
char a[]={'\0','A','C','D','B','A','B','D'};
char b[]={'\0','A','B','D','B','C','A'};
char pat[8][7];
int lcs[8][7];
for(int i=0;i<8;i++)
{
lcs[i][0]=0;
}
for(int j=0;j<7;j++)
{
lcs[0][j]=0;
}
for(i=0;i<8;i++)
{
pat[i][0]=a[i];
}
for(j=0;j<7;j++)
{
pat[0][j]=b[j];
}

lcss(a,8,b,7,lcs,pat);

cout<<"Table to store Length of Longest Common SubSequence :: \n";


for(i=0;i<8;i++)
{
for(j=0;j<7;j++)
{
cout<<lcs[i][j]<<" ";
}
cout<<"\n";
}
cout<<"Table to store the Pattern of Longest Common SubSequence :: \n";
for(i=0;i<8;i++)
{
for(j=0;j<7;j++)
{
cout<<pat[i][j]<<" ";
}
cout<<"\n";
}
cout<<"\n The Longest Common SubSequence is :: ";
printPattern(pat,a,7,6);
getch();

Output:

You might also like