0% found this document useful (0 votes)
8 views

Internal Programs

Uploaded by

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

Internal Programs

Uploaded by

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

I Internal Examination Programs

1. Quick Sort Using Divide and Conquer

#include <stdio.h>
#include <conio.h>
void quicksort(int[],int,int);
int partition(int[],int,int);
void main()
{
int i,n,a[20];
clrscr();
printf("Enter the number of elements:");
scanf("%d", &n);
printf("Enter the %d array elements:\n",n);
for(i=0;i<n;i++)
scanf("%d", &a[i]);
quicksort(a,0,n-1);
printf("The sorted array elements are:\n");
for(i=0;i<n;i++)
printf("\n %d", a[i]);
getch();
}
void quicksort(int a[],int low,int high)
{
int mid;
if(low<high)
{
mid=partition(a,low,high);
quicksort(a,low,mid-1);
quicksort(a,mid+1,high);
}
}
int partition(int a[],int low, int high)
{
int key,i,j,temp,k;
key=a[low];
i=low+1;
j=high;
while(i<=j)
{
while(i<=high && key>=a[i])
i=i+1;
while(key<a[j])
j=j-1;
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
else
{
k=a[j];
a[j]=a[low];
a[low]=k;
}
}
return j;
}

Output:
2. Merge Sort Using Divide and Conquer

#include <stdio.h>
#include <conio.h>
void Merge(int a[], int low, int mid, int high)
{
int i, j, k, b[20];
i=low;
j=mid+1;
k=low;
while(i<=mid && j<=high)
{
if(a[i]<=a[j])
b[k++]=a[i++];
else
b[k++]=a[j++];
}
while (i<=mid)
b[k++]=a[i++];
while(j<=high)
b[k++]=a[j++];
for(k=low;k<=high;k++)
a[k]=b[k];
}
void MergeSort(int a[], int low, int high)
{
int mid;
if(low>=high)
return;
mid=(low+high)/2;
MergeSort(a, low,mid);
MergeSort(a, mid+1, high);
Merge(a, low, mid, high);
}
void main()
{
int n, a[20],i;
clrscr();
printf("\n Enter the size of array:");
scanf("%d", &n);
printf("\n Enter %d numbers:\n",n);
for(i=0;i<n;i++)
scanf("%d", &a[i]);
MergeSort(a, 0, n-1);
printf("\n Sorted numbers are:\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}
Output:
3. minimum and maximum value in an array using divide and conquer

#include <stdio.h>
#include <conio.h>
int max, min;
int a[100];
void maxmin(int i, int j)
{
int max1, min1, mid;
if(i==j)
{
max=min=a[i];
}
else
{
if(i==j-1)
{
if(a[i]<a[j])
{
max=a[j];
min=a[i];
}
else
{
max=a[i];
min=a[j];
}
}
else
{
mid=(i+j)/2;
maxmin(i, mid);
max1=max;
min1=min;
maxmin(mid+1,j);
if(max<max1)
max=max1;
if(min>min1)
min=min1;
}
}
}
void main()
{
int i, num;
clrscr();
printf("\nEnter the total number of elements:");
scanf("%d", &num);
printf("Enter the numbers:\n");
for(i=1;i<=num;i++)
scanf("%d", &a[i]);
max=a[0];
min=a[0];
maxmin(1,num);
printf("Minimum element in an array:%d\n",min);
printf("Maximum element in an array:%d\n",max);
getch();
}

Output:
4. Sort a list of n elements using Selection sort technique in c

#include <stdio.h>
#include <conio.h>
void swap(int *x, int *y) {
int temp = *x;
*x = *y;
*y = temp;
}
void selectionSort(int arr[], int n) {
int i, j, min_index;
for (i = 0; i < n - 1; i++) {
min_index = i;
for (j = i + 1; j < n; j++)
if (arr[j] < arr[min_index])
min_index = j;
swap(&arr[min_index], &arr[i]);
}
}
void printArray(int arr[], int size) {
int i;
for (i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
void main() {
int n, i, arr[20];
clrscr();
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter the elements of the array:\n");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
selectionSort(arr, n);
printf("Sorted array elements are: \n");
printArray(arr, n);
getch();
}

Output:
5. Implement Linear Search

#include <stdio.h>
#define MAX 5

int linearSearch(int *a, int n)


{
int i,pos=-1;

for(i=0;i<MAX;i++) {
if(a[i]==n) {
pos=i;
break;
}
}
return pos;
}

int main()
{
int i, n, arr[MAX];
int num;
int position;
clrscr();
printf("\nEnter array elements:\n");
for (i=0;i<MAX;i++)
scanf("%d", &arr[i]);

printf("\nNow enter element to search :");


scanf("%d", &num);

position = linearSearch(arr, num);

if(num==-1)
printf("Element not found.\n");
else
printf("Element found @ %d position.\n", position);
getch();
return 0;
}

Output:
6. 0/1 Knapsack Problem Using Dynamic Programming.

#include <stdio.h>
#include <conio.h>
int max(int a, int b)
{
return (a > b)? a : b;
}
int knapSack(int W, int wt[], int val[], int n)
{
int i, w;
int K[100][100];
for (i = 0; i <= n; i++)
{
for (w = 0; w <= W; w++)
{
if (i==0 || w==0)
K[i][w] = 0;
else if (wt[i-1] <= w)
K[i][w] = max(val[i-1] + K[i-1][w-wt[i-1]],
K[i-1][w]);
else
K[i][w] = K[i-1][w];
}
} return K[n][W];
}
int main()
{
int i, n, val[20], wt[20], W;
clrscr();
printf("Enter number of items:");
scanf("%d", &n); printf("Enter value and weight of items:\n");
for(i = 0;i < n; ++i)
{
scanf("%d%d", &val[i], &wt[i]);
}
printf("Enter size of knapsack:");
scanf("%d", &W);
printf("Value=%d", knapSack(W, wt, val, n));
getch();
return 0;
}
Output:

You might also like