Skip to content

Commit 8da9714

Browse files
Merge branch 'master' into master
2 parents 29f0437 + 085325d commit 8da9714

File tree

13 files changed

+604
-21
lines changed

13 files changed

+604
-21
lines changed

Data Structures/StackUsingArray.h

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
template <typename T>
2+
class Stack {
3+
T *data;
4+
int nextIndex;
5+
int totalSize;
6+
7+
public :
8+
9+
Stack(int size) {
10+
data = new T[size];
11+
nextIndex = 0;
12+
totalSize = size;
13+
}
14+
15+
~Stack() {
16+
delete [] data;
17+
}
18+
19+
int getSize() {
20+
return nextIndex;
21+
}
22+
23+
bool isEmpty() {
24+
if(nextIndex == 0)
25+
return true;
26+
else
27+
return false;
28+
}
29+
30+
T top() {
31+
if(isEmpty()) {
32+
cout << "Stack empty ! " << endl;
33+
return -1;
34+
}
35+
return data[nextIndex - 1];
36+
}
37+
38+
void push(T element) {
39+
if(totalSize == nextIndex) {
40+
T *temp = new T[2 * totalSize];
41+
for(int i = 0; i < totalSize; i++) {
42+
temp[i] = data[i];
43+
}
44+
totalSize *= 2;
45+
delete [] data;
46+
data = temp;
47+
}
48+
data[nextIndex] = element;
49+
nextIndex++;
50+
}
51+
52+
T pop() {
53+
if(isEmpty()) {
54+
cout << "Stack empty ! " << endl;
55+
return -1;
56+
}
57+
nextIndex--;
58+
return data[nextIndex];
59+
}
60+
61+
};

contributors.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
6. [Ujjval Patel](https://github.com/Ujjval-Patel)
99
7. [Shaguna Awasthi](https://github.com/Shagunaawasthi)
1010
8. [Joy Banerjee](https://github.com/joybanerjee08)
11-
9. [Kartikey Tripathi](https://github.com/kartikeytripathi)
11+
9. [Kartikey Tripathi](https://github.com/kartikeytripathi)

knight-tour.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include<bits/stdc++.h>
2+
# define n 8
3+
using namespace std;
4+
bool issafe(int x,int y,int sol[n][n])
5+
{
6+
return (x<n && x>=0 && y<n && y>=0 && sol[x][y]==-1);
7+
8+
}
9+
bool solve(int x,int y, int mov, int sol[n][n], int xmov[n], int ymov[n])
10+
{
11+
int k,xnext,ynext;
12+
13+
if(mov == n*n)
14+
return true;
15+
16+
for(k=0;k<8;k++)
17+
{
18+
xnext=x+xmov[k];
19+
ynext=y+ymov[k];
20+
21+
if(issafe(xnext,ynext,sol))
22+
{
23+
sol[xnext][ynext]=mov;
24+
25+
if(solve(xnext,ynext,mov+1,sol,xmov,ymov)==true)
26+
return true;
27+
else
28+
sol[xnext][ynext]=-1;
29+
}
30+
}
31+
return false;
32+
}
33+
int main()
34+
{
35+
//initialize();
36+
37+
int sol[n][n];
38+
int i,j;
39+
for(i=0;i<n;i++)
40+
for(j=0;j<n;j++)
41+
sol[i][j]=-1;
42+
43+
int xmov[8] = { 2, 1, -1, -2, -2, -1, 1, 2 };
44+
int ymov[8] = { 1, 2, 2, 1, -1, -2, -2, -1 };
45+
sol[0][0]=0;
46+
47+
bool flag=solve(0,0,1,sol,xmov,ymov);
48+
if(flag==false)
49+
cout<<"solution doesnot exist \n";
50+
else
51+
{
52+
for(i=0;i<n;i++)
53+
{
54+
for(j=0;j<n;j++)
55+
cout<<sol[i][j]<<" ";
56+
cout<<"\n";
57+
}
58+
}
59+
}

n-queen.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include<bits/stdc++.h>
2+
#define n 4
3+
using namespace std;
4+
bool issafe(int board[n][n], int row, int col)
5+
{
6+
int i,j;
7+
for(i=0;i<n;i++)
8+
if(board[row][i])
9+
return false;
10+
for(i=row,j=col;i>=0 && j>=0;i--,j--)
11+
if(board[i][j])
12+
return false;
13+
for(i=row,j=col;i<n && j>=0; i++,j--)
14+
if(board[i][j])
15+
return false;
16+
17+
return true;
18+
}
19+
bool solve(int board[n][n], int col)
20+
{
21+
if(col>=n)
22+
return true;
23+
int i;
24+
for(i=0;i<n;i++)
25+
{
26+
if(issafe(board, i ,col))
27+
{
28+
board[i][col]=1;
29+
30+
if(solve(board, col+1))
31+
return true;
32+
33+
board[i][col]=0;
34+
}
35+
}
36+
return false;
37+
}
38+
int main()
39+
{
40+
int board[n][n]={{0,0,0,0},
41+
{0,0,0,0},
42+
{0,0,0,0},
43+
{0,0,0,0}};
44+
bool flag;
45+
int i,j;
46+
flag=solve(board,0);
47+
if(flag==false)
48+
cout<<"solution doesnot exist \n";
49+
else
50+
{
51+
for(i=0;i<n;i++)
52+
{
53+
for(j=0;j<n;j++)
54+
cout<<board[i][j]<<" ";
55+
cout<<"\n";
56+
}
57+
}
58+
}

rat_in_a_maze.cpp

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#include<bits/stdc++.h>
2+
#define n 4
3+
using namespace std;
4+
5+
int sol[n][n]= {{0,0,0,0},
6+
{0,0,0,0},
7+
{0,0,0,0},
8+
{0,0,0,0,}};
9+
10+
void print(int sol[n][n]);
11+
12+
int issafe(int maze[n][n], int row, int col)
13+
{
14+
if(row>=0 && row<n && col>=0 && col<n && maze[row][col]==1)
15+
return 1;
16+
return 0;
17+
}
18+
19+
int solvemaze(int maze[n][n], int row, int col, int sol[n][n])
20+
{
21+
if(row==n-1 && col==n-1)
22+
{
23+
sol[row][col]=1;
24+
25+
return 1;
26+
}
27+
28+
if(issafe(maze,row,col) == 1)
29+
{
30+
31+
sol[row][col]=1;
32+
print(sol);
33+
34+
if(solvemaze(maze, row, col+1,sol) == 1)
35+
{
36+
return 1;
37+
}
38+
if(solvemaze(maze, row+1, col,sol) == 1)
39+
{
40+
return 1;
41+
}
42+
else
43+
{
44+
cout<<"Backtracking step \n";
45+
print(sol);
46+
sol[row][col]=0;
47+
return 0;
48+
}
49+
}
50+
return 0;
51+
}
52+
53+
54+
void print(int sol[n][n])
55+
{
56+
int i,j;
57+
58+
for(i=0;i<n;i++)
59+
{
60+
cout<<"|";
61+
for(j=0;j<n;j++)
62+
{
63+
cout<<" "<<sol[i][j]<<" |";
64+
}
65+
cout<<"\n";
66+
}
67+
cout<<"\n\n";
68+
}
69+
int main()
70+
{
71+
72+
/*int maze[n][n]={{1,1,1,0,0,1,1,1},
73+
{0,0,1,1,0,0,0,0},
74+
{0,0,1,0,0,0,0,0},
75+
{0,1,1,1,0,0,0,0},
76+
{0,0,0,1,0,0,0,0},
77+
{0,0,0,1,0,1,1,1},
78+
{0,0,0,1,0,1,0,1},
79+
{0,0,0,1,1,1,0,1}};
80+
81+
82+
int sol[n][n] ={{0,0,0,0,0,0,0,0},
83+
{0,0,0,0,0,0,0,0},
84+
{0,0,0,0,0,0,0,0},
85+
{0,0,0,0,0,0,0,0},
86+
{0,0,0,0,0,0,0,0},
87+
{0,0,0,0,0,0,0,0},
88+
{0,0,0,0,0,0,0,0},
89+
{0,0,0,0,0,0,0,0}};*/
90+
91+
int maze[n][n]={{1,1,1,0},
92+
{0,1,0,1},
93+
{0,1,1,0},
94+
{0,1,1,1}};
95+
96+
97+
cout<<"Maze"<<"\n";
98+
print(maze);
99+
100+
solvemaze(maze,0,0,sol);
101+
102+
103+
print(sol);
104+
105+
}

searching/Binarysearch.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import java.util.*;
2+
public class Main {
3+
static Scanner scn = new Scanner(System.in);
4+
5+
public static void main(String[] args) {
6+
// TODO Auto-generated method stub
7+
int[] arr = takeInput();
8+
9+
int item = scn.nextInt();
10+
11+
System.out.println(binarySearch(arr,item));
12+
}
13+
14+
public static int[] takeInput() {
15+
// System.out.println("enter size of array");
16+
int n = scn.nextInt();
17+
int[] arr = new int[n];
18+
// System.out.println("enetr values in array");
19+
for (int i = 0; i < n; i++) {
20+
arr[i] = scn.nextInt();
21+
}
22+
return arr;
23+
}
24+
public static int binarySearch(int[] arr, int item) {
25+
int lo = 0;
26+
int hi = arr.length - 1;
27+
28+
while (lo <= hi) {
29+
int mid = (lo + hi) / 2;
30+
31+
if (item > arr[mid]) {
32+
lo = mid + 1;
33+
} else if (item < arr[mid]) {
34+
hi = mid - 1;
35+
} else {
36+
return mid;
37+
}
38+
}
39+
40+
return -1;
41+
}
42+
43+
}

sorting/Insertionsort.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import java.util.*;
2+
public class Main {
3+
static Scanner scn = new Scanner(System.in);
4+
5+
public static void main(String[] args) {
6+
// TODO Auto-generated method stub
7+
int[] arr = takeInput();
8+
9+
insertionsort(arr);
10+
display(arr);
11+
}
12+
public static int[] takeInput() {
13+
// System.out.println("enter size of array");
14+
int n = scn.nextInt();
15+
int[] arr = new int[n];
16+
// System.out.println("enetr values in array");
17+
for (int i = 0; i < n; i++) {
18+
arr[i] = scn.nextInt();
19+
}
20+
return arr;
21+
}
22+
23+
public static void display(int[] arr)
24+
25+
{
26+
// System.out.println("array is:");
27+
28+
for (int val : arr) {
29+
System.out.println(val);
30+
}
31+
}
32+
33+
public static void insertionsort(int[] arr) {
34+
int n = arr.length;
35+
for (int i = 1; i < n; i++) {
36+
int temp = arr[i];
37+
int j = i - 1;
38+
while (j >= 0 && arr[j] > temp) // shifting
39+
{
40+
arr[j + 1] = arr[j];
41+
j--;
42+
}
43+
44+
arr[j + 1] = temp;
45+
}
46+
}
47+
48+
}
49+

0 commit comments

Comments
 (0)