1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
//Michael Ervin - Multi-Threaded sorting
#include <iostream>
#include <fstream>
#include <iomanip>
#include <ctime>
#include <Windows.h>
#include <process.h>
using namespace std;
const int MAXARRAY = 10000;
double *staticarr;
ofstream fout("output.txt");
CRITICAL_SECTION sync;
unsigned __stdcall thread1( void *arg );
unsigned __stdcall thread2( void *arg );
unsigned __stdcall thread3( void *arg );
unsigned __stdcall thread4( void *arg );
unsigned __stdcall thread5( void *arg );
void bubblesort(double[], int);
void selectsort(double[], int);
void insertionsort(double[], int);
void quicksort(double [], int, int);
void shellsort(double temp[], int n);
void input(double[]);
void initarr(double[], double[]);
void print(double[], ofstream&, double);
void swaparr(double&, double&);
int main()
{
clock_t tstart, tend;
HANDLE hs[5];
unsigned th1, th2, th3, th4, th5;
fout << fixed << setprecision(3);
InitializeCriticalSection(&sync);
staticarr = new double[MAXARRAY];
input(staticarr);
cout << "Sorting data using five methods.\n" <<
"1) Bubble Sort.\n" <<
"2) Select Sort.\n" <<
"3) Insertion Sort.\n" <<
"4) Quick Sort.\n" <<
"5) Shell Sort." <<endl;
fout << "Starting multi-threaded sorting>>>>>" << endl << endl;
tstart = clock();
hs[0] = (HANDLE)_beginthreadex(NULL, 0, thread1, NULL, CREATE_SUSPENDED, &th1);
hs[1] = (HANDLE)_beginthreadex(NULL, 0, thread2, NULL, CREATE_SUSPENDED, &th2);
hs[2] = (HANDLE)_beginthreadex(NULL, 0, thread3, NULL, CREATE_SUSPENDED, &th3);
hs[3] = (HANDLE)_beginthreadex(NULL, 0, thread4, NULL, CREATE_SUSPENDED, &th4);
hs[4] = (HANDLE)_beginthreadex(NULL, 0, thread5, NULL, CREATE_SUSPENDED, &th5);
ResumeThread(hs[0]);
ResumeThread(hs[1]);
ResumeThread(hs[2]);
ResumeThread(hs[3]);
ResumeThread(hs[4]);
WaitForMultipleObjects(5, hs, true, INFINITE);
tend = clock();
fout << endl << "Total processing time: " << ((tend - tstart) / (double)CLOCKS_PER_SEC);
fout.close();
cin.get();
return 0;
}
unsigned __stdcall thread1( void *arg )
{
clock_t start, end;
double timediff;
double* x1 = new double[MAXARRAY];
EnterCriticalSection(&sync);
initarr(staticarr, x1);
LeaveCriticalSection(&sync);
start = clock();
bubblesort(x1, MAXARRAY);
end = clock();
timediff = (end - start)/(double)CLOCKS_PER_SEC;
EnterCriticalSection(&sync);
fout << "Bubble Sort N = " << MAXARRAY << endl;
print(x1, fout, timediff);
LeaveCriticalSection(&sync);
return 3;
}
unsigned __stdcall thread2( void *arg )
{
clock_t start, end;
double timediff;
double* x2 = new double[MAXARRAY];
EnterCriticalSection(&sync);
initarr(staticarr, x2);
LeaveCriticalSection(&sync);
start = clock();
selectsort(x2, MAXARRAY);
end = clock();
timediff = (end - start)/(double)CLOCKS_PER_SEC;
EnterCriticalSection(&sync);
fout << "Select Sort N = " << MAXARRAY << endl;
print(x2, fout, timediff);
LeaveCriticalSection(&sync);
return 3;
}
unsigned __stdcall thread3( void *arg )
{
clock_t start, end;
double timediff;
double* x3 = new double[MAXARRAY];
EnterCriticalSection(&sync);
initarr(staticarr, x3);
LeaveCriticalSection(&sync);
start = clock();
insertionsort(x3, MAXARRAY);
end = clock();
timediff = (end - start)/(double)CLOCKS_PER_SEC;
EnterCriticalSection(&sync);
fout << "Insertion Sort N = " << MAXARRAY << endl;
print(x3, fout, timediff);
LeaveCriticalSection(&sync);
return 3;
}
unsigned __stdcall thread4( void *arg )
{
clock_t start, end;
double timediff;
double* x4 = new double[MAXARRAY];
EnterCriticalSection(&sync);
initarr(staticarr, x4);
LeaveCriticalSection(&sync);
start = clock();
quicksort(x4,0, MAXARRAY - 1);
end = clock();
timediff = (end - start)/(double)CLOCKS_PER_SEC;
EnterCriticalSection(&sync);
fout << "Quick Sort N = " << MAXARRAY << endl;
print(x4, fout, timediff);
LeaveCriticalSection(&sync);
return 3;
}
unsigned __stdcall thread5( void *arg )
{
clock_t start, end;
double timediff;
double* x5 = new double[MAXARRAY];
[code]EnterCriticalSection(&sync);
initarr(staticarr, x5);
LeaveCriticalSection(&sync);
start = clock();
shellsort(x5, MAXARRAY);
end = clock();
timediff = (end - start)/(double)CLOCKS_PER_SEC;
EnterCriticalSection(&sync);
fout << "Shell Sort N = " << MAXARRAY << endl;
print(x5, fout, timediff);
LeaveCriticalSection(&sync);
return 3;
}
| |