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
|
#include <iostream>
#include <fstream>
#include <windows.h>
#include <process.h>
#include <time.h>
#include <string>
#include <sstream>
#include <string>
#include <array>
using namespace std;
int globalArraySize=0;
struct ThreadInfo
{
int *list;
int size;
bool run;
int *pLeft;
int *pRight;
};
int * readFile(){
int* array1 ;
int arraysize;
int i=0;
ifstream myfile ("input.txt");
if (myfile.is_open())
{
myfile >> arraysize;
array1= new int[arraysize];
while ( myfile.good() )
{
myfile >> array1[i++];
}
myfile.close();
}
else cout << "Unable to open file";
globalArraySize=arraysize;
return array1;
}
unsigned __stdcall QuickSort(void* a) {
ThreadInfo *info = (ThreadInfo*)a;
//int arraysize=_msize(array) / sizeof(int);
int *array=info->list;
cout << " First element is " << array[0] <<" last element is " << array[info->size-1]<< endl;
int pivot =array[int((info->size)/2)];
int leftSize=int((info->size)/2);
int rightSize=(info->size)-leftSize-1;
//cout << "Full Size is " << info->size << endl;
//cout << "Right size is : " << rightSize << endl;
if (info->size<=1){
info->run = false;
return 0;
}
while (info->pLeft < info->pRight){
while ( *info->pLeft < pivot){
++info->pLeft;
}
while ( *info->pRight > pivot){
--info->pRight;
}
int temp=*info->pLeft;
*info->pLeft=*info->pRight;
*info->pRight=temp;
}
//cout << " First element in the array after loop is : " << info->list[0] << endl;
ThreadInfo *arrLeft =new ThreadInfo;
arrLeft->list=info->list;
arrLeft->pLeft=info->list;
//cout << "First element is the left array is : " <<arraarrLeft->pLeft << endl;
arrLeft->pRight=&info->list[info->size/2];
arrLeft->size=leftSize;
arrLeft->run=true;
ThreadInfo *arrRight=new ThreadInfo;
arrRight->list=arrLeft->pRight+1;
arrRight->pLeft=arrLeft->pRight+1;
arrRight->pRight=&info->list[info->size-1];
arrRight->size=rightSize;
arrRight->run=true;
_beginthreadex(NULL, 0, QuickSort, arrLeft, 0, 0);
_beginthreadex(NULL, 0, QuickSort, arrRight, 0, 0);/*
bool anyRun = true;
while(anyRun)
{
anyRun = false;
for(int i = 0; i < 8; i++)
{
anyRun =anyRun || arrLeft[i].run || arrRight[i].run;
}
}*/
info->run = false;
return 0;
}
int main () {
//int* list;
ThreadInfo info;
//Read the input.text file , and pass an array , that contain the values in that text file .
info.list =readFile() ;
info.run = true;
info.pLeft=&info.list[0];
info.pRight=&info.list[globalArraySize-1];
info.size=globalArraySize;
//Start my thread , passing the array passed from readFile function.
_beginthreadex(NULL, 0, QuickSort, &info, 0, 0);
// while(info.run){}
Sleep(9000);
cout << "array elements are " ;
for ( int i=0;i<=7;i++){
cout <<" , " <<info.list[i];
}
cout << endl;
system ("pause");
return 0;
}
|