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})

ch2/5_Shell/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/Shell.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+
Shell::sort(vec);
15+
Shell::show(vec);
16+
}

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

ch2/6_Merge/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/Merge.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+
Merge::sort(vec);
15+
Merge::show(vec);
16+
}

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

ch2/7_MergeBU/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/MergeBU.h"
4+
5+
using namespace std;
6+
7+
int main() {
8+
ifstream file("/home/ace/AceDev/C++/algorithm/ch2/data/words3.txt");
9+
string tmp;
10+
vector<string> vec;
11+
while (file >> tmp) {
12+
vec.push_back(tmp);
13+
}
14+
MergeBU::sort(vec);
15+
MergeBU::show(vec);
16+
}

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

ch2/8_MergeX/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/MergeX.h"
4+
5+
using namespace std;
6+
7+
int main() {
8+
ifstream file("/home/ace/AceDev/C++/algorithm/ch2/data/words3.txt");
9+
string tmp;
10+
vector<string> vec;
11+
while (file >> tmp) {
12+
vec.push_back(tmp);
13+
}
14+
MergeX::sort(vec);
15+
MergeX::show(vec);
16+
}

ch2/CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(ch2)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
include_directories(head)
7+
8+
add_subdirectory(1_Insertion)
9+
add_subdirectory(2_InsertionX)
10+
add_subdirectory(3_BinaryInsertion)
11+
add_subdirectory(4_Selection)
12+
add_subdirectory(5_Shell)
13+
add_subdirectory(6_Merge)
14+
add_subdirectory(7_MergeBU)
15+
add_subdirectory(8_MergeX)

ch2/data/tiny.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
S O R T E X A M P L E

ch2/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

ch2/head/BinaryInsertion.h

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#ifndef CH2_BINARYINSERTION_H
2+
#define CH2_BINARYINSERTION_H
3+
4+
#include <vector>
5+
#include <iostream>
6+
7+
using std::vector;
8+
using std::swap;
9+
using std::cout;
10+
using std::endl;
11+
12+
/**
13+
* The {@code BinaryInsertion} class provides a static method for sorting an
14+
* array using an optimized binary insertion sort with half exchanges.
15+
* <p>
16+
* This implementation makes ~ n lg n compares for any array of length n.
17+
* However, in the worst case, the running time is quadratic because the
18+
* number of array accesses can be proportional to n^2 (e.g, if the array
19+
* is reverse sorted). As such, it is not suitable for sorting large
20+
* arrays (unless the number of inversions is small).
21+
* <p>
22+
* The sorting algorithm is stable and uses O(1) extra memory.
23+
* <p>
24+
* For additional documentation, see <a href="https://algs4.cs.princeton.edu/21elementary">Section 2.1</a> of
25+
* <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
26+
*
27+
* @author Ivan Pesin
28+
* @author Robert Sedgewick
29+
* @author Kevin Wayne
30+
*/
31+
class BinaryInsertion {
32+
public:
33+
// This class should not be instantiated.
34+
BinaryInsertion() = delete;
35+
36+
/**
37+
* Rearranges the array in ascending order, using the natural order.
38+
* @param a the array to be sorted
39+
*/
40+
template<typename T>
41+
static void sort(vector<T> &a) {
42+
int n = a.size();
43+
for (int i = 1; i < n; i++) {
44+
45+
// binary search to determine index j at which to insert a[i]
46+
T v = a[i];
47+
int lo = 0, hi = i;
48+
while (lo < hi) {
49+
int mid = lo + (hi - lo) / 2;
50+
if (v < a[mid]) hi = mid;
51+
else lo = mid + 1;
52+
}
53+
54+
// insetion sort with "half exchanges"
55+
// (insert a[i] at index j and shift a[j], ..., a[i-1] to right)
56+
for (int j = i; j > lo; --j)
57+
a[j] = a[j - 1];
58+
a[lo] = v;
59+
}
60+
}
61+
62+
// print array to standard output
63+
template<typename T>
64+
static void show(vector<T> &a) {
65+
for (int i = 0; i < a.size(); i++) {
66+
cout << a[i] << endl;
67+
}
68+
}
69+
70+
};
71+
72+
#endif //CH2_BINARYINSERTION_H

0 commit comments

Comments
 (0)