Skip to content

Commit 01a8e39

Browse files
committed
add ch2
1 parent 54bc41f commit 01a8e39

28 files changed

+963
-0
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Algorithms, 4th edition textbook code (using c++)
66
1. based on STL Library
77
2. using C++14
88
3. **Not support** drawing
9+
4. Welcome to point out the error, and pull better code
910

1011
> the code is writed and debug in CLion IDE,and not test in terminal(I will check it after finish more code)
1112
@@ -32,3 +33,12 @@ Algorithms, 4th edition textbook code (using c++)
3233
| [-](https://algs4.cs.princeton.edu/15uf/index.php#-) | [QuickFindUF.h](ch1/head/QuickFindUF.h) | quick find | [-](https://algs4.cs.princeton.edu/15uf/index.php#-) | [QuickUnionUF.h](ch1/head/QuickUnionUF.h) | quick union |
3334
| [1.5](https://algs4.cs.princeton.edu/15uf/index.php#1.5) | [WeightedQuickUnionUF.h](ch1/head/WeightedQuickUnionUF.h) | weighted quick union | [-](https://algs4.cs.princeton.edu/15uf/index.php#-) | [UF.h](ch1/head/UF.h) | union-by-rank with path halving |
3435

36+
## ch2. Sorting
37+
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+
| | | | | | |

ch2/1_Insertion/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_Insertion)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp ../head/BinaryInsertion.h ../head/Shell.h ../head/Merge.h ../head/MergeBU.h ../head/MergeX.h)
7+
add_executable(1_Insertion ${SOURCE_FILES})

ch2/1_Insertion/main.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <iostream>
2+
#include <fstream>
3+
#include "../head/Insertion.h"
4+
5+
using namespace std;
6+
7+
int main() {
8+
ifstream file("/home/ace/AceDev/C++/algorithm/ch2/data/tiny.txt");
9+
string tmp;
10+
vector<string> vec;
11+
while (file >> tmp) {
12+
vec.push_back(tmp);
13+
}
14+
Insertion::sort(vec);
15+
// Insertion::sort(vec, [](string a, string b) { return a > b; });
16+
Insertion::show(vec);
17+
}

ch2/2_InsertionX/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_InsertionX)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp)
7+
add_executable(2_InsertionX ${SOURCE_FILES})

ch2/2_InsertionX/main.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <iostream>
2+
#include <fstream>
3+
#include "../head/InsertionX.h"
4+
5+
using namespace std;
6+
7+
int main() {
8+
ifstream file("/home/ace/AceDev/C++/algorithm/ch2/data/tiny.txt");
9+
string tmp;
10+
vector<string> vec;
11+
while (file >> tmp) {
12+
vec.push_back(tmp);
13+
}
14+
InsertionX::sort(vec);
15+
InsertionX::show(vec);
16+
}

ch2/3_BinaryInsertion/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_BinaryInsertion)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp)
7+
add_executable(3_BinaryInsertion ${SOURCE_FILES})

ch2/3_BinaryInsertion/main.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <iostream>
2+
#include <fstream>
3+
#include "../head/BinaryInsertion.h"
4+
5+
using namespace std;
6+
7+
int main() {
8+
ifstream file("/home/ace/AceDev/C++/algorithm/ch2/data/tiny.txt");
9+
string tmp;
10+
vector<string> vec;
11+
while (file >> tmp) {
12+
vec.push_back(tmp);
13+
}
14+
BinaryInsertion::sort(vec);
15+
BinaryInsertion::show(vec);
16+
}

ch2/4_Selection/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_Selection)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp ../head/Insertion.h ../head/InsertionX.h)
7+
add_executable(4_Selection ${SOURCE_FILES})

ch2/4_Selection/main.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <iostream>
2+
#include <fstream>
3+
#include "../head/Selection.h"
4+
5+
using namespace std;
6+
7+
int main() {
8+
ifstream file("/home/ace/AceDev/C++/algorithm/ch2/data/tiny.txt");
9+
string tmp;
10+
vector<string> vec;
11+
while (file >> tmp) {
12+
vec.push_back(tmp);
13+
}
14+
Selection::sort(vec);
15+
Selection::show(vec);
16+
}

ch2/5_Shell/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_Shell)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp ../head/Insertion.h ../head/InsertionX.h)
7+
add_executable(5_Shell ${SOURCE_FILES})

0 commit comments

Comments
 (0)