コード例 #1
0
int main(int argc, char* argv[]){
    int x[] = {9,9,9,8,9,8,7,9,8,8,8,9,8,9,8,8,6,9};
    int len = sizeof(x)/sizeof(x[0]);
   	vector<int> nums = getVector(x,len);
   	print(nums);
   	print(partitionArray(nums,9));
   	print(nums);
}
コード例 #2
0
ファイル: QuickSort.c プロジェクト: titu1994/QuickSort
void QuickSort(int *a, int left, int right){
	int newPivotIndex;
	int partitionArray(int *a, int left, int right, int pivot);

	if(left < right){

		newPivotIndex = partitionArray(a, left, right, (left + right)/2);

		QuickSort(a, left, newPivotIndex - 1);

		QuickSort(a, newPivotIndex + 1, right);

	}

}
コード例 #3
0
ファイル: psrs.c プロジェクト: qiuqiyuan/PSRS
void
phase3(int size, int rank, int nInts, int *privateInts, int *pivots,
    int **localPartitionSizes, int **partitionIndices, 
    int **incomingPartitionSizes, int ***partitions)
{
	MPI_Request *sendRequests, *recRequests;
	MPI_Status *sendStatuses, *recStatuses;

	int i;

	sendRequests = calloc(size, sizeof(MPI_Request));
	recRequests = calloc(size, sizeof(MPI_Request));
	sendStatuses = calloc(size, sizeof(MPI_Status));
	recStatuses = calloc(size, sizeof(MPI_Status));
	
	*localPartitionSizes = calloc(size, sizeof(int));
	*partitionIndices = calloc(size, sizeof(int));
	*incomingPartitionSizes = calloc(size, sizeof(int));
	*partitions = calloc(size, sizeof(int *));
	
	partitionArray(privateInts, pivots, *partitionIndices,
	    *localPartitionSizes, nInts, size);
#ifdef HELLO 
	printf("Hello, I am %d.  This is my privateInts array:\n", rank);
	for (i = 0; i < nInts / size; i++) {
		printf("%d: privateInts[%d] = %d\n", rank, i, privateInts[i]);
	}
	printf("Hello, I am %d.  These are the pivots I have:\n", rank);
	for (i = 0; i < size - 1; i++) {
		printf("%d: pivots[%d] = %d\n", rank, i, pivots[i]);
	}
	printf("Hello, I am %d.  These are my partition indices:\n", rank);
	for (i = 0; i < size; i++) {
		printf("%d: partitionIndices[%d] = %d\n", rank, i,
		    partitionIndices[i]);
	}	
	printf("Hello, I am %d.  These are my partition sizes:\n", rank);
	for (i = 0; i < size; i++) {
		printf("%d: localPartitionSizes[%d] = %d\n", rank, i,
		    localPartitionSizes[i]);
	}	
#endif

	/* 
	 * Each process shares with the other processes the size of the 
	 * partition it will be sending that process so that the receiving
	 * process can allocate buffers of appropriate sizes.
	 */
	MPI_Alltoall(*localPartitionSizes, 1, MPI_INT, *incomingPartitionSizes,
	    1, MPI_INT, MPI_COMM_WORLD);
	/* 
	 * Now that each process knows what size partition to expect, it can
	 * appropriately allocate buffers.
	 */
	for (i = 0; i < size; i++) {
		(*partitions)[i] = calloc((*incomingPartitionSizes)[i], 
		    sizeof(int));
	}
	
	/* We are now ready for the processes to exchange partitions. */
	for (i = 0; i < size; i++) {
		
		MPI_Irecv((*partitions)[i],(*incomingPartitionSizes)[i],MPI_INT,
		    i, 0, MPI_COMM_WORLD, &(recRequests[i]));
		
		if (i == 0) {
			MPI_Isend(privateInts,(*localPartitionSizes)[i],MPI_INT, 			    i, 0, MPI_COMM_WORLD, &(sendRequests[i]));
		} else {
			MPI_Isend(privateInts + (*partitionIndices)[i-1] + 1,
			    (*localPartitionSizes)[i], MPI_INT, i, 0,
			    MPI_COMM_WORLD, &(sendRequests[i]));
		}
	}
	MPI_Waitall(size, recRequests, recStatuses);
	MPI_Waitall(size, sendRequests, sendStatuses);


	free(*localPartitionSizes);
	free(privateInts);
	free(*partitionIndices);
	free(recRequests);
	free(sendRequests);
	free(recStatuses);
	free(sendStatuses);

#ifdef DEBUG1
	int j;
	for (i = 0; i < size; i++) {
		for (j = 0; j < incomingPartitionSizes[i]; j++) {
			printf("%d: partition[%d][%d] = %d\n", rank, i, j,
			    partitions[i][j]);
		}
	}
#endif
} /* Phase 3 complete */