Skip to content

Commit 15535f5

Browse files
committed
update ch1
1 parent a353ad7 commit 15535f5

31 files changed

+3064554
-16
lines changed

README.md

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,23 @@ Algorithms, 4th edition textbook code (using c++)
1111
1212
## ch1. Fundamentals
1313

14-
| REF | PROGRAM | DESCRIPTION / JAVADOC | REF | PROGRAM | DESCRIPTION / JAVADOC |
15-
| :----------------------------------------------------------: | :----------------------------------------------------------: | :------------------------------: | :----------------------------------------------------------: | :----------------------------------------------------------: | :-----------------------------: |
16-
| [-](https://algs4.cs.princeton.edu/11model/index.php#-) | [BinarySearch.h](ch1/head/BinarySearch.h) | binary search | [-](https://algs4.cs.princeton.edu/11model/index.php#-) | [RandomSeq.cpp](ch1/2_RandomSeq/main.cpp) | random numbers in a given range |
17-
| [-](https://algs4.cs.princeton.edu/11model/index.php#-) | [Average.cpp](ch1/3_Average/main.cpp) | average of a sequence of numbers | [-](https://algs4.cs.princeton.edu/11model/index.php#-) | [Cat.cpp](ch1/4_Cat/main.cpp) | concatenate files |
18-
| [-](https://algs4.cs.princeton.edu/11model/index.php#-) | [Knuth.h](ch1/head/Knuth.h) | Knuth shuffle | [-](https://algs4.cs.princeton.edu/12oop/index.php#-) | [Counter.h](ch1/head/Counter.h) | counter |
19-
| [-](https://algs4.cs.princeton.edu/12oop/index.php#-) | [StaticSETofInts.h](ch1/head/StaticSETofInts.h) | set of integers | [-](https://algs4.cs.princeton.edu/12oop/index.php#-) | [Whitelist.cpp](ch1/8_Whitelist/main.cpp) | whitelist client |
20-
| [-](https://algs4.cs.princeton.edu/12oop/index.php#-) | [Vector.h](ch1/head/Vector.h) | Euclidean vector | [-](https://algs4.cs.princeton.edu/12oop/index.php#-) | [Date.h](ch1/head/Date.h) | date |
21-
| [-](https://algs4.cs.princeton.edu/12oop/index.php#-) | [Transaction.h](ch1/head/Transaction.h) | transaction | [-](https://algs4.cs.princeton.edu/12oop/index.php#-) | [Point2D.h](ch1/head/Point2D.h) | point |
22-
| [-](https://algs4.cs.princeton.edu/12oop/index.php#-) | [RectHV.h](ch1/head/RectHV.h) | axis-aligned rectangle | [-](https://algs4.cs.princeton.edu/12oop/index.php#-) | [Interval1D.h](ch1/head/Interval1D.h) | 1d interval |
23-
| [-](https://algs4.cs.princeton.edu/12oop/index.php#-) | [Interval2D.h](ch1/head/Interval2D.h) | 2d interval | [-](https://algs4.cs.princeton.edu/12oop/index.php#-) | [Accumulator.h](ch1/head/Accumulator.h) | running average and stddev |
24-
| [1.1](https://algs4.cs.princeton.edu/13stacks/index.php#1.1) | [ResizingArrayStack.h](ch1/head/ResizingArrayStack.h) | LIFO stack (resizing array) | [1.2](https://algs4.cs.princeton.edu/13stacks/index.php#1.2) | [LinkedStack.h](ch1/head/LinkedStack.h) | LIFO stack (linked list) |
25-
| [-](https://algs4.cs.princeton.edu/13stacks/index.php#-) | [Stack.h](ch1/head/Stack.h) | LIFO stack | [-](https://algs4.cs.princeton.edu/13stacks/index.php#-) | [ResizingArrayQueue.h](ch1/head/ResizingArrayQueue.h) | FIFO queue (resizing array) |
26-
| [1.3](https://algs4.cs.princeton.edu/13stacks/index.php#1.3) | [LinkedQueue.h](ch1/head/LinkedQueue.h) | FIFO queue (linked list) | [-](https://algs4.cs.princeton.edu/13stacks/index.php#-) | [Queue.h](ch1/head/Queue.h) | FIFO queue |
27-
| [-](https://algs4.cs.princeton.edu/13stacks/index.php#-) | [ResizingArrayBag.h](ch1/head/ResizingArrayBag.h) | multiset (resizing array) | [1.4](https://algs4.cs.princeton.edu/13stacks/index.php#1.4) | [LinkedBag.h](ch1/head/LinkedBag.h) | multiset (linked list) |
28-
| [-](https://algs4.cs.princeton.edu/13stacks/index.php#-) | [Bag.h](ch1/head/Bag.h) | multiset | | | |
14+
| REF | PROGRAM | DESCRIPTION / JAVADOC | REF | PROGRAM | DESCRIPTION / JAVADOC |
15+
| :----------------------------------------------------------: | :---------------------------------------------------: | :------------------------------: | :----------------------------------------------------------: | :----------------------------------------------------------: | :-----------------------------: |
16+
| [-](https://algs4.cs.princeton.edu/11model/index.php#-) | [BinarySearch.h](ch1/head/BinarySearch.h) | binary search | [-](https://algs4.cs.princeton.edu/11model/index.php#-) | [RandomSeq.cpp](ch1/2_RandomSeq/main.cpp) | random numbers in a given range |
17+
| [-](https://algs4.cs.princeton.edu/11model/index.php#-) | [Average.cpp](ch1/3_Average/main.cpp) | average of a sequence of numbers | [-](https://algs4.cs.princeton.edu/11model/index.php#-) | [Cat.cpp](ch1/4_Cat/main.cpp) | concatenate files |
18+
| [-](https://algs4.cs.princeton.edu/11model/index.php#-) | [Knuth.h](ch1/head/Knuth.h) | Knuth shuffle | [-](https://algs4.cs.princeton.edu/12oop/index.php#-) | [Counter.h](ch1/head/Counter.h) | counter |
19+
| [-](https://algs4.cs.princeton.edu/12oop/index.php#-) | [StaticSETofInts.h](ch1/head/StaticSETofInts.h) | set of integers | [-](https://algs4.cs.princeton.edu/12oop/index.php#-) | [Whitelist.cpp](ch1/8_Whitelist/main.cpp) | whitelist client |
20+
| [-](https://algs4.cs.princeton.edu/12oop/index.php#-) | [Vector.h](ch1/head/Vector.h) | Euclidean vector | [-](https://algs4.cs.princeton.edu/12oop/index.php#-) | [Date.h](ch1/head/Date.h) | date |
21+
| [-](https://algs4.cs.princeton.edu/12oop/index.php#-) | [Transaction.h](ch1/head/Transaction.h) | transaction | [-](https://algs4.cs.princeton.edu/12oop/index.php#-) | [Point2D.h](ch1/head/Point2D.h) | point |
22+
| [-](https://algs4.cs.princeton.edu/12oop/index.php#-) | [RectHV.h](ch1/head/RectHV.h) | axis-aligned rectangle | [-](https://algs4.cs.princeton.edu/12oop/index.php#-) | [Interval1D.h](ch1/head/Interval1D.h) | 1d interval |
23+
| [-](https://algs4.cs.princeton.edu/12oop/index.php#-) | [Interval2D.h](ch1/head/Interval2D.h) | 2d interval | [-](https://algs4.cs.princeton.edu/12oop/index.php#-) | [Accumulator.h](ch1/head/Accumulator.h) | running average and stddev |
24+
| [1.1](https://algs4.cs.princeton.edu/13stacks/index.php#1.1) | [ResizingArrayStack.h](ch1/head/ResizingArrayStack.h) | LIFO stack (resizing array) | [1.2](https://algs4.cs.princeton.edu/13stacks/index.php#1.2) | [LinkedStack.h](ch1/head/LinkedStack.h) | LIFO stack (linked list) |
25+
| [-](https://algs4.cs.princeton.edu/13stacks/index.php#-) | [Stack.h](ch1/head/Stack.h) | LIFO stack | [-](https://algs4.cs.princeton.edu/13stacks/index.php#-) | [ResizingArrayQueue.h](ch1/head/ResizingArrayQueue.h) | FIFO queue (resizing array) |
26+
| [1.3](https://algs4.cs.princeton.edu/13stacks/index.php#1.3) | [LinkedQueue.h](ch1/head/LinkedQueue.h) | FIFO queue (linked list) | [-](https://algs4.cs.princeton.edu/13stacks/index.php#-) | [Queue.h](ch1/head/Queue.h) | FIFO queue |
27+
| [-](https://algs4.cs.princeton.edu/13stacks/index.php#-) | [ResizingArrayBag.h](ch1/head/ResizingArrayBag.h) | multiset (resizing array) | [1.4](https://algs4.cs.princeton.edu/13stacks/index.php#1.4) | [LinkedBag.h](ch1/head/LinkedBag.h) | multiset (linked list) |
28+
| [-](https://algs4.cs.princeton.edu/13stacks/index.php#-) | [Bag.h](ch1/head/Bag.h) | multiset | [-](https://algs4.cs.princeton.edu/14analysis/index.php#-) | [Stopwatch.h](ch1/head/Stopwatch.h) | timer (wall time) |
29+
| [-](https://algs4.cs.princeton.edu/14analysis/index.php#-) | [StopwatchCPU.h]() | timer (CPU time) | [-](https://algs4.cs.princeton.edu/14analysis/index.php#-) | [LinearRegression.java](https://algs4.cs.princeton.edu/14analysis/LinearRegression.java.html) | simple linear regression |
30+
| [-](https://algs4.cs.princeton.edu/14analysis/index.php#-) | [ThreeSum.h](ch1/head/ThreeSum.h) | brute-force three sum | [-](https://algs4.cs.princeton.edu/14analysis/index.php#-) | [ThreeSumFast.h](ch1/head/ThreeSumFast.h) | faster three sum |
31+
| [-](https://algs4.cs.princeton.edu/14analysis/index.php#-) | [DoublingTest.h](ch1/head/DoublingTest.h) | doubling test | [-](https://algs4.cs.princeton.edu/14analysis/index.php#-) | [DoublingRatio.cpp](ch1/32_DoublingRatio/main.cpp) | doubling ratio |
32+
| [-](https://algs4.cs.princeton.edu/15uf/index.php#-) | [QuickFindUF.h](ch1/head/QuickFindUF.h) | quick find | | | |
2933

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

ch1/26_Stopwatch/main.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <iostream>
2+
#include <cmath>
3+
#include "../head/Stopwatch.h"
4+
5+
using namespace std;
6+
7+
8+
int main() {
9+
int n = 10000;
10+
// sum of square roots of integers from 1 to n using Math.sqrt(x).
11+
Stopwatch timer1;
12+
double sum1 = 0.0;
13+
for (int i = 1; i <= n; ++i)
14+
sum1 += sqrt(i);
15+
int time1 = timer1.elapsedTime();
16+
cout << sum1 << " costs " << time1 << " microseconds" << endl;
17+
18+
// sum of square roots of integers from 1 to n using Math.pow(x, 0.5).
19+
Stopwatch timer2;
20+
double sum2 = 0.0;
21+
for (int i = 1; i <= n; i++) {
22+
sum2 += pow(i, 0.5);
23+
}
24+
int time2 = timer2.elapsedTime();
25+
cout << sum2 << " costs " << time2 << " microseconds" << endl;
26+
27+
}

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

ch1/27_StopwatchCPU/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// TODO: un-finish

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

ch1/29_ThreeSum/main.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <istream>
2+
#include <fstream>
3+
#include "../head/ThreeSum.h"
4+
#include "../head/Stopwatch.h"
5+
6+
using namespace std;
7+
8+
/**
9+
* Reads in a sequence of integers from a file, specified as a command-line argument;
10+
* counts the number of triples sum to exactly zero; prints out the time to perform
11+
* the computation.
12+
*
13+
* @param args the command-line arguments
14+
*/
15+
int main() {
16+
ifstream file("/home/ace/AceDev/C++/algorithm/ch1/data/2Kints.txt");
17+
vector<int> vec;
18+
int tmp;
19+
while (file >> tmp)
20+
vec.push_back(tmp);
21+
22+
Stopwatch timer1;
23+
int count = ThreeSum::count(vec);
24+
cout << "elapsed time = " << timer1.elapsedTime() << " microseconds" << endl;
25+
cout << "number of count: " << count << endl;
26+
}

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

ch1/30_ThreeSumFast/main.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <istream>
2+
#include <fstream>
3+
#include "../head/ThreeSumFast.h"
4+
#include "../head/Stopwatch.h"
5+
6+
using namespace std;
7+
8+
/**
9+
* Reads in a sequence of distinct integers from a file, specified as a command-line argument;
10+
* counts the number of triples sum to exactly zero; prints out the time to perform
11+
* the computation.
12+
*
13+
* @param args the command-line arguments
14+
*/
15+
int main() {
16+
ifstream file("/home/ace/AceDev/C++/algorithm/ch1/data/1Kints.txt");
17+
vector<int> vec;
18+
int tmp;
19+
while (file >> tmp)
20+
vec.push_back(tmp);
21+
22+
Stopwatch timer1;
23+
int count = ThreeSumFast::count(vec);
24+
// ThreeSumFast::printAll(vec);
25+
cout << "elapsed time = " << timer1.elapsedTime() << " microseconds" << endl;
26+
cout << "number of count: " << count << endl;
27+
}

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

ch1/31_DoublingTest/main.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <istream>
2+
#include <fstream>
3+
#include "../head/DoublingTest.h"
4+
5+
using namespace std;
6+
7+
/**
8+
* Prints table of running times to call {@code ThreeSum.count()}
9+
* for arrays of size 250, 500, 1000, 2000, and so forth.
10+
*
11+
* @param args the command-line arguments
12+
*/
13+
int main() {
14+
for (int n = 250; n <= 4000; n += n) {
15+
int time = DoublingTest::timeTrial(n);
16+
cout << "time: " << time << " microseconds" << endl;
17+
}
18+
}

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

ch1/32_DoublingRatio/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 "../head/DoublingTest.h"
3+
4+
using namespace std;
5+
6+
/**
7+
* Prints table of running times to call {@code ThreeSum.count()}
8+
* for arrays of size 250, 500, 1000, 2000, and so forth, along
9+
* with ratios of running times between successive array sizes.
10+
*
11+
* @param args the command-line arguments
12+
*/
13+
int main() {
14+
int pre = DoublingTest::timeTrial(125);
15+
for (int n = 250; n <= 4000; n += n) {
16+
int time = DoublingTest::timeTrial(n);
17+
cout << "ratio: " << time / pre << " ratio" << endl;
18+
pre = time;
19+
}
20+
}

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

ch1/33_QuickFindUF/main.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <iostream>
2+
#include <fstream>
3+
#include "../head/QuickFindUF.h"
4+
5+
using namespace std;
6+
7+
/**
8+
* Reads in a sequence of pairs of integers (between 0 and n-1) from standard input,
9+
* where each integer represents some site;
10+
* if the sites are in different components, merge the two components
11+
* and print the pair to standard output.
12+
*
13+
* @param args the command-line arguments
14+
*/
15+
int main() {
16+
fstream file("/home/ace/AceDev/C++/algorithm/ch1/data/tinyUF.txt");
17+
int n, p, q;
18+
file >> n;
19+
QuickFindUF uf(n);
20+
while (file >> p >> q) {
21+
if (uf.connected(p, q)) continue;
22+
uf.union_op(p, q);
23+
cout << p << " " << q << endl;
24+
}
25+
cout << uf.getcount() << " components" << endl;
26+
}

ch1/CMakeLists.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,12 @@ add_subdirectory(21_LinkedQueue)
3131
add_subdirectory(22_Queue)
3232
add_subdirectory(23_ResizingArrayBag)
3333
add_subdirectory(24_LinkedBag)
34-
add_subdirectory(25_Bag)
34+
add_subdirectory(25_Bag)
35+
add_subdirectory(26_Stopwatch)
36+
add_subdirectory(27_StopwatchCPU)
37+
38+
add_subdirectory(29_ThreeSum)
39+
add_subdirectory(30_ThreeSumFast)
40+
add_subdirectory(31_DoublingTest)
41+
add_subdirectory(32_DoublingRatio)
42+
add_subdirectory(33_QuickFindUF)

0 commit comments

Comments
 (0)