Пример #1
0
/*
 * 将[n ... m]归并排序
 * 注意n为数组的开始下标,m为数组的结束下标,是[n, m]不是[n, m)。
 * 即10个元素的话,m=9
 *
 * 复杂度为O(nlogn)
 */
void
msort(int a[], int b[], int n, int m)
{
	int i, mid;

	if (n == m) {
		b[n] = a[n];
	} else if (n < m) {
		/* 平分数组 */
		mid = (n + m) / 2;

		/* 将a[n ... mid]归并为有序的b[n ... mid] */
		msort(a, b, n, mid);

		/* 将a[mid+1 ... m]归并为有序的b[mid+1 ... m] */
		msort(a, b, mid + 1, m);

		/* 将b[n ... mid]和b[mid+1 ... m]归并到a[n ... m] */
		merge(a, b, n, mid, m);

		/* 此时a[n .. m]为有序,但b[n .. m]还是无序的,所以要复制一下 */
		for (i = n; i <= m; ++i)
			b[i] = a[i];
	}
}
Пример #2
0
void msort(int a[], int low, int high)
{
	int pivot, length=high-low+1, merge1, merge2, i;
	int working[length];
	if(low==high)
		return;
	pivot=(low+high)/2;
	msort(a, low, pivot);
	msort(a, pivot+1, high);
	for(i=0;i<length;i++)
       		working[i]=a[low+i];
	merge1=0;
	merge2=pivot-low+1;
	for(i=0;i<length;i++)
       	{
		if(merge2<=high-low)
			if(merge1<=pivot-low)
				if(working[merge1]>working[merge2])
					a[i+low]=working[merge2++];
				else
					a[i+low]=working[merge1++];
			else
				a[i+low]=working[merge2++];
		else
			a[i+low]=working[merge1++];
	}
}
Пример #3
0
void msort(int *arr, int begin, int end){
	if(end - begin == 1)
		return;

	msort(arr, begin, (begin + end) / 2);
	msort(arr, (begin + end) / 2, end);
	merge(arr, begin, (begin + end) / 2, end);
}
Пример #4
0
void msort(int low, int high){
  if(low >= high){
    return;
  }
  int mid = (low+high)>>1;
  msort(low,mid);
  msort(mid+1,high);
  merge(low, mid, high);
}
Пример #5
0
static void msort(int *data, int *tmp, int left, int right) {
	int center;
	if (left < right) {
		center = (left + right) / 2;
		msort(data, tmp, left, center);
		msort(data, tmp, center + 1, right);
		merge(data, tmp, left, center + 1, right);
	}
}
Пример #6
0
void msort (float a[], int i, int j) { 
  if (i>=j) { return; }
  else { 
    int m = (i+j)/2;
    msort (a,i,m);
    msort (a,m+1,j);
    merge (a,i,m,j);
  }
}
void msort(int a[],int l,int h)
{
    if(l==h)
        return;
    int m=(l+h)/2;
    msort(a,l,m);
    msort(a,m+1,h);
    merge(a,l,m,h);
}
Пример #8
0
void msort(int data[],int left,int right)
{
	int m,length=right-left+1;
	if(length==1) return;
	m=(left+right)/2;
	msort(data,left,m);
	msort(data,m+1,right);

	merge(data,left,right,m,length);
}
Пример #9
0
void msort(LIST* lst){

 if(lst_length(lst) <= 1)
     return;

 LIST* lst2 = lst_prefix(lst, lst_length(lst)/2);
 msort(lst);
 msort(lst2);

 lst_merge_sorted(lst, lst2);
}
Пример #10
0
msort(int low,int high) 
{ 
	int mid; 
	if(low!=high) 
	{ 
		mid=((low+high)/2); 
		msort(low,mid); 
		msort(mid+1,high); 
		mergesort(low,mid,high); 
	} 
} 
Пример #11
0
void msort(int arr[], int temp[], int left, int right)
{
	int mid;
	if (left < right)
	{
		mid = (left + right)/2;
		msort(arr, temp, left, mid);
		msort(arr, temp, mid+1, right);
		merge(arr, temp, left,mid+1, right);
	}
}
Пример #12
0
void msort(int *array, int *tmp, int start, int end)
{
    int mid;
    if(start<end)
    {
        mid = (start+end)/2;
        msort(array, tmp, start, mid);
        msort(array, tmp, mid+1, end);
        merge(array, tmp, start, mid, end);
    }
}
Пример #13
0
/* Merge sort; uses temporary storage in tmp[]
   O(n log n) time, O(n) extra space */
static void msort(int a[], int n)
{
	int *left, *right, *t;
	int i, j;

	if (n <= 32) {
		/* Insertion sort for small subarrays */
		register int k;

		for (i = 1; i < n; i++) {
			for (k = a[i], j = i - 1; j >= 0 && a[j] > k; j--, r++)
				a[j + 1] = a[j];
			a[j + 1] = k;
		}

		return;
	}

	/* Split into two subarrays, recursively sort and count inversions */

	i = n >> 1;
	left = a;
	msort(left, i);

	right = a + i;
	j = n - i;
	msort(right, j);

	/* Merge in temporary storage, count inversions */
	for (t = tmp;;) {
		if (*left <= *right) {
			*t++ = *left++;
			if (--i == 0) break;
		} else if (*right < *left) {
			*t++ = *right++;
			r += i;
			if (--j == 0) break;
		}
	}

	if (n == input_size)
		return;

	while (i-- > 0)
		*t++ = *left++;

	while (j-- > 0)
		*t++ = *right++;

	/* Copy result back to a[] */
	for (i = 0, t = tmp; i < n; i++)
		*a++ = *t++;
}
Пример #14
0
void myMergeSort::msort(element a[], element tmp[], int left, int right)
{
	int center;

	if (left < right)
	{
		center = (left + right) / 2;
		msort(a, tmp, left, center);
		msort(a, tmp, center + 1, right);
		merge(a, tmp, left, center + 1, right);
	}
}
Пример #15
0
void msort(int list[],int l,int r)
{
int mid;

if(l<r)
{
mid=(l+r)/2;

msort(list,l,mid);
msort(list,mid+1,r);
merge(list,l,r);
}
}
Пример #16
0
/**
 * End is inclusive.
 */
int *
msort(int *start, int *end)
{
	if (start < end) {
		int alen = (end - start + 1) / 2;
		int blen = (end - start + 1) - alen;
		int *halfend = start + alen - 1;
		return merge(msort(start, halfend), alen,
		             msort(halfend + 1, end), blen);
	} else {
		return start; /* one element, already sorted. */
	}
}
Пример #17
0
void msort(int *v, int len)
{
    if (len == 0) return;
    if (len == 1) return;

    int split = len / 2;
    int vt1[split];
    int vt2[len - split];

    memcpy(vt1, v, split * sizeof(int));
    memcpy(vt2, v + split, (len - split) * sizeof(int));

    msort(vt1, split);
    msort(vt2, len - split);
    merge(v, vt1, vt2, split, len - split);
}
Пример #18
0
void test_sorting_two_elements_with_merge_sort(){
    int a[] = {3,2},expected[] = {2,3};
    void *elements[] = {&a[0],&a[1]};
    msort(elements,2,compareInteger);
    ASSERT(expected[0] == *(int*)elements[0]);
    ASSERT(expected[1] == *(int*)elements[1]);
};
Пример #19
0
/*
 * 使用递归调用 - 形式简单,实用性差
 * a为原数组,b为临时数组
 * 数组从0开始
 */
void
merge_sort_v1(int a[], int n)
{
	/* 这里最好动态生成数组 */
	int b[n];
	msort(a, b, 0, n - 1);
}
Пример #20
0
void test_sorting_two_float_elements_with_merge_sort(){
    float a[] = {3.3f,2.2f},expected[] = {2.2f,3.3f};
    void *elements[] = {&a[0],&a[1]};
    msort(elements,2,compareFloats);
    ASSERT(expected[0] == *(float*)elements[0]);
    ASSERT(expected[1] == *(float*)elements[1]);
};
Пример #21
0
int main(void){
	FILE *in;
	int numTestCase;
	int arrSort[MAX_SIZE] = { 0, };
	int sizeSort, temp, i;
	
	in = fopen("input.txt", "r");
	fscanf(in, "%d", &numTestCase);
	
	while(numTestCase--){
		fscanf(in, "%d", &sizeSort);
		
		for(i=0; i<sizeSort; i++){
			fscanf(in, "%d", &temp);
			arrSort[i] = temp;
		}

		//qsort(arrSort, sizeSort, sizeof(int), icompare); 
		msort(arrSort, sizeSort, sizeof(int), icompare);
		
		for (i=0; i<sizeSort; i++){
			printf("%d ", arrSort[i]);
		} 
		printf("\n");
	}

	return 0;
}
Пример #22
0
int main()
{
  float A[10] = {9,8,7,6,5,4,3,2,1,0};
  //printf("unsorted vector: %1.0f-%1.0f-..\n",A[0],A[1]); 
  msort(A,0,dim-1);
  //printf("sorted vector: %1.0f-%1.0f-..\n",A[0],A[1]); 
}
Пример #23
0
/** Sorts the specified task list and tests the result. */
void sortTasks(task_t** reference, int count){
	int i;
	int faults = 0;
	const size_t size = sizeof(task_t*) * count;
	
	if (NULL == reference) return;
	
	/* Arrange expected */
	task_t** expected = (task_t**) malloc(size);
	if (NULL == expected) return;
	memcpy(expected, reference, size);
	
	/* Arrange actual */
	task_t** actual = (task_t**) malloc(size);
	if (NULL == actual) return;
	memcpy(actual, reference, size);
	
	/* Act */
	qsort(expected, count, sizeof(task_t*), qcomp);
	msort(actual, count);
	
	/* Assert */
	assert(expected != NULL);
	assert(actual != NULL);
	//printf("%3s :: %5s | %5s | %5s\n","#","[exp]","[act]","[ref]");
	for(i=0; i<count; ++i){
		/*printf("%3d :: %5lu | %5lu | %5lu\n", i, expected[i]->id, actual[i]->id, reference[i]->id);*/
		if (expected[i]->id != actual[i]->id) faults++;
	}
	assert(0 == faults);
	
	/* Clean-up */
	free(expected);
	free(actual);
}
Пример #24
0
int main(){

ElemType userVal;

printf("Please enter a set of integers.\n");
int checker;

LIST* lst = lst_create();

while (1){

   checker = scanf("%i", &userVal);
   if (checker == EOF || checker == 0)
    break;
   lst_push_back(lst, userVal);
}
printf("The list before the merge sort: \n");
lst_print(lst);
printf("The list after the merge sort: \n");
msort(lst);
lst_print(lst);
lst_free(lst);


return 0;
}
Пример #25
0
int main(){
	int len = 0;
	int size = 10001;
	int *arr = malloc(sizeof(int) * size);

	int i = 0;
	char c;
	char buff[100];
	while((c = fgetc(stdin)) != EOF){
		if(c == ' ' || c == '\n'){
			arr[len++] = atoi(buff);
			memset(buff, 0, 100);
			i = 0;
		}
		else
			buff[i++] = c;
		
		if(c == '\n')
			break;

		if(len == size)
			arr = realloc(arr, size *= 2);

	}
		
	msort(arr, 0, len);
	int n;
	for(n = 0; n < len; ++n)
		printf("%d ", arr[n]);
	
	printf("\n");
	return 0;
}
Пример #26
0
int main(int argc, char *argv[])
{
  int n = ARRAY_SIZE;
  int *array;
  monitor *m;

  HANDLE(!(array = malloc(n * sizeof(int)))); 

  make_array(&array, n, ARRAY_ALGO); 
  NOTIFY(print_array(&array, n, OUT_LINE_SIZE));

  m = monitor_init(SELF);
  monitor_start(m);

  msort(&array, n);
  
  monitor_end(m);
 
  NOTIFY(print_array(&array, n, OUT_LINE_SIZE));
  assert(verify(&array, n));
  
  monitor_print_stats(m, VERBOSE);

  monitor_free(m);
  free(array);

  return 0;
}
Пример #27
0
int main()
{
	int array[] = {10,12,1,14,6,5,8,15,3,9,7,4,11,13,2};

	int size = sizeof(array)/sizeof(array[0]);

	int tmp = 0;

	int *tmp_array = NULL;
	
	tmp_array = (int*) malloc(sizeof(int)*size);

	if(!tmp_array)
	{
		printf("malloc failed in function %s()\n",__FUNCTION__);
		return 0;
	}

	msort(array,tmp_array,0,size-1);

	free(tmp_array);

	for(tmp = 0;tmp < size;tmp++)
	{
		printf("%5d",array[tmp]);
	}

	printf("\n");

	return 0;
}
Пример #28
0
void test_sorting_two_double_elements_with_merge_sort(){
    double a[] = {3.3,2.2},expected[] = {2.2,3.3};
    void *elements[] = {&a[0],&a[1]};
    msort(elements,2,compareDouble);
    ASSERT(expected[0] == *(double*)elements[0]);
    ASSERT(expected[1] == *(double*)elements[1]);
};
Пример #29
0
void
MergeSort (void *base, size_t nmemb, size_t size, GCompareFunc compare)
{
	int copy_mode;
	void *tmp;
	
	if (nmemb < 2)
		return;
	
	if (!(tmp = malloc (nmemb * size))) {
		/* fall back to using qsort() and hope & pray that the results are "stable" */
		qsort (base, nmemb, size, compare);
		return;
	}
	
	if ((((char *) base) - ((char *) 0)) % sizeof (long) == 0 && (size % sizeof (long)) == 0)
		copy_mode = 1;
	else if ((((char *) base) - ((char *) 0)) % sizeof (int) == 0 && (size % sizeof (int)) == 0)
		copy_mode = 2;
	else
		copy_mode = 0;
	
	msort (base, tmp, 0, nmemb - 1, size, copy_mode, compare);
	
	free (tmp);
}
Пример #30
0
static struct strlist*
msort(struct strlist* list, size_t len)
{
    struct strlist* p;
    struct strlist* q = NULL;
    struct strlist** lpp;
    size_t half;
    size_t n;

    if (len <= 1)
        return list;
    half = len >> 1;
    p = list;
    for (n = half; n-- > 0 ;)
    {
        q = p;
        p = p->next;
    }
    if (q) q->next = NULL;		/* terminate first half of list */
    q = msort(list, half);		/* sort first half of list */
    p = msort(p, len - half);	/* sort second half */
    lpp = &list;
    for (;;)
    {
        if (strcmp(p->text, q->text) < 0)
        {
            *lpp = p;
            lpp = &p->next;
            if ((p = *lpp) == NULL)
            {
                *lpp = q;
                break;
            }
        }
        else
        {
            *lpp = q;
            lpp = &q->next;
            if ((q = *lpp) == NULL)
            {
                *lpp = p;
                break;
            }
        }
    }
    return list;
}