Daa Lab PR 11 12
Daa Lab PR 11 12
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
i = 0;
j = 0;
k = left;
free(L);
free(R);
}
int main()
{
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
if (n <= 5000)
{
printf("Please enter a value greater than 5000\n");
return 1; // Exit if the number of elements is not greater than 5000
}
generateRandomArray(arr, n);
// Repeat the sorting process multiple times to increase duration for timing
clock_t start = clock();
for (int i = 0; i < 1000; i++)
{
mergeSort(arr, 0, n - 1);
}
clock_t end = clock();
free(arr);
return 0;
}
OUTPUT:-
********************************************************************
********************************************************************
********************************************************************
Enter number of elements: 9000
Time taken to sort 9000 elements: 0.001493 seconds
********************************************************************
********************************************************************
********************************************************************
********************************************************************
********************************************************************
12. Design and implement C/C++ Program for N Queen’s problem using
Backtracking.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
return true;
}
// Consider this column and try placing this queen in all rows one by one
for (int i = 0; i < N; i++)
{
if (isSafe(board, N, i, col))
{
// Place this queen in board[i][col]
board[i][col] = 1;
// If the queen cannot be placed in any row in this column col, then return
false
return false;
}
bool solveNQ(int N)
{
int **board = (int **)malloc(N * sizeof(int *));
for (int i = 0; i < N; i++)
{
board[i] = (int *)malloc(N * sizeof(int));
for (int j = 0; j < N; j++)
{
board[i][j] = 0;
}
}
if (!solveNQUtil(board, N, 0))
{
printf("Solution does not exist\n");
for (int i = 0; i < N; i++)
{
free(board[i]);
}
free(board);
return false;
}
printSolution(board, N);
int main()
{
int N;
printf("Enter the number of queens: ");
scanf("%d", &N);
solveNQ(N);
return 0;
}
OUTPUT
Enter the number of queens: 4
##Q#
Q###
###Q
#Q##