-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearchingASortedTable.cpp
126 lines (117 loc) · 3.76 KB
/
SearchingASortedTable.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
Searching a Sorted Table
One hundred different numbers are written on 100 cards, one number per
card.
The cards are arranged in 10 rows and 10 columns, in increasing
order in each row (left to right) and each column (top down).
All the cards are turned faced down so that you cannot see the numbers written on
them.
Can you devise an algorithm to determine whether a given number
is written on one of the cards by turning up less than 20 cards?
*/
#include <bits/stdc++.h>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include<windows.h>
#include<time.h>
using namespace std;
#define MAX_SIZE 10
void search(int mat[MAX_SIZE][MAX_SIZE], int fromRow, int toRow,
int fromCol, int toCol, int key)
{
int i = 5;
int j = 5;
if (mat[i][j] == key)
cout<<"Found "<< key << " at "<< i <<
" " << j<<endl;
else
{
if (i != toRow || j != fromCol)
search(mat, fromRow, i, j, toCol, key);
if (fromRow == toRow && fromCol + 1 == toCol)
if (mat[fromRow][toCol] == key)
cout<<"Found "<< key<< " at "<<
fromRow << " " << toCol<<endl;
if (mat[i][j] < key)
{
if (i + 1 <= toRow)
search(mat, i + 1, toRow, fromCol, toCol, key);
}
else
{
if (j - 1 >= fromCol)
search(mat, fromRow, toRow, fromCol, j - 1, key);
}
}
}
void sortByRow(int mat[MAX_SIZE][MAX_SIZE], int n)
{
for (int i = 0; i < n; i++)
sort(mat[i], mat[i] + n);
}
void transpose(int mat[MAX_SIZE][MAX_SIZE], int n)
{
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++)
swap(mat[i][j], mat[j][i]);
}
void sortMatRowAndColWise(int mat[MAX_SIZE][MAX_SIZE], int n)
{
sortByRow(mat, n);
transpose(mat, n);
sortByRow(mat, n);
transpose(mat, n);
}
void printMat(int mat[MAX_SIZE][MAX_SIZE], int n)
{
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
cout << mat[i][j] << " ";
cout << endl;
}
}
int printRandoms(int lower, int upper,
int count)
{
int i;
for (i = 0; i < count; i++) {
int num = (rand() %
(upper - lower + 1)) + lower;
return num;
}
}
int steps=0;
int main()
{
int mat[MAX_SIZE][MAX_SIZE];
int r1,c1,key;
clock_t t1,t2;
for (int i=0; i<10; i++)
{
for (int j=0; j<10; j++)
{
mat[i][j]=rand()%(10000) + 1;
}
}
int n = 10;
cout<<"\t\t\t\tSearching a Sorted Table\n One hundred different numbers are written on 100 cards, one number per card. \n The cards are arranged in 10 rows and 10 columns, in increasing order in each row (left to right) and each column (top down).\nAll the cards are turned faced down so that you cannot see the numbers written on them.\nCan you devise an algorithm to determine whether a given number is written on one of the cards by turning up less than 20 cards?\n";
cout << "\n\nOriginal Table:\n";
printMat(mat, n);
t1=clock();
sortMatRowAndColWise(mat, n);
Sleep(2000);
cout<<endl;
cout << "\nTable After Sorting:\n";
printMat(mat, n);
r1=printRandoms(0, 9, 1);
c1=printRandoms(0, 9, 1);
key=mat[r1][c1];
cout<<"\nThe card to be searched at random is located at row "<<r1+1<<" and column "<<c1+1<<"."<<endl<<"Number to be searched: "<<key<<endl;
search(mat, 0, MAX_SIZE - 1, 0, MAX_SIZE - 1, key);
t2=clock()-t1;
cout<<"\nTime taken for computation: "<<(float)t2/CLOCKS_PER_SEC<<" seconds."<<endl;
cout<<"\nNumber of steps taken to find the card: "<<steps;
cout<<"\n------------------------------";
return 0;
}