Example #1
0
o// Author: John Maslanka
// Revised: Chad David Cover
// Date: January 21, 2016
// Version: 1

#include "stdafx.h"

void MergeSort::mergesort(int data[], int n)
{
	int n1, n2;
	if (n > 1)
	{
		n1 = n / 2;
		n2 = n - n1;
		mergesort(data, n1);
		mergesort((data + n1), n2);
		merge(data, n1, n2);
	}
}
Example #2
0
void mergesort(int low, int high){
    if(low == high){
        return;
    }else if(high == low+1){
        if(a[high] < a[low]){
            swap(low,high);
        }
        return;
    }

    int mid = (low+high)/2;
    mergesort(low,mid);
    mergesort(mid+1,high);

    int itr;
    int i = low;
    int j = mid+1;
    int k = 0;
    while( i<=mid && j<=high){
        if(a[i]<a[j]){
            b[k] = a[i];
            k++;
            i++;
        }else{
            b[k] = a[j];
            j++;
            k++;
        }
    }
    while( i <= mid){
        b[k] = a[i];
        k++;
        i++;
    }
    while( j <= high){
        b[k] = a[j];
        k++;
        j++;
    }
    for(itr = 0; itr < k; itr++){
        a[low+itr] = b[itr];
    }
}
Example #3
0
void splitmerge(int *a,int start,int end,int size)
{
	int mid,i;
	if(start == end)
		return;
	mid = (start + end)/2;
	splitmerge(a,start,mid,size);
	splitmerge(a,mid+1,end,size);
	mergesort(a,start,mid,end,size);
}
Example #4
0
File: 25.c Project: moonblade/dsa
int main()
{
    int a[max];
    int n;
    n=read(a);
    printf("sorted array\n");
    mergesort(a,0,n-1);
    display(a,n);
    return 0;
}
Example #5
0
static void mergesort (char *p, 
                       char *r,
                       size_t elemSize, 
                       int (*comparator)(const void*, const void*)) {

  int numElems;
  char *q;

  if ((r-p) < (2*elemSize)) return;
  
  numElems = (r-p)/elemSize;
  numElems /= 2;
  q = p + numElems*elemSize;

  mergesort(p, q, elemSize, comparator);
  mergesort(q, r, elemSize, comparator);

  merge(p, q, r, elemSize, comparator);
}
Example #6
0
/* Autor: Philipp Lasser
 * Datum: 21.01.2009
 * Version: 1.1
 * Programmbeschreibung: Dies ist ein Programm das 10 eingegebenen 
 * Zahlen, aufsteigend sortiert wieder ausgiebt. 
 */
void mergesort(int a[], int low, int high) { //Funktion mergesort
  int i = 0;
  int elemente = high - low + 1; //Anzahl der Elemente
  int hilfsarray  = 0;
  int FeldL = 0;
  int FeldR = 0;
  int arbeitsarray[elemente];

  if(low == high) //wenn nichts zu sortieren ist
    return;

  hilfsarray  = (low + high) / 2;

  mergesort(a, low, hilfsarray); //Rekursion
  mergesort(a, hilfsarray + 1, high); 

  for(i = 0; i < elemente; i++)
    arbeitsarray[i] = a[low + i];  //schreiben ins Arbeitsarray

  FeldL = 0;
  FeldR = hilfsarray - low + 1;

  for(i = 0; i < elemente; i++) {

    if(FeldR <= high - low) //Vergleichen der 
      if(FeldL <= hilfsarray - low)


	if(arbeitsarray[FeldL] > arbeitsarray[FeldR]){   //Die Teilfelder wieder vereinigen
	  a[i + low] = arbeitsarray[FeldR++];
	}
	else {
	  a[i + low] = arbeitsarray[FeldL++];
	}
      else {
	a[i + low] = arbeitsarray[FeldR++];
      }
    else {		
      a[i + low] = arbeitsarray[FeldL++];
    }
  }

}
Example #7
0
void merge(int a[],int first,int last)
	{
	if(first<last)
		{
		int mid=(first+last)/2;
		merge(a,first,mid);
		merge(a,mid+1,last);
		mergesort(a,first,mid,last);
		}
	}
Example #8
0
void part(int arr[],int low,int high) {

    int mid;
    if(low<high) {
        mid=(low+high)/2;
        part(arr,low,mid);
        part(arr,mid+1,high);
        mergesort(arr,low,mid,high);
    }
}
int* mergesort( int* data, int length ) {
	int middle = length / 2;
	
	if( length > 1 ) {
		// rekursives Aufrufen - vom ersten Element bis zur Mitte
		// linkes Teilfeld
		mergesort(&data[0], middle);			
		// rekursives Aufrufen - vom mittleren Element bis zum letzten
		// rechtes Teilfeld
		mergesort(&data[middle], length - middle);
	}
	
	// Feld ist bereits sortiert, wenn es nur ein Element hat
	if( length == 1 )
		return data;
	
	int tmp[N];	
	int a = 0;				// Index des temporären Feldes
	int b = 0;				// Index des linken Feldes
	int c = middle;			// Index des rechten Feldes
	
	// solange im linken und im rechten Teilfeld noch Elemente sind
	while(b < middle and c < length){
		if( data[b] < data[c] )
			tmp[a++] = data[b++];		// linkes Element kleiner
		else
			tmp[a++] = data[c++];		// rechtes Element kleiner
	}
	
	// Rest des linken Teilfeldes noch übertragen
	while( b < middle )
		tmp[a++] = data[b++];
		
	// Rest des rechten Teilfeldes noch übertragen
	while( c < length ) 
		tmp[a++] = data[c++];
	
	// sortierte Elemente zurückkopieren
	for( int i = 0; i < length; i++ )
		data[i] = tmp[i];
	
	return data;
};
Example #10
0
/**
 * linkedlist* mergesort(linkedlist *head,int (*comp)(linkedlist*,linkedlist*))
 *
 * 			Merge sort function for linked lists
 *
 * @list1:	Pointer to linked list head.
 *
 * @comp:		This function allows for custom comparison
 * 				functions to be used that take in linked lists
 * 				and return <1 if node 1 is less than head two
 * 				and return >1 if node 2 is greater than head two
 *
 * @return: Returns the new top node of sorted linked list
 */
linkedlist* mergesort(linkedlist *head,int (*comp)(linkedlist*,linkedlist*))
{
	linkedlist *head_one;
	linkedlist *head_two;
	if((head == NULL)||(head->nextelm == NULL))
		return head;

	head_one = head;
	head_two = head->nextelm;
	while((head_two != NULL) && (head_two->nextelm != NULL))
	{
		head = head->nextelm;
		head_two = head->nextelm->nextelm;
	}
	head_two = head->nextelm;
	head->nextelm = NULL;

	return merge(mergesort(head_one,(comp)), mergesort(head_two,(comp)),(comp));
}
Example #11
0
typename List<T>::ListNode * List<T>::mergesort(ListNode * start, int chainLength)
{
    /// @todo Graded in MP3.2
	int half = chainLength/2;
	if(start==NULL || start->next == NULL)
		return start;

	ListNode * second = split(start,half);
	
	start = mergesort(start,half);
	if(chainLength%2==0)
	second = mergesort(second,half);
	else
	second = mergesort(second,chainLength-half);


	
	return merge(start,second); // change me!
}
int main()
{

   while(1)
   {   int nattackers,ndefenders;
       scanf("%d %d",&nattackers,&ndefenders);
       if(nattackers==0&&ndefenders==0)
        break;
       int a[15],b[15],i;
       for(i=0;i<nattackers;++i)
       {
          if(i==nattackers-1)
          scanf("%d",&a[i]);
          else
          scanf("%d ",&a[i]);
       }
       for(i=0;i<ndefenders;++i)
       {
          if(i==ndefenders-1)
          scanf("%d",&b[i]);
          else
          scanf("%d ",&b[i]);
       }
       mergesort(a,nattackers);
       mergesort(b,ndefenders);
       int count=0,flag=0;
       for(i=0;i<ndefenders;++i)
       {
           if(b[i]<=a[0])
            ++count;
            if(count>=2)
            {
                ++flag;
                break;
            }
       }
       if(flag)
        printf("N\n");
       else
        printf("Y\n");
   }
  return 0;
}
Example #13
0
void mergesort(int l,int r)
{

    int i,j,k,m;
    int *b;
    b=(int *)malloc(sizeof(int)*r);
    if(r > l)
    {
        m=(r+l)/2;
        mergesort(l,m);
        mergesort(m+1,r);
        for(i=m+1;i>l;i--)
            b[i-1]=V[i-1];
        for(j=m;j<r;j++)
            b[r+m-j]=V[j+1];
        for(k=l;k<=r;k++)
        V[k]=(b[i]<b[j]) ? b[i++] : b[j--];
    }
}
void mergesort(int *a, int n, int low, int high, int *ans){
	int mid;
	int *temp;

	temp = (int*)malloc(sizeof(int)*n);

	if(low==high){
		*(ans+low) = *(a+low);
		return;
	}
	mid = (low+high)/2;
	//sort a[low...mid]
	mergesort(a, n, low, mid, temp);
	//sort a[mid+1, high]
	mergesort(a, n, mid+1, high, temp);
	//merge temp[low...mid] temp[mid+1, high] into ans[low...high]
	merge(temp, ans, low, mid, high);
	free(temp);
}
int main()
{
	int i,j,n=6;
	int a[]={6,5,4,3,2,1};
	mergesort(a,0,5);
	for(i=0;i<n;i++)
		printf("%d ",a[i]);


}
Example #16
0
void *
mergesort(void *v, int n, int size, int (*compare)(const void *, const void *)) {
    if (n < 2) {
        return;
    }

    int half = n/2;
    int comp_result;
    char *base = (char *) v;
    char *left_base, *right_base;

    left_base  = (char *) mergesort((void *) base, half, size,  compare);
    right_base = (char *) mergesort((void *) &base[half * size], n - half, size,  compare);

    char *sorted = (char *) malloc(n * size);

    int i, p = 0, q = 0;
    for (i = 0; i < n; ++i) {
        if (p == half) {
            // sorted[i] = second_half[q++];
            memcpy(sorted + i*size, right_base + q*size, size);
            ++q;
        } else if (q == n - half) {
            // sorted[i] = first_half[p++];
            memcpy(sorted + i*size, left_base + p*size, size);
            ++p;
        } else {
            comp_result = (*compare)((const void *) &left_base[p*size], (const void *) &right_base[q*size]);
            if (comp_result <= 0) {
                // sorted[i] = first_half[p++];
                memcpy(sorted + i*size, left_base + p*size, size);
                ++p;
            } else {
                // sorted[i] = second_half[q++];
                memcpy(sorted + i*size, right_base + q*size, size);
                ++q;
            }
        }
    }

    return (void *) sorted;
}
Example #17
0
int main() {
    int array[10] = { 123, 542, 12, 76, 32, 211, 356, 864, 1, 721 };
    
    mergesort(array, 0, 9);

    int i = 0;
    while (i < 10) {
        printf("%d ", array[i++]);
    }
    printf("\n");
}
Example #18
0
int main()
{
    int i;

    mergesort(a,0,13);

     for(i=0;i<14;i++)
        {printf("%d ",a[i]);}

    return 0;
}
Example #19
0
int main(void)
{
  double arr[15] = {11.0,-77.22,-108.03,2056.1,35.44,12.14,-33.0,-9050.0,7.509,99.002,-14,17,0.9,98098,-4000};
  
  int i,size = sizeof(arr)/sizeof(double);
  mergesort(arr,size);
  printf("sorted vector:\n");
  for (i = 0; i < size ;i++)
    printf("%f ", arr[i]);
  return 0;
}
Example #20
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); 
	} 
} 
int main1() {
	/*freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout);*/
	int N;
	while (scanf("%d", &N) != EOF) {
		for (int i = 0; i < N; ++i)
			scanf("%d", &A[i]);

		printf("%lld\n", mergesort(0, N - 1));
	}
	return 0;
}
Example #22
0
File: B.c Project: jpbat/aed2011
int main(int argc, char **argv)
{
	int size, i;
	nodePtr* POIs;
	double start, stop;
	
	scanf("%d", &size);
	
	POIs = (nodePtr*) malloc (size*sizeof(nodePtr));

	aux = (nodePtr*) malloc(size * sizeof(nodePtr));
	
	for (i=0 ; i<size ; i++)
	{	
		POIs[i]= (nodePtr) malloc (sizeof(node));
		aux[i]=(nodePtr) malloc (sizeof(node));
		scanf("%d", &POIs[i]->lat.d);
		scanf("%d", &POIs[i]->lat.m);
		scanf("%d", &POIs[i]->lat.s);
		scanf("%d", &POIs[i]->lon.d);
		scanf("%d", &POIs[i]->lon.m);
		scanf("%d", &POIs[i]->lon.s);
		POIs[i]->la = toDouble(POIs[i]->lat);
		POIs[i]->lo = toDouble(POIs[i]->lon);
		POIs[i]->ind = i;
		scanf("%[^\n]",POIs[i]->text);
	}
	
	start = getCurrentTimeNano();
 	mergesort(POIs, size);
	stop = getCurrentTimeNano();
 	
 	/*
 	for (i = 0; i < size - 1; i++)
	{
		if ( compare(POIs[i],POIs[i+1]) == 0 )
		{
			if (POIs[i]->ind > POIs[i+1]->ind)
				POIs = rem(POIs, i, &size);
			else
				POIs = rem(POIs, i+1, &size);
			i--;
		}
	}
	*/
	for (i=0 ; i<size ; i++)
		/*print(POIs[i]), */free(POIs[i]);

	free(POIs);
	
	printTimeElapsed(start, stop, "Nanosegundos");
	
	return 0;
}
Example #23
0
void test_mergesort() {
	int A[] = {1, 4, 6, 2, 3, 5};
	int B[] = {5, 3, 1, 2, 4, 6};
	int C[] = {0, 0, 0, 0, 0, 0};
	
	mergesort(A, C, 0, 6);
	mergesort_iter(B, C, 5);
	
	print_list(A, 6);
	print_list(B, 5);
}
Example #24
0
int main(void){


// int array[10]={30,20,40,35,5,50,45,10,25,15};
// int array[5]={10,8,6,5,4};
int array[3]={10,8,6};
int size=3;
mergesort(array,0,size-1);


}
int main() {
    int n, q, i, x, casect= 0;
    while(scanf("%d", &n) > 0) {
        for(i = 0; i < n; i++)
            scanf("%d", &s[i]);
        tot = 0;
        mergesort(s, n);
        printf("Minimum exchange operations : %lld\n", tot);
    }
    return 0;
}
Example #26
0
float MergeSort_char(int arraysize)
{
	printf("Start MergeSort_char(%d)...\t\t",arraysize);
	for(i=0;i<arraysize;i++)
				strcpy(strdata[i],strdata2[i]);
	startTime = clock();
	mergesort(strdata,arraysize,sizeof(char **),compstr);
	endTime = clock();
	printf("MergeSort_char(%d) Complteted\n",arraysize);
	return endTime - startTime;
}
Example #27
0
void partition(int arr[],int low,int high)
{
	int mid;
	if(low<high)
	{
		mid=low+(high-low)/2;
		partition(arr,low,mid);
		partition(arr,mid+1,high);
		mergesort(arr,low,mid,high);
	}
}
Example #28
0
static void test (uint64_t a[], size_t n)
{
	size_t i;
	for (i = 0; i < n; i++)
		fprintf(stderr, " %" PRId64, a[i]);
	fprintf(stderr, " -->");
	mergesort(a, n);
	for (i = 0; i < n; i++)
		fprintf(stderr, " %" PRId64, a[i]);
	fprintf(stderr, "\n");
}
Example #29
0
int main(int argc, char const* argv[])
{
	const int n=15;
	int a[n]={11,7,3,9,14,0,12,5,15,10,2,8,1,4,6};

	print_array(n,a);
	mergesort(n,a); 
	print_array(n,a);

	return 0;
}
Example #30
0
float MergeSort_int(int arraysize)
{
	printf("Start MergeSort_int(%d)...\t\t",arraysize);
	for(i=0;i<arraysize;i++)
		intdata[i]=intdata2[i];
	startTime = clock();
	mergesort(intdata,arraysize,sizeof(int),comp);
	endTime = clock();
	printf("MergeSort_int(%d) Complteted\n",arraysize);
	return endTime - startTime;
}