Ejemplo n.º 1
0
void merge_pass( float append[], int m, int n )
{
	if( m!= n)
	{
		int mid = (m+n)/2;
		merge_pass( append, m, mid );

		merge_pass( append, mid+1, n );
		merge_sort( append, m, mid, n );
	}
}
Ejemplo n.º 2
0
void  merge_sort( ElementType list[],  int N )
{
	ElementType extra[MAXN];  /* the extra space required */
	int  length = 1;  /* current length of sublist being merged */
	while ( length < N ) {
		merge_pass( list, extra, N, length ); /* merge list into extra */
		output( extra, N );
		length *= 2;
		merge_pass( extra, list, N, length ); /* merge extra back to list */
		output( list, N );
		length *= 2;
	}
}
/*
 * Run the full sort sequence.
 */
bool MergeSorter::sort()
{
	// Get start time.
	timeval start;
	timeval end;
	gettimeofday(&start, NULL);

	// Create initial runs.
	if (!create_runs()) {
		fprintf(stderr, "Failed to create initial runs.\n");
		return false;
	}

	// Now run the actual sort until run lengths are greater than or equal
	// to number of record pages.
	unsigned int pass = 0;
	while (_run_length < _records_pages) {
		// Switch input and output.
		if (!swap_run_files()) {
			return false;
		}

		// Now merge.
		if (!merge_pass()) {
			return false;
		}
		++pass;
	}

	// Now write out sorted.
	if (fseek(_run_output, 0, SEEK_SET) != 0) {
		fprintf(stderr, "Failed to move to start of output run.\n");
		return false;
	}
	unsigned int count = _records_pages;
	while (count != 0) {
		// Read k + 1 pages at a time.
		size_t records = fread(_pages, _page_size, _k + 1, _run_output);
		if (records == 0) {
			fprintf(stderr, "Failed to read page from output run.\n");
			return false;
		}
		size_t written = fwrite(_pages, _page_size, records, _output);
		if (written != records) {
			fprintf(stderr, "Failed to write page to output.\n");
			return false;
		}
		count -= written;
	}

	gettimeofday(&end, NULL);
	int seconds = end.tv_sec - start.tv_sec;	
	int milliseconds = (end.tv_usec - start.tv_usec) / 1000;
	printf("Successfully sorted in %u passes (not including original runs)!\n", pass);
	printf("Took %d.%d seconds.\n", seconds, milliseconds);
	return true;
}
Ejemplo n.º 4
0
void Arr<Type, Size>::merge(Type *out, Type *a, int s) {
  if (s >= 1) {
    int i = 0;
    for (i=0; i+s<size; i += 2*s) {
      int end = i+2*s > size ? size : i+2*s;
      merge_pass(out, a, i, end, s);
    }
    copy(out, a, i, i, size-i);
  }
}
Ejemplo n.º 5
0
void merge_sort(int * arr, int size)
{
	int i;
	int * temp_arr = NULL;

	temp_arr = malloc(size * sizeof(int));
	if (temp_arr == NULL)
	{
		fprintf(stderr, "Error: Memory allocation failed\n");
		exit(1);
	}

	i=1;
	for (i=1; i<size; i *= 4)
	{
		merge_pass(temp_arr, arr, size, i);
		merge_pass(arr, temp_arr, size, i*2);
	}

	free(temp_arr);
}
Ejemplo n.º 6
0
/*
 * 非递归调用
 */
void
merge_sort_v2(int a[], int n)
{
	int b[n];
	int i, gap;

	/* 复制数组a为临时数组b */
	for (i = 0; i < n; ++i)
		b[i] = a[i];

	gap = 1;
	/* 两两循环归并 */
	while (gap < n) {
		merge_pass(a, b, gap, n);
		gap *= 2;
	}
}
Ejemplo n.º 7
0
void main()
{
	float list[1000];
	int i, n = 30;
	printf("\n Size of the list: %d", n);

	for(i = 0 ; i < n ; i++)
	{
		list[i] = (float) (rand() %100);
	}

	printf("\n Entered list as follows:\n");
	for( i = 0 ; i<n ; i++)
		printf(" %d ", (int) list[i]);

	i = 0 ;

	merge_pass(list, i, n-1);

	printf("\n Merge sorted list is as follows:\n");
	for( i = 0 ; i<n ; i++)
		printf(" %d", (int) list[i]);
}