Skip to content

Commit 1d376a2

Browse files
committed
finish ch2
1 parent 9eb22dd commit 1d376a2

31 files changed

+1948
-13
lines changed

README.md

+12-10
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,16 @@ Algorithms, 4th edition textbook code (using c++)
3535

3636
## ch2. Sorting
3737

38-
| REF | PROGRAM | DESCRIPTION / JAVADOC | REF | PROGRAM | DESCRIPTION / JAVADOC |
39-
| :----------------------------------------------------------: | :----------------------------------------------------------: | :-------------------------------: | :----------------------------------------------------------: | :----------------------------------------------------------: | :------------------------: |
40-
| [2.1](https://algs4.cs.princeton.edu/21elementary/index.php#2.1) | [Insertion.h](ch2/head/Insertion.h) | insertion sort | [-](https://algs4.cs.princeton.edu/21elementary/index.php#-) | [InsertionX.h](ch2/head/InsertionX.h) | insertion sort (optimized) |
41-
| [-](https://algs4.cs.princeton.edu/21elementary/index.php#-) | [BinaryInsertion.h](ch2/head/InsertionX.h) | binary insertion sort | [2.2](https://algs4.cs.princeton.edu/21elementary/index.php#2.2) | [Selection.h](ch2/head/InsertionX.h) | selection sort |
42-
| [2.3](https://algs4.cs.princeton.edu/21elementary/index.php#2.3) | [Shell.java](https://algs4.cs.princeton.edu/21elementary/Shell.java.html) | shellsort | [2.4](https://algs4.cs.princeton.edu/22mergesort/index.php#2.4) | [Merge.java](https://algs4.cs.princeton.edu/22mergesort/Merge.java.html) | top-down mergesort |
43-
| [-](https://algs4.cs.princeton.edu/22mergesort/index.php#-) | [MergeBU.h](ch2/head/MergeBU.h) | bottom-up mergesort | [-](https://algs4.cs.princeton.edu/22mergesort/index.php#-) | [MergeX.h](ch2/head/MergeX.h) | optimized mergesort |
44-
| [-](https://algs4.cs.princeton.edu/22mergesort/index.php#-) | [Inversions.java](https://algs4.cs.princeton.edu/22mergesort/Inversions.java.html) | number of inversions | [2.5](https://algs4.cs.princeton.edu/23quicksort/index.php#2.5) | [Quick.h](ch2/head/Quick.h) | quicksort |
45-
| [-](https://algs4.cs.princeton.edu/23quicksort/index.php#-) | [Quick3way.h](ch2/head/Quick3way.h) | quicksort with 3-way partitioning | [-](https://algs4.cs.princeton.edu/23quicksort/index.php#-) | [QuickX.h](ch2/head/QuickX.h) | optimized 2-way quicksort |
46-
| [-](https://algs4.cs.princeton.edu/23quicksort/index.php#-) | [QuickBentleyMcIlroy.h](ch2/head/QuickBentleyMcIlroy.h) | optimized 3-way quicksort | | | |
47-
| | | | | | |
38+
| REF | PROGRAM | DESCRIPTION / JAVADOC | REF | PROGRAM | DESCRIPTION / JAVADOC |
39+
| :----------------------------------------------------------: | :----------------------------------------------------------: | :-------------------------------: | :----------------------------------------------------------: | :----------------------------------------------------------: | :---------------------------: |
40+
| [2.1](https://algs4.cs.princeton.edu/21elementary/index.php#2.1) | [Insertion.h](ch2/head/Insertion.h) | insertion sort | [-](https://algs4.cs.princeton.edu/21elementary/index.php#-) | [InsertionX.h](ch2/head/InsertionX.h) | insertion sort (optimized) |
41+
| [-](https://algs4.cs.princeton.edu/21elementary/index.php#-) | [BinaryInsertion.h](ch2/head/InsertionX.h) | binary insertion sort | [2.2](https://algs4.cs.princeton.edu/21elementary/index.php#2.2) | [Selection.h](ch2/head/InsertionX.h) | selection sort |
42+
| [2.3](https://algs4.cs.princeton.edu/21elementary/index.php#2.3) | [Shell.java](https://algs4.cs.princeton.edu/21elementary/Shell.java.html) | shellsort | [2.4](https://algs4.cs.princeton.edu/22mergesort/index.php#2.4) | [Merge.java](https://algs4.cs.princeton.edu/22mergesort/Merge.java.html) | top-down mergesort |
43+
| [-](https://algs4.cs.princeton.edu/22mergesort/index.php#-) | [MergeBU.h](ch2/head/MergeBU.h) | bottom-up mergesort | [-](https://algs4.cs.princeton.edu/22mergesort/index.php#-) | [MergeX.h](ch2/head/MergeX.h) | optimized mergesort |
44+
| [-](https://algs4.cs.princeton.edu/22mergesort/index.php#-) | [Inversions.java](https://algs4.cs.princeton.edu/22mergesort/Inversions.java.html) | number of inversions | [2.5](https://algs4.cs.princeton.edu/23quicksort/index.php#2.5) | [Quick.h](ch2/head/Quick.h) | quicksort |
45+
| [-](https://algs4.cs.princeton.edu/23quicksort/index.php#-) | [Quick3way.h](ch2/head/Quick3way.h) | quicksort with 3-way partitioning | [-](https://algs4.cs.princeton.edu/23quicksort/index.php#-) | [QuickX.h](ch2/head/QuickX.h) | optimized 2-way quicksort |
46+
| [-](https://algs4.cs.princeton.edu/23quicksort/index.php#-) | [QuickBentleyMcIlroy.h](ch2/head/QuickBentleyMcIlroy.h) | optimized 3-way quicksort | [-](https://algs4.cs.princeton.edu/24pq/index.php#-) | [TopM.cpp](ch2/14_TopM/main.cpp) | priority queue client |
47+
| [2.6](https://algs4.cs.princeton.edu/24pq/index.php#2.6) | [MaxPQ.h](ch2/head/MaxPQ.h) | max heap priority queue | [-](https://algs4.cs.princeton.edu/24pq/index.php#-) | [MinPQ.h](ch2/head/MinPQ.h) | min heap priority queue |
48+
| [-](https://algs4.cs.princeton.edu/24pq/index.php#-) | [IndexMinPQ.h](ch2/head/IndexMinPQ.h) | index min heap priority queue | [-](https://algs4.cs.princeton.edu/24pq/index.php#-) | [IndexMaxPQ.h](ch2/head/IndexMaxPQ.h) | index max heap priority queue |
49+
| [-](https://algs4.cs.princeton.edu/24pq/index.php#-) | [Multiway.h](ch2/head/Multiway.h) | multiway merge | [2.7](https://algs4.cs.princeton.edu/24pq/index.php#2.7) | [Heap.h](ch2/head/Heap.h) | heapsort |
4850

ch2/10_Quick/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ project(10_Quick)
33

44
set(CMAKE_CXX_STANDARD 14)
55

6-
set(SOURCE_FILES main.cpp ../head/Insertion.h ../head/InsertionX.h ../head/Quick3way.h ../head/QuickX.h ../head/QuickBentleyMcIlroy.h)
6+
set(SOURCE_FILES main.cpp ../head/Insertion.h ../head/InsertionX.h ../head/Quick3way.h ../head/QuickX.h ../head/QuickBentleyMcIlroy.h ../head/MaxPQ.h ../head/MinPQ.h ../head/MinPQ.h ../head/IndexMinPQ.h ../head/IndexMaxPQ.h ../head/Multiway.h ../head/Heap.h)
77
add_executable(10_Quick ${SOURCE_FILES})

ch2/14_TopM/CMakeLists.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(14_TopM)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp ../head/Insertion.h ../head/InsertionX.h ../head/Quick3way.h)
7+
add_executable(14_TopM ${SOURCE_FILES})

ch2/14_TopM/main.cpp

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <iostream>
2+
#include <fstream>
3+
#include <stack>
4+
#include "../head/MinPQ.h"
5+
#include "../head/Transaction.h"
6+
7+
using namespace std;
8+
9+
/**
10+
* The {@code TopM} class provides a client that reads a sequence of
11+
* transactions from standard input and prints the <em>m</em> largest ones
12+
* to standard output. This implementation uses a {@link MinPQ} of size
13+
* at most <em>m</em> + 1 to identify the <em>M</em> largest transactions
14+
* and a {@link Stack} to output them in the proper order.
15+
* <p>
16+
* For additional documentation, see <a href="https://algs4.cs.princeton.edu/24pq">Section 2.4</a>
17+
* of <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
18+
*
19+
* @author Robert Sedgewick
20+
* @author Kevin Wayne
21+
*/
22+
int main() {
23+
int m = 5;
24+
auto f = [](Transaction &a1, Transaction &a2) { return a1.getamount() > a2.getamount(); };
25+
MinPQ<Transaction> pq(f);
26+
fstream file("/home/ace/AceDev/C++/algorithm/ch2/data/tinyBatch.txt");
27+
string tmp;
28+
while (getline(file, tmp)) {
29+
// Create an entry from the next line and put on the PQ.
30+
Transaction *trans = new Transaction(tmp);
31+
pq.insert(*trans);
32+
if (pq.size() > m)
33+
pq.delMin();
34+
}
35+
stack<Transaction> st;
36+
for (Transaction trans: pq)
37+
st.push(trans);
38+
while (!st.empty()) {
39+
cout << st.top() << endl;
40+
st.pop();
41+
}
42+
}

ch2/15_MaxPQ/CMakeLists.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(15_MaxPQ)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp ../head/Insertion.h ../head/InsertionX.h ../head/Quick3way.h)
7+
add_executable(15_MaxPQ ${SOURCE_FILES})

ch2/15_MaxPQ/main.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <iostream>
2+
#include <fstream>
3+
#include "../head/MaxPQ.h"
4+
5+
using namespace std;
6+
7+
int main() {
8+
MaxPQ<string> pq;
9+
fstream file("/home/ace/AceDev/C++/algorithm/ch2/data/tinyPQ.txt");
10+
string tmp;
11+
while (file >> tmp) {
12+
if (tmp != "-") pq.insert(tmp);
13+
else if (!pq.isEmpty()) cout << pq.delMax() << " ";
14+
}
15+
cout << " (" << pq.size() << " left on pq)" << endl;
16+
cout << "left (descending): ";
17+
for (auto a: pq)
18+
cout << a << " ";
19+
cout << endl;
20+
}

ch2/16_MinPQ/CMakeLists.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(16_MinPQ)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp ../head/Insertion.h ../head/InsertionX.h ../head/Quick3way.h)
7+
add_executable(16_MinPQ ${SOURCE_FILES})

ch2/16_MinPQ/main.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <iostream>
2+
#include <fstream>
3+
#include "../head/MinPQ.h"
4+
5+
using namespace std;
6+
7+
int main() {
8+
MinPQ<string> pq;
9+
fstream file("/home/ace/AceDev/C++/algorithm/ch2/data/tinyPQ.txt");
10+
string tmp;
11+
while (file >> tmp) {
12+
if (tmp != "-") pq.insert(tmp);
13+
else if (!pq.isEmpty()) cout << pq.delMin() << " ";
14+
}
15+
cout << " (" << pq.size() << " left on pq)" << endl;
16+
cout << "left (ascending): ";
17+
for (auto a: pq)
18+
cout << a << " ";
19+
cout << endl;
20+
}

ch2/17_IndexMinPQ/CMakeLists.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(17_IndexMinPQ)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp ../head/Insertion.h ../head/InsertionX.h ../head/Quick3way.h)
7+
add_executable(17_IndexMinPQ ${SOURCE_FILES})

ch2/17_IndexMinPQ/main.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <iostream>
2+
#include "../head/IndexMinPQ.h"
3+
4+
using namespace std;
5+
6+
/**
7+
* Unit tests the {@code IndexMinPQ} data type.
8+
*
9+
* @param args the command-line arguments
10+
*/
11+
int main() {
12+
// insert a bunch of strings
13+
vector<string> strs{"it", "was", "the", "best", "of", "times", "it", "was", "the", "worst"};
14+
IndexMinPQ<string> pq(strs.size());
15+
for (int i = 0; i < strs.size(); ++i)
16+
pq.insert(i, strs[i]);
17+
// delete and print each key
18+
while (!pq.isEmpty()) {
19+
int i = pq.delMin();
20+
cout << i << " " << strs[i] << endl;
21+
}
22+
23+
// reinsert the same strings
24+
for (int i = 0; i < strs.size(); ++i)
25+
pq.insert(i, strs[i]);
26+
27+
cout << "------for each form-------" << endl;
28+
// print each key using the iterator
29+
for (int i : pq) {
30+
cout << i << " " << strs[i] << endl;
31+
}
32+
while (!pq.isEmpty())
33+
pq.delMin();
34+
}

ch2/18_IndexMaxPQ/CMakeLists.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(18_IndexMaxPQ)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp ../head/Insertion.h ../head/InsertionX.h ../head/Quick3way.h)
7+
add_executable(18_IndexMaxPQ ${SOURCE_FILES})

ch2/18_IndexMaxPQ/main.cpp

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include <iostream>
2+
#include <random>
3+
#include <algorithm>
4+
#include "../head/IndexMaxPQ.h"
5+
6+
using namespace std;
7+
random_device rd;
8+
mt19937 g(rd());
9+
uniform_real_distribution<double> dis(0, 1);
10+
11+
/**
12+
* Unit tests the {@code IndexMinPQ} data type.
13+
*
14+
* @param args the command-line arguments
15+
*/
16+
int main() {
17+
// insert a bunch of strings
18+
vector<string> strs{"it", "was", "the", "best", "of", "times", "it", "was", "the", "worst"};
19+
IndexMaxPQ<string> pq(strs.size());
20+
for (int i = 0; i < strs.size(); ++i)
21+
pq.insert(i, strs[i]);
22+
23+
// print each key using the iterator
24+
for (int i : pq) {
25+
cout << i << " " << strs[i] << endl;
26+
}
27+
cout << "-----after random increase and decrease-------" << endl;
28+
// increase or decrease the key
29+
for (int i = 0; i < strs.size(); i++) {
30+
if (dis(g) < 0.5)
31+
pq.increaseKey(i, strs[i] + strs[i]);
32+
else
33+
pq.decreaseKey(i, strs[i].substr(0, 1));
34+
}
35+
// delete and print each key
36+
while (!pq.isEmpty()) {
37+
string key = pq.maxKey();
38+
int i = pq.delMax();
39+
cout << i << " " << key << endl;
40+
}
41+
cout << "-----delete in random order-------" << endl;
42+
// reinsert the same strings
43+
for (int i = 0; i < strs.size(); i++) {
44+
pq.insert(i, strs[i]);
45+
}
46+
// delete them in random order
47+
vector<int> perm(strs.size());
48+
for (int i = 0; i < strs.size(); i++)
49+
perm[i] = i;
50+
shuffle(perm.begin(), perm.end(), g);
51+
for (int i = 0; i < perm.size(); i++) {
52+
string key = pq.keyOf(perm[i]);
53+
pq.delete_op(perm[i]);
54+
cout << perm[i] << " " << key << endl;
55+
}
56+
}

ch2/19_Multiway/CMakeLists.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(19_Multiway)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp ../head/Insertion.h ../head/InsertionX.h ../head/Quick3way.h)
7+
add_executable(19_Multiway ${SOURCE_FILES})

ch2/19_Multiway/main.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <iostream>
2+
#include <random>
3+
#include <fstream>
4+
#include "../head/Multiway.h"
5+
6+
using namespace std;
7+
8+
/**
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.
13+
*
14+
* @param args the command-line arguments
15+
*/
16+
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"};
20+
vector<fstream> streams;
21+
for (auto f: files)
22+
streams.push_back(fstream(f));
23+
Multiway::merge(streams);
24+
}

ch2/1_Insertion/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ project(1_Insertion)
33

44
set(CMAKE_CXX_STANDARD 14)
55

6-
set(SOURCE_FILES main.cpp ../head/BinaryInsertion.h ../head/Shell.h ../head/Merge.h ../head/MergeBU.h ../head/MergeX.h)
6+
set(SOURCE_FILES main.cpp ../head/BinaryInsertion.h ../head/Shell.h ../head/Merge.h ../head/MergeBU.h ../head/MergeX.h ../head/Inversions.h ../head/Quick.h)
77
add_executable(1_Insertion ${SOURCE_FILES})

ch2/20_Heap/CMakeLists.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(20_Heap)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp ../head/Insertion.h ../head/InsertionX.h ../head/Quick3way.h)
7+
add_executable(20_Heap ${SOURCE_FILES})

ch2/20_Heap/main.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <iostream>
2+
#include <random>
3+
#include <fstream>
4+
#include "../head/Heap.h"
5+
6+
using namespace std;
7+
8+
/**
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.
13+
*
14+
* @param args the command-line arguments
15+
*/
16+
int main() {
17+
fstream file("/home/ace/AceDev/C++/algorithm/ch2/data/tiny.txt");
18+
string tmp;
19+
vector<string> vec;
20+
while (file >> tmp)
21+
vec.push_back(tmp);
22+
cout << "before sort: " << endl;
23+
Heap::show(vec);
24+
cout << "after sort: " << endl;
25+
Heap::sort(vec);
26+
Heap::show(vec);
27+
}

ch2/CMakeLists.txt

+8-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,11 @@ add_subdirectory(9_Inversions)
1717
add_subdirectory(10_Quick)
1818
add_subdirectory(11_Quick3way)
1919
add_subdirectory(12_QuickX)
20-
add_subdirectory(13_QuickBentleyMcIlroy)
20+
add_subdirectory(13_QuickBentleyMcIlroy)
21+
add_subdirectory(14_TopM)
22+
add_subdirectory(15_MaxPQ)
23+
add_subdirectory(16_MinPQ)
24+
add_subdirectory(17_IndexMinPQ)
25+
add_subdirectory(18_IndexMaxPQ)
26+
add_subdirectory(19_Multiway)
27+
add_subdirectory(20_Heap)

ch2/data/m1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
A B C F G I I Z

ch2/data/m2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
B D H P Q Q

ch2/data/m3.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
A B E F J N

ch2/data/tinyBatch.txt

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Turing 6/17/1990 644.08
2+
vonNeumann 3/26/2002 4121.85
3+
Dijkstra 8/22/2007 2678.40
4+
vonNeumann 1/11/1999 4409.74
5+
Dijkstra 11/18/1995 837.42
6+
Hoare 5/10/1993 3229.27
7+
vonNeumann 2/12/1994 4732.35
8+
Hoare 8/18/1992 4381.21
9+
Turing 1/11/2002 66.10
10+
Thompson 2/27/2000 4747.08
11+
Turing 2/11/1991 2156.86
12+
Hoare 8/12/2003 1025.70
13+
vonNeumann 10/13/1993 2520.97
14+
Dijkstra 9/10/2000 708.95
15+
Turing 10/12/1993 3532.36
16+
Hoare 2/10/2005 4050.20

ch2/data/tinyPQ.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
P Q E - X A M - P L E -

0 commit comments

Comments
 (0)