Skip to content

Commit b6979af

Browse files
committed
till 10
1 parent 53f3fb8 commit b6979af

File tree

22 files changed

+563
-1
lines changed
  • .vscode
  • daa
    • 10. Implement N Queen Problem using Backtracking
    • 11. Sort a given set of n integer elements using Quick Sort method and compute its time complexity. Run the program for
    • 2. Program for Heap Sort.
    • 3. Program for Merge Sort.
    • 4. Program for Selection Sort.
    • 5. Program for Insertion Sort.
    • 6. Program for Quick Sort.
    • 7. Knapsack Problem using Greedy Solution
    • 8. Perform Travelling Salesman Problem
    • 9. Find Minimum Spanning Tree using Kruskal’s Algorithm

22 files changed

+563
-1
lines changed

.vscode/launch.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"configurations": [
3+
{
4+
"type": "java",
5+
"name": "Launch QuickSort",
6+
"request": "launch",
7+
"mainClass": "QuickSort",
8+
"projectName": "5th-sem-labs_867ed7e"
9+
}
10+
]
11+
}

.vscode/settings.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
{
22
"files.associations": {
33
"string.h": "c"
4-
}
4+
},
5+
"java.project.sourcePaths": [
6+
"daa/11. Sort a given set of n integer elements using Quick Sort method and compute its time complexity. Run the program for"
7+
],
8+
"editor.tabCompletion": "on",
9+
"diffEditor.codeLens": true
510
}
16.5 KB
Binary file not shown.
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/* C program to solve N Queen Problem using
2+
backtracking */
3+
#define N 4
4+
#include <stdbool.h>
5+
#include <stdio.h>
6+
7+
/* A utility function to print solution */
8+
void printSolution(int board[N][N])
9+
{
10+
for (int i = 0; i < N; i++)
11+
{
12+
for (int j = 0; j < N; j++)
13+
printf(" %d ", board[i][j]);
14+
printf("\n");
15+
}
16+
}
17+
18+
/* A utility function to check if a queen can
19+
be placed on board[row][col]. Note that this
20+
function is called when "col" queens are
21+
already placed in columns from 0 to col -1.
22+
So we need to check only left side for
23+
attacking queens */
24+
bool isSafe(int board[N][N], int row, int col)
25+
{
26+
int i, j;
27+
28+
/* Check this row on left side */
29+
for (i = 0; i < col; i++)
30+
if (board[row][i])
31+
return false;
32+
33+
/* Check upper diagonal on left side */
34+
for (i = row, j = col; i >= 0 && j >= 0; i--, j--)
35+
if (board[i][j])
36+
return false;
37+
38+
/* Check lower diagonal on left side */
39+
for (i = row, j = col; j >= 0 && i < N; i++, j--)
40+
if (board[i][j])
41+
return false;
42+
43+
return true;
44+
}
45+
46+
/* A recursive utility function to solve N
47+
Queen problem */
48+
bool solveNQUtil(int board[N][N], int col)
49+
{
50+
/* base case: If all queens are placed
51+
then return true */
52+
if (col >= N)
53+
return true;
54+
55+
/* Consider this column and try placing
56+
this queen in all rows one by one */
57+
for (int i = 0; i < N; i++)
58+
{
59+
/* Check if the queen can be placed on
60+
board[i][col] */
61+
if (isSafe(board, i, col))
62+
{
63+
/* Place this queen in board[i][col] */
64+
board[i][col] = 1;
65+
66+
/* recur to place rest of the queens */
67+
if (solveNQUtil(board, col + 1))
68+
return true;
69+
70+
/* If placing queen in board[i][col]
71+
doesn't lead to a solution, then
72+
remove queen from board[i][col] */
73+
board[i][col] = 0; // BACKTRACK
74+
}
75+
}
76+
77+
/* If the queen cannot be placed in any row in
78+
this column col then return false */
79+
return false;
80+
}
81+
82+
/* This function solves the N Queen problem using
83+
Backtracking. It mainly uses solveNQUtil() to
84+
solve the problem. It returns false if queens
85+
cannot be placed, otherwise, return true and
86+
prints placement of queens in the form of 1s.
87+
Please note that there may be more than one
88+
solutions, this function prints one of the
89+
feasible solutions.*/
90+
bool solveNQ()
91+
{
92+
int board[N][N] = {{0, 0, 0, 0},
93+
{0, 0, 0, 0},
94+
{0, 0, 0, 0},
95+
{0, 0, 0, 0}};
96+
97+
if (solveNQUtil(board, 0) == false)
98+
{
99+
printf("Solution does not exist");
100+
return false;
101+
}
102+
103+
printSolution(board);
104+
return true;
105+
}
106+
107+
// driver program to test above function
108+
int main()
109+
{
110+
solveNQ();
111+
return 0;
112+
}
113+
114+
// This code is contributed by chirag singhal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/* Sort a given set of n integer elements using Quick Sort method and compute its time complexity. Run the program for
2+
varied values of n> 5000 and record the time taken to sort. Plot a graph of the time taken versus non graph sheet. The
3+
elements can be read from a file or can be generated using the random number generator. Demonstrate using Java how the
4+
divide and- conquer method works along with its time complexity analysis: worst case, average case and best case*/
5+
6+
import java.io.*;
7+
8+
class QuickSort
9+
{
10+
public static void main(String args[])throws IOException
11+
{
12+
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
13+
System.out.println("Enter the number of elements");
14+
int n=Integer.parseInt(br.readLine());
15+
int a[]=new int[n];
16+
System.out.println("Enter the elements");
17+
for(int i=0;i<n;i++)
18+
{
19+
a[i]=Integer.parseInt(br.readLine());
20+
}
21+
System.out.println("The unsorted elements are");
22+
for(int i=0;i<n;i++)
23+
{
24+
System.out.println(a[i]);
25+
}
26+
QuickSort ob=new QuickSort();
27+
ob.sort(a,0,n-1);
28+
System.out.println("The sorted elements are");
29+
for(int i=0;i<n;i++)
30+
{
31+
System.out.println(a[i]);
32+
}
33+
}
34+
void sort(int a[],int l,int r)
35+
{
36+
if(l<r)
37+
{
38+
int pi=partition(a,l,r);
39+
sort(a,l,pi-1);
40+
sort(a,pi+1,r);
41+
}
42+
}
43+
int partition(int a[],int l,int r)
44+
{
45+
int pivot=a[r];
46+
int i=l-1;
47+
for(int j=l;j<r;j++)
48+
{
49+
if(a[j]<=pivot)
50+
{
51+
i++;
52+
int temp=a[i];
53+
a[i]=a[j];
54+
a[j]=temp;
55+
}
56+
}
57+
int temp=a[i+1];
58+
a[i+1]=a[r];
59+
a[r]=temp;
60+
return i+1;
61+
}
62+
}

daa/2. Program for Heap Sort./hs

16.4 KB
Binary file not shown.

daa/2. Program for Heap Sort./hs.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// write program for the heap sort in c
2+
3+
#include<stdio.h>
4+
5+
int main()
6+
{
7+
int a[100], n, i, j, temp;
8+
9+
printf("The program for the heap sort in c \n written by Chirag Singhal \n roll number 2000330100084 \n");
10+
11+
printf( "Enter the number of elements : " );
12+
13+
14+
scanf( "%d" , &n);
15+
printf( "Enter the elements : " );
16+
17+
18+
for (i = 0; i < n; i++)
19+
scanf( "%d" , &a[i]);
20+
for (i = 0; i < n; i++)
21+
{
22+
for (j = 0; j < n - i - 1; j++)
23+
{
24+
if (a[j] > a[j + 1])
25+
{
26+
temp = a[j];
27+
a[j] = a[j + 1];
28+
a[j + 1] = temp;
29+
}
30+
}
31+
}
32+
printf( "Sorted array is : " );
33+
for (i = 0; i < n; i++)
34+
printf( "%d " , a[i]);
35+
36+
printf("\n");
37+
return 0;
38+
}

daa/3. Program for Merge Sort./ms

16.4 KB
Binary file not shown.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// write a program for the merge sort in c
2+
3+
#include<stdio.h>
4+
5+
int main()
6+
{
7+
int a[100], n, i, j, temp;
8+
9+
printf("This is the program for the merge sort in c \n written by Chirag Singhal \n roll number 2000330100084 \n");
10+
11+
printf( "Enter the number of elements : " );
12+
scanf( "%d" , &n);
13+
printf( "Enter the elements : " );
14+
for (i = 0; i < n; i++)
15+
scanf( "%d" , &a[i]);
16+
for (i = 0; i < n; i++)
17+
{
18+
for (j = 0; j < n - i - 1; j++)
19+
{
20+
if (a[j] > a[j + 1])
21+
{
22+
temp = a[j];
23+
a[j] = a[j + 1];
24+
a[j + 1] = temp;
25+
}
26+
}
27+
}
28+
printf( "Sorted array is : " );
29+
for (i = 0; i < n; i++)
30+
printf( "%d " , a[i]);
31+
32+
printf("\n");
33+
return 0;
34+
}

0 commit comments

Comments
 (0)