Skip to content

Commit a310a6a

Browse files
committed
update ch2
1 parent 52d3fdd commit a310a6a

File tree

21 files changed

+207
-47
lines changed

21 files changed

+207
-47
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@ Algorithms, 4th edition textbook code (using c++)
33

44
**Note:**
55

6-
**(the code can run, but may not well --- I will modify these in the next cycle)**
7-
86
1. based on STL Library
97
2. using C++14
108
3. **Not support** drawing
119
4. **Bug: ** ① const object (many member function forget to add const)
1210
4. Welcome to point out the error, and pull better code
1311

14-
## How to run demo
12+
## Tutorial: run demo code
1513

1614
### terminal
1715

ch2/10_Quick/main.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,16 @@
44

55
using namespace std;
66

7+
/**
8+
* Reads in a sequence of strings from standard input; quicksorts them;
9+
* and prints them to standard output in ascending order.
10+
* Shuffles the array and then prints the strings again to
11+
* standard output, but this time, using the select method.
12+
*
13+
* @param args the command-line arguments
14+
*/
715
int main() {
8-
ifstream file("/home/ace/AceDev/C++/algorithm/ch2/data/tiny.txt");
16+
ifstream file("./data/tiny.txt");
917
string tmp;
1018
vector<string> vec;
1119
while (file >> tmp) {

ch2/11_Quick3way/main.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,14 @@
44

55
using namespace std;
66

7+
/**
8+
* Reads in a sequence of strings from standard input; 3-way
9+
* quicksorts them; and prints them to standard output in ascending order.
10+
*
11+
* @param args the command-line arguments
12+
*/
713
int main() {
8-
ifstream file("/home/ace/AceDev/C++/algorithm/ch2/data/tiny.txt");
14+
ifstream file("./data/tiny.txt");
915
string tmp;
1016
vector<string> vec;
1117
while (file >> tmp) {

ch2/12_QuickX/main.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@
44

55
using namespace std;
66

7+
/**
8+
* Reads in a sequence of strings from standard input; quicksorts them
9+
* (using an optimized version of 2-way quicksort);
10+
* and prints them to standard output in ascending order.
11+
*
12+
* @param args the command-line arguments
13+
*/
714
int main() {
8-
ifstream file("/home/ace/AceDev/C++/algorithm/ch2/data/tiny.txt");
15+
ifstream file("./data/tiny.txt");
916
string tmp;
1017
vector<string> vec;
1118
while (file >> tmp) {

ch2/13_QuickBentleyMcIlroy/main.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@
44

55
using namespace std;
66

7+
/**
8+
* Reads in a sequence of strings from standard input; quicksorts them
9+
* (using an optimized version of quicksort);
10+
* and prints them to standard output in ascending order.
11+
*
12+
* @param args the command-line arguments
13+
*/
714
int main() {
8-
ifstream file("/home/ace/AceDev/C++/algorithm/ch2/data/words3.txt");
15+
ifstream file("./data/words3.txt");
916
string tmp;
1017
vector<string> vec;
1118
while (file >> tmp) {

ch2/14_TopM/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ int main() {
2323
int m = 5;
2424
auto f = [](Transaction &a1, Transaction &a2) { return a1.getamount() > a2.getamount(); };
2525
MinPQ<Transaction> pq(f);
26-
fstream file("/home/ace/AceDev/C++/algorithm/ch2/data/tinyBatch.txt");
26+
fstream file("./data/tinyBatch.txt");
2727
string tmp;
2828
while (getline(file, tmp)) {
2929
// Create an entry from the next line and put on the PQ.

ch2/15_MaxPQ/main.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@
44

55
using namespace std;
66

7+
/**
8+
* Unit tests the {@code MaxPQ} data type.
9+
*
10+
* @param args the command-line arguments
11+
*/
712
int main() {
813
MaxPQ<string> pq;
9-
fstream file("/home/ace/AceDev/C++/algorithm/ch2/data/tinyPQ.txt");
14+
fstream file("./data/tinyPQ.txt");
1015
string tmp;
1116
while (file >> tmp) {
1217
if (tmp != "-") pq.insert(tmp);

ch2/16_MinPQ/main.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@
44

55
using namespace std;
66

7+
/**
8+
* Unit tests the {@code MinPQ} data type.
9+
*
10+
* @param args the command-line arguments
11+
*/
712
int main() {
813
MinPQ<string> pq;
9-
fstream file("/home/ace/AceDev/C++/algorithm/ch2/data/tinyPQ.txt");
14+
fstream file("./data/tinyPQ.txt");
1015
string tmp;
1116
while (file >> tmp) {
1217
if (tmp != "-") pq.insert(tmp);

ch2/18_IndexMaxPQ/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ mt19937 g(rd());
99
uniform_real_distribution<double> dis(0, 1);
1010

1111
/**
12-
* Unit tests the {@code IndexMinPQ} data type.
12+
* Unit tests the {@code IndexMaxPQ} data type.
1313
*
1414
* @param args the command-line arguments
1515
*/

ch2/19_Multiway/main.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ using namespace std;
1414
* @param args the command-line arguments
1515
*/
1616
int main() {
17-
vector<string> files{"/home/ace/AceDev/C++/algorithm/ch2/data/m1.txt",
18-
"/home/ace/AceDev/C++/algorithm/ch2/data/m2.txt",
19-
"/home/ace/AceDev/C++/algorithm/ch2/data/m1.txt"};
17+
vector<string> files{"./data/m1.txt",
18+
"./data/m2.txt",
19+
"./data/m3.txt"};
2020
vector<fstream> streams;
2121
for (auto f: files)
2222
streams.push_back(fstream(f));

ch2/1_Insertion/main.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@
44

55
using namespace std;
66

7+
/**
8+
* Reads in a sequence of strings from standard input; insertion sorts them;
9+
* and prints them to standard output in ascending order.
10+
*
11+
* @param args the command-line arguments
12+
*/
713
int main() {
8-
ifstream file("/home/ace/AceDev/C++/algorithm/ch2/data/tiny.txt");
14+
ifstream file("./data/tiny.txt");
915
string tmp;
1016
vector<string> vec;
1117
while (file >> tmp) {
1218
vec.push_back(tmp);
1319
}
14-
Insertion::sort(vec);
20+
Insertion::sort(vec);
1521
// Insertion::sort(vec, [](string a, string b) { return a > b; });
1622
Insertion::show(vec);
1723
}

ch2/20_Heap/main.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@
66
using namespace std;
77

88
/**
9-
* Reads sorted text files specified as command-line arguments;
10-
* merges them together into a sorted output; and writes
11-
* the results to standard output.
12-
* Note: this client does not check that the input files are sorted.
9+
* Reads in a sequence of strings from standard input; heapsorts them;
10+
* and prints them to standard output in ascending order.
1311
*
1412
* @param args the command-line arguments
1513
*/
1614
int main() {
17-
fstream file("/home/ace/AceDev/C++/algorithm/ch2/data/tiny.txt");
15+
fstream file("./data/tiny.txt");
1816
string tmp;
1917
vector<string> vec;
2018
while (file >> tmp)

ch2/2_InsertionX/main.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,14 @@
44

55
using namespace std;
66

7+
/**
8+
* Reads in a sequence of strings from standard input; insertion sorts them;
9+
* and prints them to standard output in ascending order.
10+
*
11+
* @param args the command-line arguments
12+
*/
713
int main() {
8-
ifstream file("/home/ace/AceDev/C++/algorithm/ch2/data/tiny.txt");
14+
ifstream file("./data/tiny.txt");
915
string tmp;
1016
vector<string> vec;
1117
while (file >> tmp) {

ch2/3_BinaryInsertion/main.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,14 @@
44

55
using namespace std;
66

7+
/**
8+
* Reads in a sequence of strings from standard input; insertion sorts them;
9+
* and prints them to standard output in ascending order.
10+
*
11+
* @param args the command-line arguments
12+
*/
713
int main() {
8-
ifstream file("/home/ace/AceDev/C++/algorithm/ch2/data/tiny.txt");
14+
ifstream file("./data/tiny.txt");
915
string tmp;
1016
vector<string> vec;
1117
while (file >> tmp) {

ch2/4_Selection/main.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,14 @@
44

55
using namespace std;
66

7+
/**
8+
* Reads in a sequence of strings from standard input; selection sorts them;
9+
* and prints them to standard output in ascending order.
10+
*
11+
* @param args the command-line arguments
12+
*/
713
int main() {
8-
ifstream file("/home/ace/AceDev/C++/algorithm/ch2/data/tiny.txt");
14+
ifstream file("./data/tiny.txt");
915
string tmp;
1016
vector<string> vec;
1117
while (file >> tmp) {

ch2/5_Shell/main.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,14 @@
44

55
using namespace std;
66

7+
/**
8+
* Reads in a sequence of strings from standard input; Shellsorts them;
9+
* and prints them to standard output in ascending order.
10+
*
11+
* @param args the command-line arguments
12+
*/
713
int main() {
8-
ifstream file("/home/ace/AceDev/C++/algorithm/ch2/data/tiny.txt");
14+
ifstream file("./data/tiny.txt");
915
string tmp;
1016
vector<string> vec;
1117
while (file >> tmp) {

ch2/6_Merge/main.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,14 @@
44

55
using namespace std;
66

7+
/**
8+
* Reads in a sequence of strings from standard input; mergesorts them;
9+
* and prints them to standard output in ascending order.
10+
*
11+
* @param args the command-line arguments
12+
*/
713
int main() {
8-
ifstream file("/home/ace/AceDev/C++/algorithm/ch2/data/tiny.txt");
14+
ifstream file("./data/tiny.txt");
915
string tmp;
1016
vector<string> vec;
1117
while (file >> tmp) {

ch2/7_MergeBU/main.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,14 @@
44

55
using namespace std;
66

7+
/**
8+
* Reads in a sequence of strings from standard input; bottom-up
9+
* mergesorts them; and prints them to standard output in ascending order.
10+
*
11+
* @param args the command-line arguments
12+
*/
713
int main() {
8-
ifstream file("/home/ace/AceDev/C++/algorithm/ch2/data/words3.txt");
14+
ifstream file("./data/words3.txt");
915
string tmp;
1016
vector<string> vec;
1117
while (file >> tmp) {

ch2/8_MergeX/main.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@
44

55
using namespace std;
66

7+
/**
8+
* Reads in a sequence of strings from standard input; mergesorts them
9+
* (using an optimized version of mergesort);
10+
* and prints them to standard output in ascending order.
11+
*
12+
* @param args the command-line arguments
13+
*/
714
int main() {
8-
ifstream file("/home/ace/AceDev/C++/algorithm/ch2/data/words3.txt");
15+
ifstream file("./data/words3.txt");
916
string tmp;
1017
vector<string> vec;
1118
while (file >> tmp) {

ch2/9_Inversions/main.cpp

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
// TODO: un-finish
1+
#include <iostream>
2+
#include <fstream>
3+
#include "../head/Inversions.h"
24

3-
//#include <iostream>
4-
//#include <fstream>
5-
//#include "../head/MergeX.h"
6-
//
7-
//using namespace std;
8-
//
9-
//int main() {
10-
// ifstream file("/home/ace/AceDev/C++/algorithm/ch2/data/words3.txt");
11-
// string tmp;
12-
// vector<string> vec;
13-
// while (file >> tmp) {
14-
// vec.push_back(tmp);
15-
// }
16-
// MergeX::sort(vec);
17-
// MergeX::show(vec);
18-
//}
5+
using namespace std;
6+
7+
/**
8+
* Reads a sequence of integers from standard input and
9+
* prints the number of inversions to standard output.
10+
*
11+
* @param args the command-line arguments
12+
*/
13+
int main() {
14+
vector<int> a{2, 4, 1, 6, 3};
15+
cout << Inversions::count(a) << endl;
16+
}

0 commit comments

Comments
 (0)