Skip to content

Commit 2c19d0f

Browse files
committed
first commit
1 parent 34b7700 commit 2c19d0f

File tree

18 files changed

+274
-0
lines changed

18 files changed

+274
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# algs4cplusplus
22
Algorithms, 4th edition textbook code (using c++)
3+

ch1/1_BinarySearch/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(1_BinarySearch)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp)
7+
add_executable(1_BinarySearch ${SOURCE_FILES})

ch1/1_BinarySearch/main.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <fstream>
4+
#include <algorithm>
5+
6+
using namespace std;
7+
8+
/**
9+
* Returns the index of the specified key in the specified array.
10+
*
11+
* @param a the array of integers, must be sorted in ascending order
12+
* @param key the search key
13+
* @return index of key in array {@code a} if present; {@code -1} otherwise
14+
*/
15+
int indexOf(vector<int> &a, int key) {
16+
int lo = 0, hi = a.size() - 1;
17+
while (lo <= hi) {
18+
// Key is in a[lo..hi] or not present.
19+
int mid = lo + (hi - lo) / 2;
20+
if (key < a[mid]) hi = mid - 1;
21+
else if (key > a[mid]) lo = mid + 1;
22+
else return mid;
23+
}
24+
return -1;
25+
}
26+
27+
int main() {
28+
// TODO: change to relative path
29+
ifstream input("/home/ace/AceDev/C++/algorithm/ch1/1_BinarySearch/tinyW.txt");
30+
vector<int> whitelist;
31+
int t;
32+
while (input >> t) {
33+
whitelist.push_back(t);
34+
}
35+
sort(whitelist.begin(), whitelist.end());
36+
ifstream check("/home/ace/AceDev/C++/algorithm/ch1/1_BinarySearch/tinyT.txt");
37+
while (check >> t) {
38+
int idx = indexOf(whitelist, t);
39+
if (idx == -1)
40+
cout << t << endl;
41+
}
42+
}

ch1/1_BinarySearch/tinyT.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
23
2+
50
3+
10
4+
99
5+
18
6+
23
7+
98
8+
84
9+
11
10+
10
11+
48
12+
77
13+
13
14+
54
15+
98
16+
77
17+
77
18+
68

ch1/1_BinarySearch/tinyW.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
84
2+
48
3+
68
4+
10
5+
18
6+
98
7+
12
8+
23
9+
54
10+
57
11+
33
12+
16
13+
77
14+
11
15+
29

ch1/2_RandomSeq/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(2_RandomSeq)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp)
7+
add_executable(2_RandomSeq ${SOURCE_FILES})

ch1/2_RandomSeq/main.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <iomanip>
4+
#include <algorithm>
5+
6+
using namespace std;
7+
8+
const int seed = 100;
9+
10+
/**
11+
* Reads in two command-line arguments lo and hi and prints n uniformly
12+
* random real numbers in [lo, hi) to standard output.
13+
*
14+
* @param args the command-line arguments
15+
*/
16+
int main() {
17+
int n = 10;
18+
double lo = 0.0, hi = 1.0;
19+
// uniform[0.0, 1.0)
20+
default_random_engine e(seed);
21+
uniform_real_distribution<double> dis(0.0, 1.0);
22+
cout << fixed;
23+
for (int i = 0; i < n; ++i)
24+
cout << setprecision(4) << dis(e) << " ";
25+
cout << endl;
26+
// uniform[lo, high)
27+
lo = 2.5, hi = 3.0;
28+
uniform_real_distribution<double> dis2(lo, hi);
29+
for (int i = 0; i < n; ++i)
30+
cout << setprecision(4) << dis2(e) << " ";
31+
cout << endl;
32+
}

ch1/3_Average/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(3_Average)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp)
7+
add_executable(3_Average ${SOURCE_FILES})

ch1/3_Average/main.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <iomanip>
4+
#include <algorithm>
5+
6+
using namespace std;
7+
8+
const int seed = 100;
9+
10+
/**
11+
* Reads in a sequence of real numbers from standard input and prints
12+
* out their average to standard output.
13+
*
14+
* @param args the command-line arguments
15+
*/
16+
int main() {
17+
int count = 0;
18+
double sum = 0.0;
19+
vector<double> arr{10.0, 5.0, 6.0, 3.0, 7.0, 32.0};
20+
for (auto a: arr) {
21+
sum += a;
22+
count++;
23+
}
24+
double avg = sum / count;
25+
cout << "Average is " << avg << endl;
26+
}

ch1/4_Cat/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(4_Cat)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp)
7+
add_executable(4_Cat ${SOURCE_FILES})

ch1/4_Cat/in1.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is

ch1/4_Cat/in2.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
a tiny
2+
test.

ch1/4_Cat/main.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <fstream>
4+
#include <algorithm>
5+
6+
using namespace std;
7+
8+
const int seed = 100;
9+
10+
/**
11+
* Reads in a sequence of text files specified as the first command-line
12+
* arguments, concatenates them, and writes the results to the file
13+
* specified as the last command-line argument.
14+
*
15+
* @param args the command-line arguments
16+
*/
17+
int main() {
18+
// TODO: change to relative path
19+
ofstream out("/home/ace/AceDev/C++/algorithm/ch1/4_Cat/out.txt");
20+
vector<string> files{"/home/ace/AceDev/C++/algorithm/ch1/4_Cat/in1.txt",
21+
"/home/ace/AceDev/C++/algorithm/ch1/4_Cat/in2.txt"};
22+
string tmp;
23+
for (int i = 0; i < files.size(); ++i) {
24+
ifstream in(files[i]);
25+
while (getline(in, tmp))
26+
out << tmp << endl;
27+
in.close();
28+
}
29+
out.close();
30+
}

ch1/4_Cat/out.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This is
2+
a tiny
3+
test.

ch1/5_Knuth/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(5_Knuth)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp)
7+
add_executable(5_Knuth ${SOURCE_FILES})

ch1/5_Knuth/cards.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2C 3C 4C 5C 6C 7C 8C 9C 10C JC QC KC AC
2+
2D 3D 4D 5D 6D 7D 8D 9D 10D JD QD KD AD
3+
2H 3H 4H 5H 6H 7H 8H 9H 10H JH QH KH AH
4+
2S 3S 4S 5S 6S 7S 8S 9S 10S JS QS KS AS

ch1/5_Knuth/cardsUnicode.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2♣ 3♣ 4♣ 5♣ 6♣ 7♣ 8♣ 9♣ 10♣ J♣ Q♣ K♣ A♣
2+
2♦ 3♦ 4♦ 5♦ 6♦ 7♦ 8♦ 9♦ 10♦ J♦ Q♦ K♦ A♦
3+
2♥ 3♥ 4♥ 5♥ 6♥ 7♥ 8♥ 9♥ 10♥ J♥ Q♥ K♥ A♥
4+
2♠ 3♠ 4♠ 5♠ 6♠ 7♠ 8♠ 9♠ 10♠ J♠ Q♠ K♠ A♠

ch1/5_Knuth/main.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <fstream>
4+
#include <iomanip>
5+
#include <algorithm>
6+
7+
using namespace std;
8+
9+
std::random_device rd;
10+
std::mt19937 gen(rd());
11+
12+
/**
13+
* Rearranges an array of objects in uniformly random order
14+
* (under the assumption that {@code Math.random()} generates independent
15+
* and uniformly distributed numbers between 0 and 1).
16+
* @param a the array to be shuffled
17+
*/
18+
template<typename T>
19+
void shuffle_(vector<T> &a) {
20+
int n = a.size();
21+
for (int i = 0; i < n; i++) {
22+
// choose index uniformly in [0, i]
23+
uniform_int_distribution<> dis(0, i);
24+
int r = dis(gen);
25+
swap(a[r], a[i]);
26+
}
27+
}
28+
29+
template<typename T>
30+
void shuffleAlternate_(vector<T> &a) {
31+
int n = a.size();
32+
for (int i = 0; i < n; i++) {
33+
// choose index uniformly in [i, n-1]
34+
uniform_int_distribution<> dis(0, n - i);
35+
int r = i + dis(gen);
36+
swap(a[r], a[i]);
37+
}
38+
}
39+
40+
/**
41+
* Reads in a sequence of text files specified as the first command-line
42+
* arguments, concatenates them, and writes the results to the file
43+
* specified as the last command-line argument.
44+
*
45+
* @param args the command-line arguments
46+
*/
47+
int main() {
48+
// TODO: change to relative path
49+
ifstream file("/home/ace/AceDev/C++/algorithm/ch1/5_Knuth/cards.txt");
50+
string tmp;
51+
vector<string> a;
52+
while (file >> tmp) {
53+
a.push_back(tmp);
54+
}
55+
shuffle_(a);
56+
for (int i = 0; i < a.size(); ++i) {
57+
cout << setw(6)<<left<<a[i];
58+
if ((i + 1) % 13 == 0)
59+
cout << endl;
60+
}
61+
}

0 commit comments

Comments
 (0)