Sie sind auf Seite 1von 12

PROGRAM -9

Q-9 Program for Quick Sort


#include <stdio.h>
int partition(int a[], int beg, int end);
void quickSort(int a[], int beg, int end);
void main()
{
int i;
int arr[10]={90,23,101,45,65,23,67,89,34,23};
quickSort(arr, 0, 9);
printf("\n The sorted array is: \n");
for(i=0;i<10;i++)
printf(" %d\t", arr[i]);
}
int partition(int a[], int beg, int end)
{

int left, right, temp, loc, flag;


loc = left = beg;
right = end;
flag = 0;
while(flag != 1)
{
while((a[loc] <= a[right]) && (loc!=right))
right--;
if(loc==right)
flag =1;
else if(a[loc]>a[right])
{
temp = a[loc];
a[loc] = a[right];
a[right] = temp;
loc = right;
}
if(flag!=1)
{

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;

for (i = 0; i < n; i++)


(count[array[i]])++;

for (i = 0, j = 0; i < n; i++)


for(; count[i] > 0; (count[i])--)
array[j++] = i;
}

int main()
{
int array[100], i, num;
clrscr();

printf("Enter the elements to be sorted:\n");


for (i = 0; i <10 ; i++)
scanf("%d", &array[i]);
printf("\nThe array of elements before sorting : \n");
for (i = 0; i < 10; i++)
printf("%d ", array[i]);
printf("\nThe array of elements after sorting : \n");
Bucket_Sort(array, 10);
for (i = 0; i < 10; i++)
printf("%d ", array[i]);
printf("\n");
return 0;
}

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]);
}

int largest(int a[])


{
int larger=a[0], i;
for(i=1;i<10;i++)
{
if(a[i]>larger)
larger = a[i];
}
return larger;
}
void radix_sort(int a[])
{
int bucket[10][10], bucket_count[10];
int i, j, k, remainder, NOP=0, divisor=1, larger, pass;
larger = largest(a);
while(larger>0)
{
NOP++;
larger/=10;
}

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

(Serial) Sorting Algorithms:


We already know several (comparison- based) sorting algorithms:
Insertion sort, Bubble sort, Merge sort, Quick sort, Heap sort
 Execute one operation at a time
 Can handle arbitrarily large inputs
 Sequence of comparisons is not set in advance

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

A problem is said to be polynomial if there exists an


algorithm that solve the problem in time T(n)=O(nc), where c
is a constant.

Examples of polynomial problems:


 Sorting: O(n log n)= O(n2)
 All-pairs shortest path: O(n3)
 Minimum spanning tree: O(E log E)=O(E2)

A problem is said to be exponential if no polynomial-time algorithm


can be developed for it and if we can find an algorithm that solves it
in O(nu(n)), where u(n) goes to infinity as n goes to infinity.

The world of computation can be subdivided into three classes:


1. Polynomial problem (P)
2. Exponential problem(E)
3. Intractable (non-computable) problems(I)

Ajeet Shakya 28
Study of Cook’s theorem

Stephen Cook presented four theorems in his paper “The


Complexity of Theorem Proving Procedures”. These theorems
are stated below. We do understand that many unknown terms
are being used in this chapter, but we don’t have any scope to
discuss everything in detail.
Following are the four theorems by Stephen Cook −

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

Das könnte Ihnen auch gefallen