Hi ,
Thanks for the amazing discussion .I thought this is done .
Well now that issue is over now , I am pointing to the correct location now .
But here is the issue now .
I am having an issue , as kbw said sizeoff() is not working .i looked around and i apperantly you cant use sizeoff for a pointer array , make sense for sure , but not helping .
So i user a global variable .
My problem is that when i call QuickSort , the array size is not passed correctly .I need to call quicksort and pass it the left and right side of the array .
As you can see in my code
int arraysize=globalArraySize;
will always set that to 8 , and basically i cant terminate my recursive call .
I have to only pass array , was thinking of adding the size at the end , but i was hopping for a better way to do that.
Again thanks to all who participated here , amazing info as usual.
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
|
#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;
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) {
int *array = (int*)a;
int arraysize=globalArraySize;
int leftRec=0;
int rightRec=arraysize-1;
int pivot =array[int(arraysize/2)];
int leftSize=arraysize-int(arraysize/2);
int rightSize=arraysize-leftSize;
//cout << " left is " << leftSize << "right is " << rightSize << endl;
if (arraysize<=1){
return 0;
}
while (leftRec < rightRec){
while ( array[leftRec] <pivot){
++leftRec;
}
while ( array[rightRec] >pivot){
--rightRec;
}
int temp=array[leftRec];
array[leftRec]=array[rightRec];
array[rightRec]=temp;
}
_beginthreadex(NULL, 0, QuickSort, array, 0, 0);
return 0;
}
int main () {
int* list;
list=readFile();
_beginthreadex(NULL, 0, QuickSort, list, 0, 0);
system ("pause");
return 0;
}
|