Skip to content

Commit ed9f8e4

Browse files
committed
add ch5
1 parent 1c27e80 commit ed9f8e4

File tree

13 files changed

+540
-0
lines changed

13 files changed

+540
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,11 @@ Algorithms, 4th edition textbook code (using c++)
9595
| [-](https://algs4.cs.princeton.edu/44sp/index.php#-) | [Arbitrage.cpp](ch4/49_Arbitrage/main.cpp) | arbitrage detection | [-](https://algs4.cs.princeton.edu/44sp/index.php#-) | [FloydWarshall.h](ch4/head/FloydWarshall.h) | all-pairs shortest paths (dense) |
9696
| [-](https://algs4.cs.princeton.edu/44sp/index.php#-) | [AdjMatrixEdgeWeightedDigraph.h](ch4/head/AdjMatrixEdgeWeightedDigraph.h) | edge-weighted graph (dense) | | | |
9797

98+
## ch5. Strings
99+
100+
| REF | PROGRAM | DESCRIPTION / C++DOC | REF | PROGRAM | DESCRIPTION / C++DOC |
101+
| :---------------------------------------------------------: | :----------------------------------------------------------: | :----------------------: | :---------------------------------------------------------: | :----------------------------------------------------------: | :------------------: |
102+
| [-](https://algs4.cs.princeton.edu/50strings/index.php#-) | [Alphabet.java](https://algs4.cs.princeton.edu/50strings/Alphabet.java.html) | alphabet | [-](https://algs4.cs.princeton.edu/50strings/index.php#-) | [Count.java](https://algs4.cs.princeton.edu/50strings/Count.java.html) | alphabet client |
103+
| [5.1](https://algs4.cs.princeton.edu/51radix/index.php#5.1) | [LSD.h](ch5/head/LSD.h) | LSD radix sort | [5.2](https://algs4.cs.princeton.edu/51radix/index.php#5.2) | [MSD.h](ch5/head/MSD.h) | MSD radix sort |
104+
| [-](https://algs4.cs.princeton.edu/51radix/index.php#-) | [InplaceMSD.h](ch5/head/InplaceMSD.h) | In-place MSD radix sort1 | | | |
105+

ch5/3_LSD/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_LSD)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp ../head/LSD.h ../head/MSD.h ../head/InplaceMSD.h)
7+
add_executable(3_LSD ${SOURCE_FILES})

ch5/3_LSD/main.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include "../head/LSD.h"
2+
#include <iostream>
3+
#include <fstream>
4+
5+
using namespace std;
6+
7+
/**
8+
* Reads in a sequence of fixed-length strings from standard input;
9+
* LSD radix sorts them;
10+
* and prints them to standard output in ascending order.
11+
*
12+
* @param args the command-line arguments
13+
*/
14+
int main() {
15+
string file = "/home/ace/AceDev/C++/algorithm/ch5/data/words3.txt";
16+
fstream in(file);
17+
string tmp;
18+
vector<string> a;
19+
while (in >> tmp)
20+
a.push_back(tmp);
21+
22+
int n = a.size();
23+
int w = a[0].length();
24+
25+
// sort the strings
26+
LSD::sort(a, w);
27+
28+
// print results
29+
for (int i = 0; i < n; ++i)
30+
cout << a[i] << endl;
31+
}

ch5/4_MSD/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_MSD)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp ../head/LSD.h ../head/MSD.h)
7+
add_executable(4_MSD ${SOURCE_FILES})

ch5/4_MSD/main.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include "../head/MSD.h"
2+
#include <iostream>
3+
#include <fstream>
4+
5+
using namespace std;
6+
7+
/**
8+
* Reads in a sequence of extended ASCII strings from standard input;
9+
* MSD radix sorts them;
10+
* and prints them to standard output in ascending order.
11+
*
12+
* @param args the command-line arguments
13+
*/
14+
int main() {
15+
string file = "/home/ace/AceDev/C++/algorithm/ch5/data/shells.txt";
16+
fstream in(file);
17+
string tmp;
18+
vector<string> a;
19+
while (in >> tmp)
20+
a.push_back(tmp);
21+
22+
int n = a.size();
23+
int w = a[0].length();
24+
25+
// sort the strings
26+
MSD::sort(a);
27+
28+
// print results
29+
for (int i = 0; i < n; ++i)
30+
cout << a[i] << endl;
31+
}

ch5/5_InplaceMSD/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_InplaceMSD)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp ../head/LSD.h ../head/MSD.h)
7+
add_executable(5_InplaceMSD ${SOURCE_FILES})

ch5/5_InplaceMSD/main.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include "../head/InplaceMSD.h"
2+
#include <iostream>
3+
#include <fstream>
4+
5+
using namespace std;
6+
7+
/**
8+
* Reads in a sequence of extended ASCII strings from standard input;
9+
* in-place MSD radix sorts them;
10+
* and prints them to standard output in ascending order.
11+
*
12+
* @param args the command-line arguments
13+
*/
14+
int main() {
15+
string file = "/home/ace/AceDev/C++/algorithm/ch5/data/shells.txt";
16+
fstream in(file);
17+
string tmp;
18+
vector<string> a;
19+
while (in >> tmp)
20+
a.push_back(tmp);
21+
22+
int n = a.size();
23+
int w = a[0].length();
24+
25+
// sort the strings
26+
InplaceMSD::sort(a);
27+
28+
// print results
29+
for (int i = 0; i < n; ++i)
30+
cout << a[i] << endl;
31+
}

ch5/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(ch5)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
include_directories(head)
7+
include_directories(data)
8+
9+
add_subdirectory(3_LSD)
10+
add_subdirectory(4_MSD)
11+
add_subdirectory(5_InplaceMSD)
12+

ch5/data/shells.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
she sells seashells by the sea shore
2+
the shells she sells are surely seashells

ch5/data/words3.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
bed bug dad yes zoo
2+
now for tip ilk dim
3+
tag jot sob nob sky
4+
hut men egg few jay
5+
owl joy rap gig wee
6+
was wad fee tap tar
7+
dug jam all bad yet

0 commit comments

Comments
 (0)