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
|
#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;
if (info->size<=1){
info->run = false;
return 0;
}
cout << " First element is " << array[0] <<endl;//<<" last element is " << array[info->size-1]<< endl;
srand(time(NULL));
int pivotIndex=(rand())%info->size;
cout << "Pivot Index is " << pivotIndex << endl;
int pivot =array[pivotIndex];
int *pPivot=&array[pivotIndex];
cout << " Pivot is " << pivot<< endl;
int leftSize=pivotIndex;
int rightSize=info->size-(pivotIndex)-1;
//cout << "Full Size is " << info->size << endl;
//cout << "Right size is : " << rightSize << endl;
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=pPivot-1;
arrLeft->size=leftSize;
arrLeft->run=true;
ThreadInfo *arrRight=new ThreadInfo;
arrRight->list=pPivot+1;
arrRight->pLeft=pPivot+1;
arrRight->pRight=&info->list[info->size-1];
arrRight->size=rightSize;
arrRight->run=true;
HANDLE t1=(HANDLE) _beginthreadex(NULL, 0, QuickSort, arrLeft, 0, 0);
HANDLE t2=(HANDLE)_beginthreadex(NULL, 0, QuickSort, arrRight, 0, 0);
/*
bool rightFlag = true;
bool leftFlag = true;
while(rightFlag && leftFlag)
{
rightFlag = false;
leftFlag=false;
for(int i = 0; i < 2; i++)
{
rightFlag =rightFlag || arrRight[i].run;
}
for(int i = 0; i < 2; i++)
{
leftFlag = leftFlag || arrLeft[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.
HANDLE main=(HANDLE) _beginthreadex(NULL, 0, QuickSort, &info, 0, 0);
WaitForMultipleObjects(1, &main, 0, 0);
// while(info.run){}
cout << "array elements are " ;
for ( int i=0;i<=7;i++){
cout <<" , " <<info.list[i];
}
cout << endl;
system ("pause");
return 0;
}
|