Skip to content

Commit 301bd25

Browse files
first commit
0 parents  commit 301bd25

File tree

5 files changed

+369
-0
lines changed

5 files changed

+369
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/.vscode
2+
/main
3+
/main_single
4+
/thread_algo

main.cc

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#include <iostream>
2+
#include <pthread.h>
3+
#include <time.h>
4+
#include "thread_algo.h"
5+
using std::cout;
6+
7+
vector<int> inputvect;
8+
int sz = 50; // input size
9+
10+
typedef struct Data
11+
{
12+
vector<int> left;
13+
vector<int> right;
14+
} sort_thread;
15+
16+
// function for sorting threads
17+
void *Sort(void *arg)
18+
{
19+
20+
vector<int> thread_vect = *((vector<int> *)arg);
21+
22+
int threshold = 10;
23+
int size = thread_vect.size();
24+
if (size <= threshold)
25+
selectionSort(*((vector<int> *)arg));
26+
27+
else
28+
{
29+
//mergeSort(*((vector<int> *)arg), 0, size - 1);
30+
quickSort(*((vector<int> *)arg), 0, size - 1);
31+
}
32+
return NULL;
33+
}
34+
35+
// function for merging thread
36+
void *merge_thread(void *arg)
37+
{
38+
Data sort_vect = *((Data *)arg);
39+
vector<int> left = sort_vect.left;
40+
vector<int> right = sort_vect.right;
41+
inputvect.clear();
42+
merge(inputvect, left, right);
43+
return NULL;
44+
}
45+
46+
int main()
47+
{
48+
clock_t t1, t2; // clock variable to compute time
49+
50+
// creating random vector
51+
for (int i = 0; i < sz; i++)
52+
{
53+
inputvect.push_back(rand() % 100);
54+
}
55+
cout << "unsorted array is: \n";
56+
for (int i = 0; i < sz; i++)
57+
cout << inputvect[i] << " ";
58+
cout << "\n";
59+
60+
// distribute the main VECTOR in two parts for each thread
61+
vector<int> leftthread, rightthread;
62+
int mid = (int)sz / 2;
63+
cout << "mid:" << mid << "\n";
64+
for (int i = 0; i < mid; i++)
65+
leftthread.push_back(inputvect[i]);
66+
for (int i = mid; i < sz; i++)
67+
rightthread.push_back(inputvect[i]);
68+
69+
t1 = clock(); // start time calculation (only thread execution is considered)
70+
pthread_t sthread0, sthread1; // declaring sorting threads
71+
pthread_create(&sthread0, NULL, Sort, &leftthread); // creating sorting threads
72+
pthread_create(&sthread1, NULL, Sort, &rightthread);
73+
if (sthread0 < 0 || sthread0 < 0) // checking for thread creation success
74+
{
75+
std::cerr << "error in sort thread creation";
76+
}
77+
else
78+
{
79+
// providing a join so that above two threads i.e. sorting threads
80+
// so that we have sorted vectors before executing merge thread
81+
pthread_join(sthread1, NULL);
82+
pthread_t mergethread;
83+
// storing left and right sorted vector
84+
// in struct for passing to merge thread
85+
Data Sort_thread = {leftthread, rightthread};
86+
pthread_create(&mergethread, NULL, merge_thread, &Sort_thread); // creating merge thread
87+
if (mergethread < 0) // checking for thread creation success
88+
{
89+
std::cerr << "error in mergethread creation";
90+
}
91+
else
92+
{
93+
pthread_join(mergethread, NULL); // providing a join to wait for merge thread execution
94+
}
95+
}
96+
t2 = clock(); // end time calculation
97+
cout << "Sorted Array \n";
98+
for (int i = 0; i < sz; i++)
99+
cout << inputvect[i] << " ";
100+
cout << "\n";
101+
102+
cout << "Time Taken=" << (t2 - t1);
103+
104+
return 0;
105+
}

main_single.cc

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <iostream>
2+
#include <time.h>
3+
#include <pthread.h>
4+
#include "thread_algo.h"
5+
using std::cout;
6+
7+
void *singleThread_sort(void *arg)
8+
{
9+
vector<int> vect = *((vector<int> *)arg);
10+
quickSort(*((vector<int> *)arg), 0, vect.size() - 1);
11+
return NULL;
12+
}
13+
14+
int main()
15+
{
16+
vector<int> inputvect;
17+
int sz = 50;
18+
clock_t t1, t2;
19+
for (int i = 0; i < sz; i++)
20+
{
21+
inputvect.push_back(rand() % 100);
22+
}
23+
cout << "unsorted array is: \n";
24+
for (int i = 0; i < sz; i++)
25+
cout << inputvect[i] << " ";
26+
cout << "\n";
27+
t1 = clock();
28+
pthread_t singlethread; // single thread creation to compare performance
29+
30+
pthread_create(&singlethread, NULL, singleThread_sort, &inputvect);
31+
if (singlethread < 0)
32+
{
33+
std::cerr << "thread creation failed";
34+
exit(1);
35+
}
36+
pthread_join(singlethread, NULL);
37+
38+
t2 = clock();
39+
cout << "Sorted Array \n";
40+
for (int i = 0; i < sz; i++)
41+
cout << inputvect[i] << " ";
42+
cout << "\n";
43+
cout << "Time Taken=" << (t2 - t1);
44+
return 0;
45+
}

thread_algo.cc

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
2+
#include "thread_algo.h"
3+
4+
void swap(int &xp, int &yp)
5+
{
6+
int temp = xp;
7+
xp = yp;
8+
yp = temp;
9+
}
10+
11+
// merge function overloading to merge two sorted thread results
12+
void merge(vector<int> &input, vector<int> &leftthread, vector<int> &rightthread)
13+
{
14+
int indexLeft = 0, // Initial index of left vector
15+
indexRight = 0, // Initial index of rightvector
16+
indexMerged = 0; // Initial index of merge vector
17+
18+
// Merge the sorted threads results back into original input while sorting
19+
while (indexLeft < leftthread.size() && indexRight < rightthread.size())
20+
{
21+
if (leftthread[indexLeft] <= rightthread[indexRight])
22+
{
23+
input[indexMerged] = leftthread[indexLeft];
24+
indexLeft++;
25+
}
26+
else
27+
{
28+
input[indexMerged] = rightthread[indexRight];
29+
indexRight++;
30+
}
31+
indexMerged++;
32+
}
33+
// Copy the remaining elements of left thread result, if left
34+
while (indexLeft < leftthread.size())
35+
{
36+
input[indexMerged] = leftthread[indexLeft];
37+
indexLeft++;
38+
indexMerged++;
39+
}
40+
// Copy the remaining elements of right thread result, if left
41+
while (indexRight < rightthread.size())
42+
{
43+
input[indexMerged] = rightthread[indexRight];
44+
indexRight++;
45+
indexMerged++;
46+
}
47+
}
48+
49+
// merge sort
50+
// merge funtion for merge Sort
51+
void merge(vector<int> &vector, int const left, int const mid,
52+
int const right)
53+
{
54+
int leftvectorSize = mid + 1 - left;
55+
int rightvectorSize = right - mid;
56+
57+
// temp vectors are created
58+
std::vector<int> leftvector, rightvector;
59+
60+
// Copy data to temp vectors from main vector
61+
for (int i = 0; i < leftvectorSize; i++)
62+
leftvector.push_back(vector[left + i]);
63+
64+
for (int j = 0; j < rightvectorSize; j++)
65+
rightvector.push_back(vector[mid + 1 + j]);
66+
67+
int indexLeft = 0, // Initial index of left vector
68+
indexRight = 0; // Initial index of rightvector
69+
int indexMerged = left; // Initial index of merge vector
70+
71+
// Merge the temp vector back into original vector while sorting
72+
while (indexLeft < leftvectorSize && indexRight < rightvectorSize)
73+
{
74+
if (leftvector[indexLeft] <= rightvector[indexRight])
75+
{
76+
vector[indexMerged] = leftvector[indexLeft];
77+
indexLeft++;
78+
}
79+
else
80+
{
81+
vector[indexMerged] = rightvector[indexRight];
82+
indexRight++;
83+
}
84+
indexMerged++;
85+
}
86+
// Copy the remaining elements of left vector , if left
87+
while (indexLeft < leftvectorSize)
88+
{
89+
vector[indexMerged] = leftvector[indexLeft];
90+
indexLeft++;
91+
indexMerged++;
92+
}
93+
// Copy the remaining elements of right vector, if left
94+
while (indexRight < rightvectorSize)
95+
{
96+
vector[indexMerged] = rightvector[indexRight];
97+
indexRight++;
98+
indexMerged++;
99+
}
100+
}
101+
// Divide the vector into two subvectors, sort them and merge them
102+
void mergeSort(vector<int> &vect, int l, int h)
103+
{
104+
if (l >= h)
105+
return; // Returns recursively
106+
107+
int mid = l + (h - l) / 2;
108+
mergeSort(vect, l, mid);
109+
mergeSort(vect, mid + 1, h);
110+
merge(vect, l, mid, h);
111+
}
112+
113+
// selction Sort :for input less than equal to 100
114+
void selectionSort(vector<int> &vect)
115+
{
116+
int min_idx;
117+
int n = vect.size();
118+
119+
// One by one move boundary of unsorted subvector
120+
for (int i = 0; i < (n - 1); i++)
121+
122+
{
123+
// Find the minimum element in unsorted subvector
124+
min_idx = i;
125+
for (int j = i + 1; j < n; j++)
126+
if (vect[j] < vect[min_idx])
127+
min_idx = j;
128+
129+
// Swap the found minimum element with the first element
130+
if (min_idx != i)
131+
swap(vect[min_idx], vect[i]);
132+
}
133+
}
134+
135+
// Quick sort
136+
// function to rearrange vector (find the partition point)
137+
int partition(vector<int> &vector, int low, int high)
138+
{
139+
140+
// select the rightmost element as pivot
141+
int pivot = vector[high];
142+
143+
// pointer for greater element
144+
int i = (low - 1);
145+
146+
// traverse each element of the vector
147+
// compare them with the pivot
148+
for (int j = low; j < high; j++)
149+
{
150+
if (vector[j] <= pivot)
151+
{
152+
153+
// if element smaller than pivot is found
154+
// swap it with the greater element pointed by i
155+
i++;
156+
157+
// swap element at i with element at j
158+
swap(vector[i], vector[j]);
159+
}
160+
}
161+
162+
// swap pivot with the greater element at i
163+
swap(vector[i + 1], vector[high]);
164+
165+
// return the partition point
166+
return (i + 1);
167+
}
168+
169+
void quickSort(vector<int> &vector, int low, int high)
170+
{
171+
if (low < high)
172+
{
173+
174+
// find the pivot element such that
175+
// elements smaller than pivot are on left of pivot
176+
// elements greater than pivot are on righ of pivot
177+
int pi = partition(vector, low, high);
178+
179+
// recursive call on the left of pivot
180+
quickSort(vector, low, pi - 1);
181+
182+
// recursive call on the right of pivot
183+
quickSort(vector, pi + 1, high);
184+
}
185+
}

thread_algo.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#ifndef __thread_algo__h
2+
#define __thread_algo__h
3+
#include <vector>
4+
#include<ostream>
5+
using std::vector;
6+
using std::ostream;
7+
8+
class intVector{
9+
public:
10+
intVector(){};
11+
intVector(int sz);
12+
bool operator<(const intVector &v);
13+
bool operator>(const intVector &v);
14+
bool operator==(const intVector &v);
15+
bool operator>=(const intVector &v);
16+
bool operator<=(const intVector &v);
17+
friend ostream& operator<<(ostream &os,const intVector&v );
18+
private:
19+
int size;
20+
vector<int> vect;
21+
};
22+
23+
void merge(vector<int> &arr, vector<int> &left, vector<int> &right);
24+
void swap(int &x, int &y);
25+
void merge(vector<int> &arr, int low, int mid, int high);
26+
void mergeSort(vector<int> &vect, int l, int h);
27+
void quickSort(vector<int> &vect, int l, int h);
28+
int partition(vector<int> &array, int low, int high);
29+
void selectionSort(vector<int> &arr);
30+
#endif

0 commit comments

Comments
 (0)