0% found this document useful (0 votes)
15 views17 pages

DAA Prac. Complete

The document provides a detailed explanation and implementation of two algorithms: Quick Sort and the N-Queen Problem. Quick Sort is implemented in C with a time complexity of O(n log n) and space complexity of O(log n), while the N-Queen Problem is also implemented in C with a time complexity of O(n!) and space complexity of O(n). Both programs include step-by-step algorithms and sample outputs.

Uploaded by

ubantumalik2
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)
15 views17 pages

DAA Prac. Complete

The document provides a detailed explanation and implementation of two algorithms: Quick Sort and the N-Queen Problem. Quick Sort is implemented in C with a time complexity of O(n log n) and space complexity of O(log n), while the N-Queen Problem is also implemented in C with a time complexity of O(n!) and space complexity of O(n). Both programs include step-by-step algorithms and sample outputs.

Uploaded by

ubantumalik2
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/ 17

Practical No.

- 7

Objective: - Write a program for Quick Sort.

Algorithm:

Step 1 - Choose the highest index value has pivot

Step 2 - Take two variables to point left and right of the list excluding pivot

Step 3 - Left points to the low index

Step 4 - Right points to the high

Step 5 - While value at left is less than pivot move right

Step 6 - While value at right is greater than pivot move left

Step 7 - If both step 5 and step 6 does not match swap left and right

Step 8 - If left ≥ right, the point where they met is new pivot

Program:

#include <stdio.h> swap(&arr[i], &arr[j]);


void swap(int *a, int *b) }
{ }
int temp = *a; swap(&arr[i + 1], &arr[high]);
*a = *b; return (i + 1);
*b = temp; }
} void quickSort(int arr[], int low, int high)
int partition(int arr[], int low, int high) {
{ if (low < high)
int pivot = arr[high]; {
int i = (low - 1); int pi = partition(arr, low, high);
for (int j = low; j < high; j++)
{ quickSort(arr, low, pi - 1);
if (arr[j] <= pivot) quickSort(arr, pi + 1, high);
{ }
i++; }
13
void printArray(int arr[], int size) int arr[n];
{ printf("Enter the elements:\n");
for (int i = 0; i < size; i++) for (int i = 0; i < n; i++)
{ {
printf("%d ", arr[i]); scanf("%d", &arr[i]);
} }
printf("\n"); printf("Original array: \n");
} printArray(arr, n);
int main() quickSort(arr, 0, n - 1);
{ printf("Sorted array: \n");
int n; printArray(arr, n);
printf("Enter the number of elements: "); return 0;
scanf("%d", &n); }

Output:
Enter the number of elements: 6
Enter the elements:
23
10
9
60
1
43
Original array:
23 10 9 60 1 43
Sorted array:
1 9 10 23 43 60

Time Complexity: O(n log n)


Space Complexity: O(log n)

14
Practical No.-8

Objective: -Write a program for N-Queen Problem.

Algorithm:

Step 1 - Place the queen row-wise, starting from the left-most cell.

Step 2 - If all queens are placed then return true and print the solution matrix.

Step 3 - Else try all columns in the current row.

 Condition 1 - Check if the queen can be placed safely in this column, then mark the current
cell [Row, Column] in the solution matrix as 1 and try to check the rest of the problem
recursively by placing the queen here leads to a solution or not.
 Condition 2 - If placing the queen [Row, Column] can lead to the solution return true and
print the solution for each queen's position.
 Condition 3 - If placing the queen cannot lead to the solution then unmark this [row,
column] in the solution matrix as 0, BACKTRACK, and go back to condition 1 to try other
rows.

Step 4 - If all the rows have been tried and nothing worked, return false to trigger backtracking.

Program:

#include <stdio.h> for (i = 1; i <= n; i++)

#include <math.h> {

int chess_board[20], count; printf("\t%d", i);

void n_Queen(int row, int n); }

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

void print_board(int n) {

{ printf("\n\n%d\t", i);

int i, j; for (j = 1; j <= n; j++)

printf("\n\n N-Queen Solution %d : \n\n", {


++count);
if (chess_board[i] == j)

printf("Q\t");
15
else for (col = 1; col <= n; col++)

printf("-\t"); {

} if (check_place(row, col))

} {

} chess_board[row] = col;

if (row == n)

int check_place(int row, int col) print_board(n);

int i; else

for (i = 1; i <= row - 1; i++) n_Queen(row + 1, n);

{ }

if (chess_board[i] == col) }

return 0; }

else if (abs(chess_board[i] - col) == abs(i -


row))
int main()
return 0;
{
}
int n;
return 1;
printf("\nEnter Number of n_Queens : ");
}
scanf("%d", &n);

n_Queen(1, n);
void n_Queen(int row, int n)
return 0;
{
}
int col;

16
Output:

Enter Number of n_Queens : 4

N-Queen Solution 1 : N-Queen Solution 2 :

1 2 3 4 1 2 3 4

1 - Q - - 1 - - Q -

2 - - - Q 2 Q - - -

3 Q - - - 3 - - - Q

4 - - Q - 4 - Q - -

Time Complexity: O(n!)

Space Complexity: O(n)

17

You might also like