Skip to content

Commit 1d376a2

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

31 files changed

+1948
-13
lines changed

README.md

Lines changed: 12 additions & 10 deletions
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

Lines changed: 1 addition & 1 deletion
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

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(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

Lines changed: 42 additions & 0 deletions
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

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(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

Lines changed: 20 additions & 0 deletions
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

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(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

Lines changed: 20 additions & 0 deletions
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

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(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

Lines changed: 34 additions & 0 deletions
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+
}

0 commit comments

Comments
 (0)