Browse Source

first commit

Rafael Arce Nazario 5 years ago
commit
c18cf09a08
6 changed files with 454 additions and 0 deletions
  1. 20
    0
      Makefile
  2. 60
    0
      insert.cpp
  3. 109
    0
      matrixMult.cpp
  4. 122
    0
      nHighest.cpp
  5. 66
    0
      search.cpp
  6. 77
    0
      sorting.cpp

+ 20
- 0
Makefile View File

@@ -0,0 +1,20 @@
1
+all: insert search matrixMult nHighest sorting
2
+
3
+sorting: sorting.cpp
4
+	g++ -o sorting sorting.cpp -std=c++11
5
+
6
+nHighest: nHighest.cpp
7
+	g++ -o nHighest nHighest.cpp -std=c++11
8
+
9
+matrixMult: matrixMult.cpp
10
+	g++ -o matrixMult matrixMult.cpp -std=c++11
11
+
12
+search: search.cpp
13
+	g++ -o search search.cpp -std=c++11
14
+
15
+insert: insert.cpp
16
+	g++ -o insert insert.cpp -std=c++11
17
+
18
+
19
+clean: 
20
+	rm insert search matrixMult nHighest sorting

+ 60
- 0
insert.cpp View File

@@ -0,0 +1,60 @@
1
+// Compare the execution times of inserting at the front vs. inserting
2
+// at the back of a vector.
3
+//
4
+// Usage:
5
+//    ./<exec name> <size of list>
6
+
7
+#include <iostream>
8
+#include <vector>
9
+#include <cstdlib>
10
+#include <algorithm>
11
+using namespace std;
12
+
13
+
14
+void insertManyFront(vector<int> & v, int size) {
15
+	for (int i = 0; i < size; i++) 
16
+    	v.insert(v.begin(), rand() % size);
17
+}
18
+
19
+void insertManyBack(vector<int> & v, int size) {
20
+	for (int i = 0; i < size; i++) 
21
+    	v.push_back(rand() % size);
22
+}
23
+
24
+int main(int argc, char *argv[]) {
25
+  
26
+  if (argc < 2) { 
27
+  	cout << "Usage: " << argv[0] << " <size> \n"; 
28
+    exit(1);
29
+  }
30
+  int size = atoi(argv[1]);
31
+  
32
+  vector<int> v,w;
33
+  
34
+  // initialize the random seed
35
+  srand(time(NULL));
36
+  
37
+  
38
+  double elapsed_secs;
39
+
40
+  // measure time to insert many elements in front
41
+  clock_t begin = clock();
42
+	insertManyFront(v, size);
43
+  clock_t end = clock();
44
+
45
+  elapsed_secs = static_cast<double>(end - begin) / CLOCKS_PER_SEC;
46
+  cout << "\nInsert at front elapsed: " << elapsed_secs << " seconds\n";
47
+  
48
+  // measure time to insert many elements in the back
49
+  begin = clock();
50
+  insertManyBack(w, size);
51
+  end = clock();
52
+
53
+  elapsed_secs = static_cast<double>(end - begin) / CLOCKS_PER_SEC;
54
+  cout << "Insert back elapsed: " << elapsed_secs << " seconds\n\n";
55
+  
56
+  return 0;
57
+}
58
+
59
+
60
+

+ 109
- 0
matrixMult.cpp View File

@@ -0,0 +1,109 @@
1
+// Compare the execution times of multiplying a matrix by a vector vs
2
+// a matrix by a matrix.
3
+//
4
+// Usage:
5
+//    ./<exec name> <size of list>
6
+
7
+#include <iostream>
8
+#include <vector>
9
+#include <cstdlib>
10
+#include <algorithm>
11
+
12
+typedef unsigned int uint;
13
+using namespace std;
14
+
15
+const int SIZE = 20000;
16
+
17
+void vectorMatrixMult(const vector< vector <int> > & M, const vector<int> & y, vector<int> &res) {
18
+	res.resize(y.size());
19
+  for (int r = 0; r < M.size(); r++) {
20
+    	int sum = 0;
21
+    	for (int c = 0; c < y.size(); c++) {
22
+        	sum = sum + (M[r][c] * y[c]);
23
+        }
24
+      	res[r] = sum; 
25
+    }
26
+}
27
+
28
+void matrixMatrixMult(const vector< vector <int> > & A, 
29
+  const vector< vector <int> > & B, vector< vector <int> > & res) {
30
+  
31
+  res.resize(A.size());
32
+  for (auto &r: res) {
33
+    r.resize(B[0].size());
34
+  }
35
+
36
+  for (uint cb = 0; cb < B[0].size(); cb++) {
37
+    for (uint ra = 0; ra < A.size(); ra++) {
38
+      int sum = 0;
39
+      for (uint ca = 0; ca < A[0].size(); ca++) {
40
+        // printf("ra ca cb: %d %d %d\n",ra,ca,cb);
41
+          sum = sum + (A[ra][ca] * B[ca][cb]);
42
+      }
43
+      res[ra][cb] = sum; 
44
+    }
45
+  }
46
+}
47
+
48
+
49
+
50
+void fillMatrix(vector< vector <int> > & M, uint size) {
51
+  M.resize(size);
52
+  for (uint r = 0; r < size; r++) {
53
+    M[r].resize(size);	
54
+    for (uint c = 0; c < size; c++) {
55
+        	M[r][c] = rand() % size;
56
+        }
57
+    }	
58
+}
59
+
60
+void fillVector(vector<int> & V, int size) {
61
+  V.resize(size);
62
+  for (int r = 0; r < size; r++) {
63
+       V[r]= rand() % size;
64
+  }	
65
+}
66
+
67
+
68
+int main(int argc, char *argv[]) {
69
+  
70
+  if (argc < 2) { 
71
+  	cout << "Usage: " << argv[0] << " <size> \n"; 
72
+    exit(1);
73
+  }
74
+  int size = atoi(argv[1]);
75
+  
76
+  vector< vector<int> > M;
77
+  vector <int> x, y;
78
+  
79
+  srand(time(NULL));
80
+  
81
+  
82
+  fillMatrix(M,size);
83
+  fillVector(y,size);
84
+  
85
+
86
+  double elapsed_secs;
87
+
88
+  // measure vector matrix mult time
89
+  clock_t begin = clock();
90
+  vectorMatrixMult(M, y, x);
91
+  clock_t end = clock();
92
+
93
+  elapsed_secs = static_cast<double>(end - begin) / CLOCKS_PER_SEC;
94
+  cout << "\nTime elapsed for vector matrix mult: " << elapsed_secs << " seconds\n";
95
+  
96
+  // measure matrix matrix mult time
97
+  vector < vector <int> > R;
98
+  begin = clock();
99
+  matrixMatrixMult(M,M,R);
100
+  end = clock();
101
+
102
+  elapsed_secs = static_cast<double>(end - begin) / CLOCKS_PER_SEC;
103
+  cout << "Time elapsed for matrix matrix mult: " << elapsed_secs << " seconds\n\n";
104
+    
105
+  return 0;
106
+}
107
+
108
+
109
+

+ 122
- 0
nHighest.cpp View File

@@ -0,0 +1,122 @@
1
+// Measure execution times of two algorithms for computing 
2
+// the 10 largest numbers in a list.
3
+// 
4
+// Must compile using the -std=c++11 option.
5
+//
6
+// Usage:
7
+//    ./<exec name> <size of list>
8
+
9
+#include <iostream>
10
+#include <vector>
11
+#include <cstdlib>
12
+#include <algorithm>
13
+#include <queue>
14
+
15
+using namespace std;
16
+
17
+//
18
+// Given a vector V and an integer n, copy the n largest 
19
+// numbers to rest.  This algorithm uses sorting.
20
+//
21
+void nHighest(vector <float> &V, int n, vector <float> &res) {
22
+  sort(V.begin(), V.end());
23
+  res.resize(n);
24
+  copy(V.begin()+V.size()-n, V.end(), res.begin());
25
+}
26
+
27
+
28
+//
29
+// Given a vector V and an integer n, copy the n largest 
30
+// numbers to rest.  This algorithm uses a priority queue.
31
+//
32
+void nHighestWithPQ(const vector <float> &V, int n, vector <float> &res) {
33
+  res.resize(0);
34
+  priority_queue<float, std::vector<float> , std::greater<float>> q;
35
+
36
+  // put the first n+1 elements into the queue
37
+  for (int i = 0; i <= n; i++) q.push(V[i]);
38
+  
39
+  // for the rest, if they are higher than the smallest in the queue,
40
+  // then enqueue
41
+  for (int i = n+1; i < V.size(); i++) {
42
+    if (V[i] > q.top()) {
43
+  	  q.pop();       // pop the 11th largest element
44
+      q.push(V[i]);  // push one element
45
+    }
46
+  }
47
+  q.pop();
48
+
49
+  while (!q.empty()) {
50
+    res.push_back(q.top());
51
+    q.pop();
52
+  }
53
+}
54
+
55
+
56
+
57
+int main(int argc, char *argv[]) {
58
+  
59
+  // get the command line argument
60
+  if (argc < 2) { 
61
+  	cout << "Usage: " << argv[0] << " <size> \n"; 
62
+    exit(1);
63
+  }
64
+  int size = atoi(argv[1]);
65
+  
66
+
67
+  vector<float> v,w;
68
+  
69
+    // fill the v vector with random integers
70
+  for (int i = 0; i < size; i++) 
71
+  	v.push_back(rand() / static_cast<float>(RAND_MAX));
72
+  
73
+  // copy v to w
74
+  w = v;
75
+  
76
+  // initialize the random seed
77
+  srand(time(NULL));
78
+  
79
+  vector<float> res1, res2;
80
+  
81
+  double elapsed_secs;
82
+
83
+  // measure the time for the sorting version
84
+  clock_t begin = clock();
85
+  nHighest(v, 10, res1);
86
+  clock_t end = clock();
87
+
88
+  elapsed_secs = static_cast<double>(end - begin) / CLOCKS_PER_SEC;
89
+  cout << "\nAlgorithm A: " << elapsed_secs << " seconds\n";
90
+
91
+  // measure the time for the priority queue version
92
+  begin = clock();
93
+  nHighestWithPQ(w, 10, res2);
94
+  end = clock();
95
+
96
+  elapsed_secs = static_cast<double>(end - begin) / CLOCKS_PER_SEC;
97
+  cout << "Algorithm B: " << elapsed_secs << " seconds\n";
98
+
99
+  #ifdef DEBUG  
100
+  cout << "Results for algorithm A:\n";
101
+  for (auto e: res1) cout << e <<" ";
102
+  cout << '\n';
103
+
104
+  cout << "Results for algorithm B:\n";
105
+  for (auto e: res1) cout << e <<" ";
106
+  cout << '\n';
107
+  #endif 
108
+  
109
+  if (res1 == res2) 
110
+    cout << "Congratulations. Both algorithms had the ame results!\n\n";
111
+  else              
112
+    cout << "Bad new. The algorithms obtained different results :-(\n";
113
+  
114
+  return 0;
115
+}
116
+
117
+//  125000
118
+//  250000
119
+//  500000
120
+// 1000000
121
+
122
+

+ 66
- 0
search.cpp View File

@@ -0,0 +1,66 @@
1
+// Measure execution times of binary search and linear search
2
+// on a sorted list.
3
+//
4
+// Usage:
5
+//    ./<exec name> <size of list>
6
+
7
+#include <iostream>
8
+#include <vector>
9
+#include <cstdlib>
10
+#include <algorithm>
11
+using namespace std;
12
+
13
+int main(int argc, char *argv[]) {
14
+  
15
+  // get the command line arguments
16
+  if (argc < 2) { 
17
+  	cout << "Usage: " << argv[0] << " <size> \n"; 
18
+    exit(1);
19
+  }
20
+  int size = atoi(argv[1]);
21
+  
22
+  vector<int> v; 
23
+  
24
+  // fill the v vector with random integers and then sort
25
+  for (int i = 0; i < size; i++) v.push_back(rand());
26
+  
27
+  sort(v.begin(), v.end());
28
+  
29
+  // initialize the random seed
30
+  srand(time(NULL));
31
+  
32
+  int search_item;  
33
+  double elapsed_secs;
34
+
35
+  clock_t begin = clock();
36
+  
37
+  // do a 100k searches using binary search
38
+  for (int i = 0; i < 100000; i++) {
39
+    search_item = rand();
40
+    binary_search(v.begin(), v.end(), search_item );
41
+  }
42
+    
43
+  clock_t end = clock();
44
+
45
+  
46
+  elapsed_secs = static_cast<double>(end - begin) / CLOCKS_PER_SEC;
47
+  cout << "\nBinary search: " << elapsed_secs << " seconds\n";
48
+  
49
+  begin = clock();
50
+  
51
+  // do 1000 searches using linear find. 
52
+  for (int i = 0; i < 1000; i++) {
53
+    search_item = rand();
54
+    find(v.begin(), v.end(), search_item);
55
+  }
56
+
57
+  end = clock();
58
+
59
+  elapsed_secs = static_cast<double>(end - begin) / CLOCKS_PER_SEC;
60
+  cout << "Linear search: " << elapsed_secs << " seconds\n\n";
61
+    
62
+  return 0;
63
+}
64
+
65
+
66
+

+ 77
- 0
sorting.cpp View File

@@ -0,0 +1,77 @@
1
+// Measure execution times of bubble sort vs. the STL implementation
2
+// of sort.
3
+//
4
+// Usage:
5
+//    ./<exec name> <size of list>
6
+
7
+#include <iostream>
8
+#include <vector>
9
+#include <cstdlib>
10
+#include <algorithm>
11
+using namespace std;
12
+
13
+
14
+//
15
+// Given a vector if ints, bubble sort it.
16
+//
17
+void bubbleSort(vector<int> & arr) {
18
+  int i, j;
19
+  int n = arr.size();
20
+  for (i = 0; i < n-1; i++)  {    
21
+    // Last i elements are already in place   
22
+    for (j = 0; j < n-i-1; j++) {
23
+      if (arr[j] > arr[j+1])
24
+        swap(arr[j], arr[j+1]);
25
+    }
26
+  }
27
+}
28
+
29
+
30
+int main(int argc, char *argv[]) {
31
+  
32
+  if (argc < 2) { 
33
+  	cout << "Usage: " << argv[0] << " <size> \n"; 
34
+    exit(1);
35
+  }
36
+  int size = atoi(argv[1]);
37
+  
38
+  vector<int> v,w;
39
+  
40
+  // initialize the random seed
41
+  srand(time(NULL));
42
+  
43
+  // fill the v vector with random integers
44
+  for (unsigned int i = 0; i < size; i++) 
45
+  	v.push_back(rand() % size);
46
+  
47
+  // copy v to w
48
+  w = v;
49
+  
50
+  double elapsed_secs;
51
+
52
+  // measure time to bubble sort
53
+  clock_t begin = clock();
54
+  bubbleSort(v);
55
+  clock_t end = clock();
56
+
57
+  elapsed_secs = static_cast<double>(end - begin) / CLOCKS_PER_SEC;
58
+  cout << "\nTime elapsed for Bubblesort: " << elapsed_secs << " seconds\n";
59
+  
60
+  // measure time to sort using the STL sort algorithm
61
+  begin = clock();
62
+  sort(w.begin(),w.end());
63
+  end = clock();
64
+
65
+  elapsed_secs = static_cast<double>(end - begin) / CLOCKS_PER_SEC;
66
+  cout << "Time elapsed for STL sort: " << elapsed_secs << " seconds\n";
67
+  
68
+  if ( is_sorted(v.begin(),v.end()) && is_sorted(w.begin(),w.end()) ) 
69
+    cout << "Congrats! Both vectors were properly sorted!\n\n";
70
+  else 
71
+    cout << "At least one of the algorithms returned an unsorted vector.\n\n"; 
72
+  
73
+  return 0;
74
+}
75
+
76
+
77
+