コード例 #1
0
void 
Mergesort(int A[], int aLength)
{
  if(aLength > 1) {
    int i = 0;
    int bLength = aLength / 2;
    int cLength = aLength - bLength;
    int *B = NULL, *C=NULL;
    B = (int*)calloc(bLength, sizeof(int));
    C = (int*)calloc(cLength, sizeof(int));
    copy(B, 0, bLength-1, A, 0, aLength / 2 - 1);
    copy(C, 0, cLength-1, A, aLength / 2, aLength - 1);
    
    printf("B: ");
    for(i = 0; i < bLength; i++)
      printf("%2d", B[i]);
    printf("\n");

    printf("C: ");
    for(i = 0; i < cLength; i++)
      printf("%2d", C[i]);
    printf("\n");
    
    Mergesort(B, bLength);
    Mergesort(C, cLength);
    Merge(B, bLength, C, cLength, A, aLength);
  }
}
コード例 #2
0
ファイル: MSort.cpp プロジェクト: 218490/pamsi
/*!
 *\brief Mergesort na tablicy
 *
 *\param[in] tablica-struktura przechowujaca tablice dynamiczna
 *\param[in] lewy- lewa granica sortowania (numer indeksu)
 *\param[in] prawy- prawa granica sortowania (numer indeksu)
 */
void MSort::Mergesort(Tab &tablica,int lewy,int prawy){
  if(prawy<=lewy) return; 

  int srodek = (prawy+lewy)/2;
  Mergesort(tablica,lewy,srodek); 
  Mergesort(tablica,srodek+1,prawy);

  Scal(tablica,lewy,srodek,prawy);

}
コード例 #3
0
ファイル: mergesort.c プロジェクト: andersonsilvade/python_C
void Mergesort (int p, int r, int v[]) {
     if (p < r - 1) {
        int q = (p + r)/2;
        printf ("Mergesort(%2d, %2d, v)\n", p, q);
        printf ("Mergesort(%2d, %2d, v)\n", q, r);
        system ("pause");
        Mergesort (p, q, v);//cpu1
        Mergesort (q, r, v);//cpu2
        Intercala (p, q, r, v);
     }
}
コード例 #4
0
ファイル: mergesort.c プロジェクト: gauravkmr/C-codes
void Mergesort(int A[], int n, int m)
{
	if(n > m)
	{
		Mergesort(A + 0, (n+1)/2, m);
		Mergesort(A + (n+1)/2, n - (n+1)/2, m);
		Merge(A, 0, (n+1)/2 - 1, (n+1)/2, n-1);
	}
	else
	{
		SpecialMergeSort(A, n);
	}
}
コード例 #5
0
int main(int argc, char **argv)
{	// for test
	ElementType A[7] = { 12, 15, 123, 61, 2, 1, -1 };
	Mergesort(A, 7);

	system("pause");
	return 0;
}
コード例 #6
0
void Mergesort(int num, int* data)
{
    if(num==1)
        return;
    int Lhs = num/2;
    int Rhs = num-Lhs;
    int left[Lhs], right[Rhs];

    //Copy data to local array.(to left[] and right[])
    int index;
    for(index=0 ; index<Lhs ; ++index)
        left[index] = data[index];
    for(index=0 ; index<Rhs ; ++index)
        right[index] = data[index+Lhs];
    //After copy, then cut the array until the length of array = 1.
    Mergesort(Lhs, left), Mergesort(Rhs, right);
    //Then, start to merge the arrays.
    my_merge(Lhs, Rhs, left, right, data);
}
コード例 #7
0
void Mergesort(int a[], int num) {
	int i;
	int *left, *right;
	int nl, nr;

	if (num < 2)
		return;

	//divide array a[] into 2 arraies left &right
	nl = (num + 1) / 2;
	nr = num - nl;
	left  = (int *)malloc(nl * sizeof(int));
	right = (int *)malloc(nr * sizeof(int));

	//move half of elements to left, the rest of elements to right
	printf("Left subarray is :\n");
	for (i = 0; i < nl; i++) {
		left[i] = a[i];
	/*illustration*/
		printf("%d ", left[i]);
	}
	printf("\n");
	printf("Right subarray is: \n");
	for (i = 0; i < nr; i++) {
		right[i] = a[nl + i];
	/*illustration*/
		printf("%d ", right[i]);
	}
	printf("\n");

	//recursive call
	Mergesort(left, nl);//a[] is changed into subarray left[]
	Mergesort(right, nr);//a[] is changed into subarray right[]

	merge(left, nl, right, nr, a);
	free(left);
	free(right);
}
コード例 #8
0
main (int argc, char *argv[]) {
	int i;
	int array[] = {82, 49, 70, 83, 68, 44, 94, 40, 32, 10, 99, 59};
	//don't forget to update LEN when this array is modified

	if (argc == 1)
		Mergesort(array, LEN);
	else if (argv[1][1] == 'b')
		mergesortBU(array, LEN);
	else
		printf("Illegal option.\n");

	for (i = 0; i < LEN; i++)
		printf("%d ", array[i]);
	printf("\n");

	return 0;
}
コード例 #9
0
ファイル: mergesort.c プロジェクト: gauravkmr/C-codes
int main()
{
	int A[50];
	int n, m, i;

	printf("enter size:\n");
	scanf("%d", &n);

	printf("enter elements:\n");
	for(i = 0; i < n; i++)
		scanf("%d", &A[i]);

	printf("enter size of chunk:\n");
	scanf("%d", &m );

	Mergesort(A, n, m);
	
	for(i = 0; i < n; i++)
		printf("%d  ", A[i]);
}
コード例 #10
0
int main(){
  
  //String Merge Sort
  /* string_t A = createString(); */
  /* printf("The A String  is %s\n", A.string); */
  /* printf("The Length is %d\n", A.length); */
  /* Mergesort(A.string, A.length); */
  /* Mergesort(A); */
  
  //Number Merge Sort
  int A[] = {23, 24, 29, 8, 3, 2, 9, 7, 1, 5, 4};
  int aLength = 11;
  int i;

  Mergesort(A, aLength);

  printf("After Mergsort: ");
  for(i = 0; i < aLength; i++)
    printf("%2d ", A[i]);
  printf("\n");

  return 0;
}
コード例 #11
0
ファイル: integ.c プロジェクト: g1g19/codeforces
int main(void)
{
	unsigned long long i,x,count,k;
	count = 0;
	long long m;
	scanf("%llu",&n);
	A = (long long*)malloc(n*sizeof(long long));
	for(i=0;i<n;i++)
	{
		scanf("%lld",&A[i]);
	}
	Mergesort(A,n);
	scanf("%llu",&x);
	i = counter();
	if(i == 0 || x==0)
		printf("%d\n",0);
	else if(x>0 && x<i)
	{
		m = A[x-1];
		m = (-1)*m;
		count += (m*x);
		for(k=0;k<n;k++)
		{
			A[k] = A[k]+m;
		}
		m = countall();
		count += m;
		printf("%llu\n",count);
	}
	else
	{
		i = countall();
		printf("%llu\n",i);
	}
	return 0;
}
コード例 #12
0
int main (int argc, char *argv[]) {

	double all_start, all_end, all_total;
	double comm_start, comm_end, comm_total;
	double ior_start, ior_end, ior_total, iow_start, iow_end, iow_total, io_total;
	double compute_start, compute_end, compute_total;

	all_total = 0;
	comm_total = 0;
	io_total = 0;
	ior_total = 0;
	iow_total = 0;
	compute_total = 0;



	int i, j, k;
	//	Initial MPI environment
	/*
		rank: the ID of each process
		size: # total available process
	*/
	int rank, size;

	MPI_Init(&argc, &argv);

	all_start = MPI_Wtime();


	MPI_Comm_size(MPI_COMM_WORLD, &size);
	MPI_Comm_rank(MPI_COMM_WORLD, &rank);

	//	Argument exception handle
	if(argc < 4) {
		if (rank == ROOT) {
			fprintf(stderr, "Insufficient args\n");
			fprintf(stderr, "Usage: %s N input_file", argv[0]);
		}
		MPI_Barrier(MPI_COMM_WORLD);
		MPI_Finalize();
		return 0;
	}

	//	Argument assigned
	const int N = atoi(argv[1]);
	const char *input = argv[2];
	const char *output = argv[3];

	//	Part 1: Determine arguments of the I/O, and do MPI I/O
	/*
		num_per_node:	# number stored in each process
		rank_first_add:	the first process which need to add MAX_INT
		num_first_add:	# number that the first process which need to add
		read_file:		indicate the process if it need to read file
		*node_arr:		local number array of each process
	*/
	MPI_File ifh;
	MPI_Status istatus;	
	int num_per_node, rank_first_add, num_first_add, read_file, *node_arr;
	read_file=0;
	
	compute_start = MPI_Wtime();
	if(N<size){ //	N < #process
		num_per_node = 1;
		num_first_add = 1;
		rank_first_add = N;
		if(rank<rank_first_add)
			read_file = 1;
	}
	else{ //	N >= #process
		if(N%size){ //	If N can't be divided into the # process
			num_per_node = (N/size) + 1;
			rank_first_add = N/num_per_node; // # element to be add in the first rank
			num_first_add = num_per_node - (N%num_per_node); 
			if(rank<=rank_first_add)
				read_file = 1;
		}
		else{ //	If N can be divided into # process
			num_per_node = N/size;
			rank_first_add = size; // no need to add
			num_first_add = 0;
			read_file = 1;
		}
	}
	compute_end = MPI_Wtime();
	compute_total += (compute_end - compute_start);

	node_arr = (int*) malloc(num_per_node * sizeof(int)); // store the N/P numbers in each node
	
	ior_start = MPI_Wtime();
	MPI_File_open(MPI_COMM_WORLD, input, MPI_MODE_RDONLY, MPI_INFO_NULL, &ifh);
	if(read_file)
		MPI_File_set_view(ifh, rank*num_per_node*sizeof(int), MPI_INT, MPI_INT, "native", MPI_INFO_NULL);
	else
		MPI_File_set_view(ifh, 0, MPI_INT, MPI_INT, "native", MPI_INFO_NULL);
	if(read_file && rank!=rank_first_add)
		MPI_File_read_all(ifh, node_arr, num_per_node, MPI_INT, &istatus);
	else if(read_file && rank==rank_first_add){
		MPI_File_read_all(ifh, node_arr, (num_per_node-num_first_add), MPI_INT, &istatus);
		for(i=(num_per_node-num_first_add); i<num_per_node; ++i)
			node_arr[i] = MAX_INT;
	}
	else{
		MPI_File_read_all(ifh, node_arr, 0, MPI_INT, &istatus);
		for(i=0; i<num_per_node; ++i)
			node_arr[i] = MAX_INT;
	}
	MPI_File_close(&ifh);
	ior_end = MPI_Wtime();
	ior_total = (ior_end - ior_start);
	io_total += ior_total;

	MPI_Barrier(MPI_COMM_WORLD);

	// Part 2: Start odd-even sort algorithm
	MPI_Status status;
	int *next_arr, *merge_arr, *ori_arr;
	next_arr = (int*) malloc(num_per_node * sizeof(int));

	compute_start = MPI_Wtime();
	Mergesort(num_per_node, node_arr);
	compute_end = MPI_Wtime();
	compute_total += (compute_end - compute_start);

	MPI_Barrier(MPI_COMM_WORLD);
	for(i=0; i<size; ++i){
		if(i%2==0){ //	Even-phase
			if(rank%2){ //	Odd-rank process: # 1, 3, 5...(Sender) => P -> P-1
				
				comm_start = MPI_Wtime();
				MPI_Send(node_arr, num_per_node, MPI_INT, rank-1, 8, MPI_COMM_WORLD);
				comm_end = MPI_Wtime();
				comm_total += (comm_end - comm_start);

				comm_start = MPI_Wtime();
				MPI_Recv(node_arr, num_per_node, MPI_INT, rank-1, 8, MPI_COMM_WORLD, &status);
				comm_end = MPI_Wtime();
				comm_total += (comm_end - comm_start);
			}
			else{ // Even-rank process: # 0, 2, 4...(Receiver) => P-1 <- P
				if(rank!=size-1){
					
					comm_start = MPI_Wtime();
					MPI_Recv(next_arr, num_per_node, MPI_INT, rank+1, 8, MPI_COMM_WORLD, &status);
					comm_end = MPI_Wtime();
					comm_total += (comm_end - comm_start);

					compute_start = MPI_Wtime();
					merge_arr = (int*) malloc(num_per_node * 2 * sizeof(int));
					for(j=0; j<num_per_node; ++j){
						merge_arr[j] = node_arr[j];
						merge_arr[j+num_per_node] = next_arr[j];
					}
					Mergesort(num_per_node*2, merge_arr);
					for(j=0; j<num_per_node; ++j){
						node_arr[j] = merge_arr[j];
						next_arr[j] = merge_arr[j+num_per_node];
					}
					free(merge_arr);
					compute_end = MPI_Wtime();
					compute_total += (compute_end - compute_start);


					comm_start = MPI_Wtime();
					MPI_Send(next_arr, num_per_node, MPI_INT, rank+1, 8, MPI_COMM_WORLD);
					comm_end = MPI_Wtime();
					comm_total += (comm_end - comm_start);
				}
			}
		}
		else{ //	Odd-phase
			if(rank%2==0){ //	Even-rank process: # 0, 2, 4... (Sender) => Q -> Q-1
				if(rank!=0){

					comm_start = MPI_Wtime();
					MPI_Send(node_arr, num_per_node, MPI_INT, rank-1, 8, MPI_COMM_WORLD);
					comm_end = MPI_Wtime();
					comm_total += (comm_end - comm_start);


					comm_start = MPI_Wtime();
					MPI_Recv(node_arr, num_per_node, MPI_INT, rank-1, 8, MPI_COMM_WORLD, &status);
					comm_end = MPI_Wtime();
					comm_total += (comm_end - comm_start);
				}
			}
			else{ //	Odd-rank process: # 1, 3, 5... (Receiver) => Q-1 <- Q
				if(rank!=size-1){


					comm_start = MPI_Wtime();
					MPI_Recv(next_arr, num_per_node, MPI_INT, rank+1, 8, MPI_COMM_WORLD, &status);
					comm_end = MPI_Wtime();
					comm_total += (comm_end - comm_start);

					compute_start = MPI_Wtime();
					merge_arr = (int*) malloc(num_per_node * 2 * sizeof(int));
					for(j=0; j<num_per_node; ++j){
						merge_arr[j] = node_arr[j];
						merge_arr[j+num_per_node] = next_arr[j];
					}
					Mergesort(num_per_node*2, merge_arr);
					for(j=0; j<num_per_node; ++j){
						node_arr[j] = merge_arr[j];
						next_arr[j] = merge_arr[j+num_per_node];
					}
					free(merge_arr);
					compute_end = MPI_Wtime();
					compute_total += (compute_end - compute_start);

					comm_start = MPI_Wtime();
					MPI_Send(next_arr, num_per_node, MPI_INT, rank+1, 8, MPI_COMM_WORLD);
					comm_end = MPI_Wtime();
					comm_total += (comm_end - comm_start);
				}
			}
		}
	}
	MPI_Barrier(MPI_COMM_WORLD);
	free(next_arr);

	MPI_File ofh;
	MPI_Status ostatus;
	

	iow_start = MPI_Wtime();
	MPI_File_open(MPI_COMM_WORLD, output, MPI_MODE_CREATE | MPI_MODE_WRONLY, MPI_INFO_NULL, &ofh);
	if(read_file)
		MPI_File_set_view(ofh, rank*num_per_node*sizeof(int), MPI_INT, MPI_INT, "native", MPI_INFO_NULL);
	else
		MPI_File_set_view(ofh, 0, MPI_INT, MPI_INT, "native", MPI_INFO_NULL);
	if(read_file && rank!=rank_first_add)
		MPI_File_write_all(ofh, node_arr, num_per_node, MPI_INT, &ostatus);
	else if(read_file && rank==rank_first_add)
		MPI_File_write_all(ofh, node_arr, (num_per_node-num_first_add), MPI_INT, &ostatus);
	else
		MPI_File_write_all(ofh, node_arr, 0, MPI_INT, &ostatus);

	MPI_File_close(&ofh);
	iow_end = MPI_Wtime();
	iow_total = (iow_end - iow_start);
	io_total += iow_total;

	

	all_end = MPI_Wtime();
	all_total = (all_end - all_start);

	printf("Rank:%d (All:%lf, I:%lf, W:%lf, I/O:%lf, Comm:%lf, Compute:%lf)\n", rank, all_total,
		 ior_total, iow_total, io_total, comm_total, compute_total);

	MPI_Barrier(MPI_COMM_WORLD);
	MPI_Finalize();

	return 0;
}
コード例 #13
0
ファイル: e11.cpp プロジェクト: cqiyi/kaoshi
void main() {
	Seqlist S;
	int i;
	char ch1,ch2;
	printf("请输入10个待排序数据:(每个数据间用空格隔开)\n");
	for(i=1; i<=n; i++)
		scanf("%d",&S[i].key);
	ch1='y';
	while (ch1=='y' || ch1=='Y') {
		printf("*****************菜单***********************\n");
		printf("请选择下列*作:\n");
		printf("1------------------更新待排序数据-----------\n");
		printf("2------------------直接插入排序-------------\n");
		printf("3------------------冒泡排序-----------------\n");
		printf("4------------------快速排序-----------------\n");
		printf("5------------------直接选择排序-------------\n");
		printf("6------------------堆排序-------------------\n");
		printf("7------------------归并排序-----------------\n");
		printf("8------------------基数排序-----------------\n");
		printf("9------------------退出---------------------\n");
		printf("请选择*作类别(1-9):");
		scanf("\n%c",&ch2);
		switch (ch2) {
		case '1':
			printf("请输入更新待排序数据:\n");
			for (i=1; i<=n; i++)
				scanf ("%d",&S[i].key);
			break;
		case '2':
			printf("请输入要输出第几趟结果:");
			scanf("\n%d",&m);
			for (i=1; i<=n; i++)
				R[i].key=S[i].key;
			Insertsort();
			break;
		case '3':
			printf("请输入要输出第几趟结果:");
			scanf("\n%d",&m);
			for (i=1; i<n+1; i++)
				R[i].key=S[i].key;
			Bubblesort();
			break;
		case '4':
			printf("请输入要输出第几趟结果:");
			scanf("\n%d",&m);
			for (i=1; i<n+1; i++)
				R[i].key=S[i].key;
			num=0;
			Quicksort(1,n);
			break;
		case '5':
			printf("请输入要输出第几趟结果:");
			scanf("\n%d",&m);
			for (i=1; i<n+1; i++)
				R[i].key=S[i].key;
			Selectsort();
			break;
		case '6':
			printf("请输入要输出第几趟结果:");
			scanf("\n%d",&m);
			for (i=1; i<n+1; i++)
				R[i].key=S[i].key;
			Heapsort();
			break;
		case '7':
			printf("请输入要输出第几趟结果:");
			scanf("\n%d",&m);
			for (i=1; i<n+1; i++)
				R[i].key=S[i].key;
			Mergesort();
			break;
		case '8':
			printf("请输入要输出第几趟结果:");
			scanf("\n%d",&m);
			for (i=0; i<n; i++)
				R[i].key=S[i+1].key;
			Radixsort();
			break;
		case '9':
			ch1='n';
			break;
		default:
			ch1='n';
		}
	}
}