Skip to content

Commit 9eb22dd

Browse files
committed
update ch2
1 parent 01a8e39 commit 9eb22dd

File tree

17 files changed

+650
-8
lines changed

17 files changed

+650
-8
lines changed

README.md

+11-7
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,14 @@ 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-
| | | | | | |
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+
| | | | | | |
48+

ch2/10_Quick/CMakeLists.txt

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

ch2/10_Quick/main.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <iostream>
2+
#include <fstream>
3+
#include "../head/Quick.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+
Quick::sort(vec);
15+
for (auto a: vec)
16+
cout << a << " ";
17+
cout << endl;
18+
// shuffle
19+
shuffle(vec.begin(), vec.end(), g);
20+
cout << "select kth large in array: " << endl;
21+
for (auto a: vec)
22+
cout << a << " ";
23+
cout << endl;
24+
for (int i = 0; i < vec.size(); ++i) {
25+
auto str = Quick::select(vec, i);
26+
cout << i << " th: " << str << endl;
27+
}
28+
}

ch2/11_Quick3way/CMakeLists.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(11_Quick3way)
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(11_Quick3way ${SOURCE_FILES})

ch2/11_Quick3way/main.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <iostream>
2+
#include <fstream>
3+
#include "../head/Quick3way.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+
Quick3way::sort(vec);
15+
Quick3way::show(vec);
16+
}

ch2/12_QuickX/CMakeLists.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(12_QuickX)
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(12_QuickX ${SOURCE_FILES})

ch2/12_QuickX/main.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <iostream>
2+
#include <fstream>
3+
#include "../head/QuickX.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+
QuickX::sort(vec);
15+
QuickX::show(vec);
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(13_QuickBentleyMcIlroy)
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(13_QuickBentleyMcIlroy ${SOURCE_FILES})

ch2/13_QuickBentleyMcIlroy/main.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <iostream>
2+
#include <fstream>
3+
#include "../head/QuickBentleyMcIlroy.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+
QuickBentleyMcIlroy::sort(vec);
15+
QuickBentleyMcIlroy::show(vec);
16+
}

ch2/9_Inversions/CMakeLists.txt

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

ch2/9_Inversions/main.cpp

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

ch2/CMakeLists.txt

+6-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,9 @@ add_subdirectory(4_Selection)
1212
add_subdirectory(5_Shell)
1313
add_subdirectory(6_Merge)
1414
add_subdirectory(7_MergeBU)
15-
add_subdirectory(8_MergeX)
15+
add_subdirectory(8_MergeX)
16+
add_subdirectory(9_Inversions)
17+
add_subdirectory(10_Quick)
18+
add_subdirectory(11_Quick3way)
19+
add_subdirectory(12_QuickX)
20+
add_subdirectory(13_QuickBentleyMcIlroy)

ch2/head/Inversions.h

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#ifndef CH2_INVERSIONS_H
2+
#define CH2_INVERSIONS_H
3+
4+
#include <vector>
5+
6+
using std::vector;
7+
8+
/**
9+
* The {@code Inversions} class provides static methods to count the
10+
* number of <em>inversions</em> in either an array of integers or comparables.
11+
* An inversion in an array {@code a[]} is a pair of indicies {@code i} and
12+
* {@code j} such that {@code i < j} and {@code a[i] > a[j]}.
13+
* <p>
14+
* This implementation uses a generalization of mergesort. The <em>count</em>
15+
* operation takes time proportional to <em>n</em> log <em>n</em>,
16+
* where <em>n</em> is the number of keys in the array.
17+
* <p>
18+
* For additional documentation, see <a href="https://algs4.cs.princeton.edu/22mergesort">Section 2.2</a>
19+
* of <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
20+
*
21+
* @author Robert Sedgewick
22+
* @author Kevin Wayne
23+
*/
24+
class Inversions {
25+
public:
26+
// do not instantiate
27+
Inversions() = delete;
28+
29+
private:
30+
// merge and count
31+
template<typename T>
32+
static long merge(vector<T> &a, vector<T> &aux, int lo, int mid, int hi) {
33+
long inversions = 0;
34+
35+
// copy to aux[]
36+
for (int k = lo; k <= hi; k++) {
37+
aux[k] = a[k];
38+
}
39+
40+
// merge back to a[]
41+
int i = lo, j = mid + 1;
42+
for (int k = lo; k <= hi; k++) {
43+
if (i > mid) a[k] = aux[j++];
44+
else if (j > hi) a[k] = aux[i++];
45+
else if (aux[j] < aux[i]) {
46+
a[k] = aux[j++];
47+
inversions += (mid - i + 1);
48+
}
49+
else a[k] = aux[i++];
50+
}
51+
return inversions;
52+
}
53+
54+
};
55+
56+
#endif //CH2_INVERSIONS_H

ch2/head/Quick.h

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#ifndef CH2_QUICK_H
2+
#define CH2_QUICK_H
3+
4+
#include <vector>
5+
#include <random>
6+
#include <iostream>
7+
#include <algorithm>
8+
#include <stdexcept>
9+
10+
using std::vector;
11+
using std::swap;
12+
using std::cout;
13+
using std::endl;
14+
using std::to_string;
15+
using std::random_device;
16+
using std::mt19937;
17+
using std::shuffle;
18+
using std::runtime_error;
19+
20+
random_device rd;
21+
mt19937 g(rd());
22+
23+
/**
24+
* The {@code Quick} class provides static methods for sorting an
25+
* array and selecting the ith smallest element in an array using quicksort.
26+
* <p>
27+
* For additional documentation,
28+
* see <a href="https://algs4.cs.princeton.edu/23quick">Section 2.3</a> of
29+
* <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
30+
*
31+
* @author Robert Sedgewick
32+
* @author Kevin Wayne
33+
*/
34+
class Quick {
35+
public:
36+
// This class should not be instantiated.
37+
Quick() = delete;
38+
39+
/**
40+
* Rearranges the array in ascending order, using the natural order.
41+
* @param a the array to be sorted
42+
*/
43+
template<typename T>
44+
static void sort(vector<T> &a) {
45+
shuffle(a.begin(), a.end(), g);
46+
sort(a, 0, a.size() - 1);
47+
}
48+
49+
/**
50+
* Rearranges the array so that {@code a[k]} contains the kth smallest key;
51+
* {@code a[0]} through {@code a[k-1]} are less than (or equal to) {@code a[k]}; and
52+
* {@code a[k+1]} through {@code a[n-1]} are greater than (or equal to) {@code a[k]}.
53+
*
54+
* @param a the array
55+
* @param k the rank of the key
56+
* @return the key of rank {@code k}
57+
* @throws IllegalArgumentException unless {@code 0 <= k < a.length}
58+
*/
59+
template<typename T>
60+
static T select(vector<T> &a, int k) {
61+
if (k < 0 || k >= a.size()) {
62+
throw runtime_error("index is not between 0 and " + to_string(a.size()) + ": " + to_string(k));
63+
}
64+
shuffle(a.begin(), a.end(), g);
65+
int lo = 0, hi = a.size() - 1;
66+
while (hi > lo) {
67+
int i = partition(a, lo, hi);
68+
if (i > k) hi = i - 1;
69+
else if (i < k) lo = i + 1;
70+
else return a[i];
71+
}
72+
return a[lo];
73+
}
74+
75+
// print array to standard output
76+
template<typename T>
77+
static void show(vector<T> &a) {
78+
for (int i = 0; i < a.size(); i++) {
79+
cout << a[i] << endl;
80+
}
81+
}
82+
83+
84+
private:
85+
// quicksort the subarray from a[lo] to a[hi]
86+
template<typename T>
87+
static void sort(vector<T> &a, int lo, int hi) {
88+
if (hi <= lo) return;
89+
int j = partition(a, lo, hi);
90+
sort(a, lo, j - 1);
91+
sort(a, j + 1, hi);
92+
}
93+
94+
// partition the subarray a[lo..hi] so that a[lo..j-1] <= a[j] <= a[j+1..hi]
95+
// and return the index j.
96+
template<typename T>
97+
static int partition(vector<T> &a, int lo, int hi) {
98+
int i = lo;
99+
int j = hi + 1;
100+
T v = a[lo];
101+
while (true) {
102+
// find item on lo to swap
103+
while (a[++i] < v) {
104+
if (i == hi) break;
105+
}
106+
107+
// find item on hi to swap
108+
while (v < a[--j]) {
109+
if (j == lo) break; // redundant since a[lo] acts as sentinel
110+
}
111+
112+
// check if pointers cross
113+
if (i >= j) break;
114+
115+
swap(a[i], a[j]);
116+
}
117+
118+
// put partitioning item v at a[j]
119+
swap(a[lo], a[j]);
120+
121+
// now, a[lo .. j-1] <= a[j] <= a[j+1 .. hi]
122+
return j;
123+
}
124+
};
125+
126+
#endif //CH2_QUICK_H

0 commit comments

Comments
 (0)