0% found this document useful (0 votes)
3 views7 pages

Daa Lab PR 11 12

The document contains two C/C++ programs: one for sorting a set of integers using the Merge Sort method and another for solving the N Queen's problem using backtracking. The Merge Sort program measures the time taken to sort arrays of random integers greater than 5000 and outputs the time for various sizes, while the N Queen's program prompts the user for the number of queens and displays a valid arrangement if one exists. Both programs include necessary functions for their respective algorithms and handle memory allocation appropriately.

Uploaded by

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

Daa Lab PR 11 12

The document contains two C/C++ programs: one for sorting a set of integers using the Merge Sort method and another for solving the N Queen's problem using backtracking. The Merge Sort program measures the time taken to sort arrays of random integers greater than 5000 and outputs the time for various sizes, while the N Queen's program prompts the user for the number of queens and displays a valid arrangement if one exists. Both programs include necessary functions for their respective algorithms and handle memory allocation appropriately.

Uploaded by

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

11.

Design and implement C/C++ Program to sort a given set of n integer


elements using Merge Sort method and compute its time complexity. Run the
program for varied values of n> 5000, and record the time taken to sort. Plot
a graph of the time taken versus n. The elements can be read from a file or
can be generated using the random number generator.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// Function to merge two sorted arrays


void merge(int arr[], int left, int mid, int right)
{
int i, j, k;
int n1 = mid - left + 1;
int n2 = right - mid;

int *L = (int *)malloc(n1 * sizeof(int));


int *R = (int *)malloc(n2 * sizeof(int));

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


L[i] = arr[left + i];
for (j = 0; j < n2; j++)
R[j] = arr[mid + 1 + j];

i = 0;
j = 0;
k = left;

while (i < n1 && j < n2)


{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}

while (i < n1)


{
arr[k] = L[i];
i++;
k++;
}

while (j < n2)


{
arr[k] = R[j];
j++;
k++;
}

free(L);
free(R);
}

// Function to implement Merge Sort


void mergeSort(int arr[], int left, int right)
{
if (left < right)
{
int mid = left + (right - left) / 2;

mergeSort(arr, left, mid);


mergeSort(arr, mid + 1, right);

merge(arr, left, mid, right);


}
}

// Function to generate random integers


void generateRandomArray(int arr[], int n)
{
for (int i = 0; i < n; i++)
arr[i] = rand() % 100000; // Generate random integers between 0 and
99999
}

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
}

int *arr = (int *)malloc(n * sizeof(int));


if (arr == NULL)
{
printf("Memory allocation failed\n");
return 1; // Exit if memory allocation fails
}

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();

// Calculate the time taken for one iteration


double time_taken = ((double)(end - start)) / CLOCKS_PER_SEC / 1000.0;

printf("Time taken to sort %d elements: %f seconds\n", n, time_taken);

free(arr);
return 0;
}
OUTPUT:-

nter number of elements: 6000


Time taken to sort 6000 elements: 0.000709 seconds

********************************************************************

Enter number of elements: 7000


Time taken to sort 7000 elements: 0.000752 seconds

********************************************************************

Enter number of elements: 8000


Time taken to sort 8000 elements: 0.000916 seconds

********************************************************************
Enter number of elements: 9000
Time taken to sort 9000 elements: 0.001493 seconds

********************************************************************

Enter number of elements: 10000


Time taken to sort 10000 elements: 0.001589 seconds

********************************************************************

Enter number of elements: 11000


Time taken to sort 11000 elements: 0.002562 seconds

********************************************************************

Enter number of elements: 12000


Time taken to sort 12000 elements: 0.001944 seconds

********************************************************************

Enter number of elements: 13000


Time taken to sort 13000 elements: 0.002961 seconds

********************************************************************

Enter number of elements: 15000


Time taken to sort 15000 elements: 0.003563 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>

// Function to print the solution


void printSolution(int **board, int N)
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
printf("%s ", board[i][j] ? "Q" : "#");
}
printf("\n");
}
}

// Function to check if a queen can be placed on board[row][col]


bool isSafe(int **board, int N, int row, int col)
{
int i, j;

// Check this row on left side


for (i = 0; i < col; i++)
{
if (board[row][i])
{
return false;
}
}

// Check upper diagonal on left side


for (i = row, j = col; i >= 0 && j >= 0; i--, j--)
{
if (board[i][j])
{
return false;
}
}

// Check lower diagonal on left side


for (i = row, j = col; j >= 0 && i < N; i++, j--)
{
if (board[i][j])
{
return false;
}
}

return true;
}

// A recursive utility function to solve N Queen problem


bool solveNQUtil(int **board, int N, int col)
{
// If all queens are placed, then return true
if (col >= N)
{
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;

// Recur to place rest of the queens


if (solveNQUtil(board, N, col + 1))
{
return true;
}

// If placing queen in board[i][col] doesn't lead to a solution,


// then remove queen from board[i][col]
board[i][col] = 0; // BACKTRACK
}
}

// 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);

for (int i = 0; i < N; i++)


{
free(board[i]);
}
free(board);
return true;
}

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##

You might also like