Beruflich Dokumente
Kultur Dokumente
Ajeet Shakya 19
while((a[loc] >= a[left]) && (loc!=left))
left++;
if(loc==left)
flag =1;
else if(a[loc] <a[left])
{
temp = a[loc];
a[loc] = a[left];
a[left] = temp;
loc = left;
}
}
}
return loc;
}
void quickSort(int a[], int beg, int end)
{
int loc;
if(beg<end)
{
loc = partition(a, beg, end);
quickSort(a, beg, loc-1);
quickSort(a, loc+1, end);
}
}
OUTPUT
Ajeet Shakya 20
PROGRAM -10
Q-10 Program for Counting Sort
#include <stdio.h>
void counting_sort(int A[], int k, int n)
{
int i, j;
int B[15], C[100];
for (i = 0; i <= k; i++)
C[i] = 0;
for (j = 1; j <= n; j++)
C[A[j]] = C[A[j]] + 1;
for (i = 1; i <= k; i++)
C[i] = C[i] + C[i-1];
for (j = n; j >= 1; j--)
{
B[C[A[j]]] = A[j];
C[A[j]] = C[A[j]] - 1;
}
printf("The Sorted array is : ");
for (i = 1; i <= n; i++)
printf("%d ", B[i]);
}
int main()
{
int n, k = 0, A[15], i;
clrscr();
printf("Enter the number of input : ");
scanf("%d", &n);
printf("\nEnter the elements to be sorted :\n");
for (i = 1; i <= n; i++)
{
scanf("%d", &A[i]);
if (A[i] > k) {
k = A[i];
}
}
Ajeet Shakya 21
counting_sort(A, k, n);
printf("\n");
return 0;
}
OUTPUT
Ajeet Shakya 22
PROGRAM -11
Q-11 Program for Bucket Sort
#include <stdio.h>
void Bucket_Sort(int array[], int n)
{
int i, j;
int count[10];
for (i = 0; i < n; i++)
count[i] = 0;
int main()
{
int array[100], i, num;
clrscr();
Ajeet Shakya 23
OUTPUT
Ajeet Shakya 24
PROGRAM -12
Q-12 Program for Radix Sort
#include <stdio.h>
int largest(int a[]);
void radix_sort(int a[]);
void main()
{
int i;
int a[10]={90,23,101,87,2,65,43,12,98,32,};
clrscr();
radix_sort(a);
printf("\n The sorted array is: \n");
for(i=0;i<10;i++)
printf(" %d\t", a[i]);
}
Ajeet Shakya 25
for(pass=0;pass<NOP;pass++)
{
for(i=0;i<10;i++)
bucket_count[i]=0;
for(i=0;i<10;i++)
{
remainder = (a[i]/divisor)%10;
bucket[remainder][bucket_count[remainder]] = a[i];
bucket_count[remainder] += 1;
}
i=0;
for(k=0;k<10;k++)
{
for(j=0;j<bucket_count[k];j++)
{
a[i] = bucket[k][j];
i++;
}
}
divisor *= 10;
}
}
OUTPUT
Ajeet Shakya 26
Study of Sorting Network
Sorting Networks:
Only perform comparisons.
Can only handle inputs of a fixed size.
Sequence of comparisons is set in advance.
Comparisons can be performed in parallel.
Ajeet Shakya 27
Study of NP- Complete theory
Ajeet Shakya 28
Study of Cook’s theorem
Theorem-1
He emphasized the significance of polynomial time
reducibility. It means that if we have a polynomial time
reduction from one problem to another, this ensures that any
polynomial time algorithm from the second problem can be
converted into a corresponding polynomial time algorithm for
the first problem.
Theorem-2
He focused attention on the class NP of decision problems that
can be solved in polynomial time by a non-deterministic
computer. Most of the intractable problems belong to this class,
NP.
Theorem-3
He proved that one particular problem in NP has the property
that every other problem in NP can be polynomially reduced
to it. If the satisfiability problem can be solved with a
polynomial time algorithm, then every problem in NP can also
be solved in polynomial time.
If any problem in NP is intractable, then satisfiability problem
must be intractable. Thus, satisfiability problem is the hardest
problem in NP.
Ajeet Shakya 29
Theorem-4
Cook suggested that other problems in NP might share with the
satisfiability problem this property of being the hardest
member of NP.
Ajeet Shakya 30