AOA Experiments
AOA Experiments
AOA Experiments
Selection Sort
Code:
#include<stdio.h>
#include<conio.h>
void main(){
int size,i,j,temp,list[100];
printf("Dhruva Kawli\n");
printf("Enter the size of the list:");
scanf("%d",&size);
printf("Enter %d integer
values:",size); for(i=0;i<size;i++)
scanf("%d",&list[i]);
for(i=0;i<size;i++){
for(j=i+1;j<size;j++){
if(list[i]>list[j])
{
temp=list[i];
list[i]=list[j];
list[j]=temp;
}
}
}
printf("list after sorting is:");
for(i=0;i<size;i++)
printf("\t%d",list[i]);
getch();
}
Output:
Insertion Sort
Code:
#include<stdio.h>
#include<conio.h>
void main(){
int size,i,j,temp,list[100];
printf("Dhruva Kawli\n");
printf("Enter the size of the
list:"); scanf("%d",&size);
printf("Enter %d integer
values:",size); for(i=0;i<size;i++)
scanf("%d",&list[i]);
for(i=0;i<size;i++){
temp=list[i];
j=i-1;
while((temp<list[j])&&(j>=0)){ list[j+1
]=list[j];
j=j-1;
}
list[j+1]=temp;
}
printf("list after sorting
is:"); for(i=0;i<size;i++)
printf("\t%d",list[i]);
getch();
}
Output:
Experiment No:2
Merge Sort
Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void
merge_sort(int I, int j, int a[], int temp[]) {
if(j<=i)
{
return;
}
int mid = (i+j)/2;
merge_sort(I,mid,a,temp);
merge_sort(mid+1,j,a,temp);
int left=I;
int right=mid+1;
int k;
for (k = I; k <= j; k++)
{
if (left == mid + 1)
{
temp[k] = a[right];
right++;
}
else if (right == j + 1)
{
temp[k] = a[left];
left++;
} else if (a[left] < a[right])
{
temp[k] = a[left];
left++;
} else
{
temp[k] = a[right];
right++;
}
}
for (k=I;k<=j;k++)
{
a[k] = temp[k];
}
}
int main()
{
int a[50], temp[50], n, I, d, swap;
printf(“Dhruva Kawli\n”);
printf(“Enter size of an array : “);
scanf(“%d”, &n);
printf(“Enter the %d elements for sorting \n”,
n); for (I = 0; I < n; i++)
scanf(“%d”, &a[i]);
merge_sort(0, n – 1, a, temp);
printf(“Sorted array:\n”);
for (I = 0; I < n; i++)
printf(“%d “, a[i]);
return 0;
}
Output:
Quick sort
Code:
#include<stdio.h>
void quicksort(int number[25],int first,int
last){ int I, j, pivot, temp;
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j){
while(number[i]<=number[pivot]&&i<la
st) i++;
while(number[j]>number[pivot])
j--;
if(i<j){
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
temp=number[pivot];
number[pivot]=number[j];
number[j]=temp;
quicksort(number,first,j-1);
quicksort(number,j+1,last);
}
}
int main(){
int I, count, number[25];
printf(“Dhruva Kawli\n”);
printf(“Enter number of elements: “);
scanf(“%d”,&count);
printf(“Enter %d elements: “, count);
for(i=0;i<count;i++)
scanf(“%d”,&number[i]);
quicksort(number,0,count-1);
printf(“The Sorted Order is: “);
for(i=0;i<count;i++)
printf(“ %d”,number[i]);
return 0;
}
Output:
Experiment No 3
Code:-
#include<stdio.h>
#include<conio.h>
#define INFINITY 9999
#define MAX 10
int main()
{
int G[MAX][MAX],i,j,n,u;
printf("Enter no. of vertices:");
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n"); for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
printf("\nEnter the starting node:");
scanf("%d",&u);
dijkstra(G,n,u);
return 0;
}
Experiment no. 4
Code:
# include<stdio.h>
void knapsack(int n, float weight[], float profit[], float
capacity) {
float x[20], tp = 0;
int i, j, u;
u = capacity;
if (i < n)
x[i] = u / weight[i];
tp = tp + (x[i] * profit[i]);
int main() {
float weight[20], profit[20], capacity;
int num, i, j;
float ratio[20], temp;
temp = weight[j];
weight[j] = weight[i];
weight[i] = temp;
temp = profit[j];
profit[j] = profit[i];
profit[i] = temp;
}
}
}
Output:
#include <stdio.h>
void main()
{
int a[100][100],n,i,j,k;
printf("Dhruva Kawli\n");
printf("Roll no: 57\n");
printf("Enter the number of rows and column:"); scanf("%d", &n);
printf("\n");
}
Output:
Dhruva Kawli
Roll no: 57
Enter the number of rows and column:3 Enter the matrix:
0 4 11
602
3 9999 0
The Entered matrix is:
0 4 11
602
3 9999 0
output after calculation is:
046
502
370
Experiment No: 6
Program:
#include <stdio.h>
void main()
{
int a[100][100],n,i,j,k;
printf("Dhruva Kawli\n");
printf("Roll no: 57\n");
printf("Enter the number of rows and column:"); scanf("%d", &n);
printf("\n");
}
Output:
Dhruva Kawli
Roll no: 57
Enter the number of rows and column:3 Enter the matrix:
0 4 11
602
3 9999 0
The Entered matrix is:
0 4 11
602
3 9999 0
output after calculation is:
046
502
370
Experiment No: 7
Code:
#include<stdio.h>
#include<string.h>
#define d 256
void search(char pat[], char txt[], int q) {
int M = strlen(pat); int N = strlen(txt); int i, j; int p = 0; int t = 0; int h = 1;
for (i = 0; i < M-1; i++)
h = (h*d)%q;
for (i = 0; i < M; i++)
{
p = (d*p + pat[i])%q;
t = (d*t + txt[i])%q;
}
for (i = 0; i <= N - M; i++)
{
if ( p == t )
{
for (j = 0; j < M; j++)
{
if (txt[i+j] != pat[j])
break;
}
if (j == M)
printf("Pattern found at index %d \n", i); }
if ( i < N-M )
{
t = (d*(t - txt[i]*h) + txt[i+M])%q; if (t < 0)
t = (t + q);
}
}
}
int main()
{ char txt[80],pat[80];
int q;
printf("Enter some text \n");
scanf("%s",txt);
printf("Enter a pattern to be searched \n"); scanf("%s",&pat);
printf("Enter a prime number \n"); scanf("%d",&q);
search(pat, txt, q);
return 0;
}
Output:
Enter some text
2359023141526739921
Enter a pattern to be searched 31415
Enter a prime number
13
Pattern found at index 6
Experiment No 8
Code:
#include <stdbool.h>
#include <stdio.h>
void printSolution(int board[10][10],int N) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++)
printf(" %d ", board[i][j]);
printf("\n");
}
}
bool isSafe(int board[10][10], int row, int col, int N) {
int i, j;
for (i = 0; i < col; i++)
if (board[row][i])
return false;
for (i = row, j = col; i >= 0 && j >= 0; i--, j--) if (board[i][j])
return false;
for (i = row, j = col; j >= 0 && i < N; i++, j--) if (board[i][j])
return false;
return true;
}
bool solveNQUtil(int board[10][10], int col, int N) {
if (col >= N)
return true;
for (int i = 0; i < N; i++) {
if (isSafe(board, i, col,N)) {
board[i][col] = 1;
if (solveNQUtil(board, col + 1,N))
return true;
board[i][col] = 0; // BACKTRACK
}
}
return false;
}
bool solveNQ()
{
int board[10][10], N,i,j;
printf("Enter the Size of board\n");
scanf("%d",&N);
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
board[i][j]=0;
}
}
if (solveNQUtil(board, 0,N) == false) { printf("Solution
does not exist"); return false;
}
printSolution(board,N);
return true;
}
int main()
{
solveNQ();
return 0;
}
Output:
Enter the Size of board 8
10000000 00000010 0
0001000 00000001 01
000000 00010000 000
00100 00100000