Esempio n. 1
0
/**
 *  \brief Divides recursively the array.
 *  \param [in,out] array   int[] Array to sort.
 *  \param [in]     left    int   Left boundary.
 *  \param [in]     right   int   Right boundary.
 */
void m_sort(int array[], const int left, const int right)
{
    /*
     * While right boundary is upper to the left one, divide
     * the array.
     */
    if (right > left){

        int middle = (left + right) / 2;
        
        /*
         * Subdivides the left part of the array.
         */
        m_sort(array, left, middle);

        /*
         * Subdivides the right part of the array.
         */
        m_sort(array, middle + 1, right);

		/*
		 * Merges left and right parts.
		 */
        merge(left, middle, right, array);
    }

}
/***ここからマージソート***/
void m_sort(int first, int last){
  if(first < last){
    m_sort(first, (first + last)/2);
    m_sort(((first + last)/2) + 1, last);
    merge_two(first, (first + last)/2, ((first + last)/2) + 1, last);
  }
}
Esempio n. 3
0
void m_sort(int *A, int *B, int left, int right) {
	int mid;
	if (right > left) {
		mid = (right + left) / 2;
		m_sort(B, A, left, mid);
		m_sort(B, A, mid+1, right);
		merge(A+left, B+left, mid-left+1, B+mid+1, right-mid);
	}
}
Esempio n. 4
0
void m_sort(ElementType arr[], ElementType tmp_arr[], int left, int right)
{
    if(left < right){
        int center = (left + right)/2;
        m_sort(arr, tmp_arr, left, center);
        m_sort(arr, tmp_arr, center+1, right);
        merge(arr, tmp_arr, left, right);
    }
}
Esempio n. 5
0
/*-- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
 |	Subroutine:	m_sort
 --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
*/
void m_sort(int list[], int left, int right){
	int mid;								//Determines the point of split
	
	if(left < right){
		mid = (left+right)/2;
		m_sort(list, left, mid);
		m_sort(list, mid+1, right);
		merge(list, left, mid, right);	//Sorts this peice once components are sorted.
	}
}
Esempio n. 6
0
void m_sort(arraymeta numbers[], arraymeta temp[], int left, int right){
    int mid;
    
    if (right > left){
        mid = (right + left) / 2;
        m_sort(numbers, temp, left, mid);
        m_sort(numbers, temp, mid+1, right);
        merge(numbers, temp, left, mid+1, right);
    }
}
Esempio n. 7
0
int m_sort(int *p, int low, int high)
{
	int middle;

	if (low < high) {
		middle = (low + high) / 2;
		m_sort(p, low, middle);
		m_sort(p, middle + 1, high);
		merge(p, low, middle, high);
	}
	return 0;
}
Esempio n. 8
0
void m_sort(int *numbers, int *temp, char *c, int left, int right)
{
  int mid;
 
  if (right > left)
  {
    mid = (right + left) / 2;
    m_sort(numbers, temp, c, left, mid);
    m_sort(numbers, temp, c, mid+1, right);
 
    merge(numbers, temp, c, left, mid+1, right);
  }
}
Esempio n. 9
0
static void m_sort(redcord_t sr[], redcord_t tr[], int s, int t, MODE mode)
{
	redcord_t temp[MAX_SIZE + 1];
	if (s == t)
	{
		tr[s] = sr[s];
	}
	else
	{
		int m = (s + t) / 2;
		m_sort(sr, temp, s, m, mode);
		m_sort(sr, temp, m + 1, t, mode);
		merge(temp, tr, s, m, t, mode);
	}
}
Esempio n. 10
0
//合并(归并)排序
void mergeSort(int T[], int length)
{
	int* t2 = (int*)malloc(length*sizeof(int));
	memset(t2, 0, length);

	m_sort(T, t2, 0, length - 1);
}
int main(void){
  int i;

  printf("N? ");
  scanf("%d", &N);

  /*set random numbers Bubble[], Quick[], Merge[]*/
  srand((unsigned int)time(0));
 
  for(i = 0; i < N; i++){
    Bubble[i] = Quick[i] = Merge[i] = (rand() % 10000) + 1;
  }

  time(&tm1);  /*tm1(秒)*/
  bubblesort();
  time(&tm2);  /*tm2(秒)*/
  printf("Bubble Sort %ld sec \n", tm2 - tm1);

  time(&tm1);  /*tm1(秒)*/
  quicksort(0, N - 1);
  time(&tm2);  /*tm2(秒)*/
  printf("Quick Sort %ld sec \n", tm2 - tm1);

  time(&tm1);  /*tm1(秒)*/
  m_sort(0, N - 1);
  time(&tm2);  /*tm2(秒)*/
  printf("Merge Sort %ld sec \n", tm2 - tm1);

  return 0;
}
int main(void){
  int i;
  double t, x, y, z;  /*t: ソート処理前のclock, x: バブルソート実行時間, 
                       *y: クイックソート実行時間, z: マージソート実行時間
                       */

  printf("N? ");
  scanf("%d", &N);

  /*set random numbers Bubble[], Quick[], Merge[]*/
  srand((unsigned int)time(0));
 
  for(i = 0; i < N; i++){
    Bubble[i] = Quick[i] = Merge[i] = (rand() % 10000) + 1;
  }

  t = clock();
  bubblesort();
  x = (clock() - t) / CLOCKS_PER_SEC;
  printf("Bubble Sort %g sec \n", x);

  t = clock();
  quicksort(0, N - 1);
  y = (clock() - t) / CLOCKS_PER_SEC;
  printf("Quick Sort %g sec \n", y);

  t = clock();
  m_sort(0, N - 1);
  z = (clock() - t) / CLOCKS_PER_SEC;
  printf("Merge Sort %g sec \n", z);

  return 0;
}
Esempio n. 13
0
void m_sort(int m, int n)
{
	int mid;
	
	printf("\n m_sort called ");
	
	if(m<n)
	{
	mid=(m+n)/2;
	
	m_sort(m,mid);
	m_sort(mid+1,n);
	merge(m,mid,n);
	}

}
Esempio n. 14
0
//排序
//可以在元素个数小于7的时候采用插入排序
//本实现中数据分成2部分,实际上是只在 m_merge()函数中做排序
void m_sort(int T1[], int T2[], int low, int high)
{
	if(low >= high)
		return;

	int mid = low + (high - low)/2;

	//分成2个数据区
	LOG_DEBUG("m_sort l [" << low << ", " << mid << "]");
	m_sort(T1, T2, low, mid);

	LOG_DEBUG("m_sort r [" << mid + 1 << ", " << high << "]");
	m_sort(T1, T2, mid + 1, high);
	//合并数据区
	LOG_DEBUG("m_merge [" << low << ", " << mid << ", " << high << "]");
	m_merge(T1, T2, low, mid, high);
}
Esempio n. 15
0
    void m_sort(std::vector<int> &array){

        if (array.size() < 2) {
            return;
        }
        std::vector<int>::const_iterator left_first = array.begin();
        std::vector<int>::const_iterator left_last = array.begin() + (array.size()/2+(array.size()%2));
        std::vector<int>::const_iterator right_first = array.begin() + (array.size()/2+(array.size()%2));
        std::vector<int>::const_iterator right_last = array.end();
        
        std::vector<int> left(left_first, left_last);
        std::vector<int> right(right_first, right_last);
        
        m_sort(left); // left
        m_sort(right); // right
        
        combine(array,left, right);
    }
void m_sort(int *A, int min, int max)
{
	int *C;		/* dummy, just to fit the function */
	int mid = (min+max)/2;
	int lowerCount = mid - min + 1;
	int upperCount = max - mid;

	/* If the range consists of a single element, it's already sorted */
	if (max == min) {
		return;
	} else {
		/* Otherwise, sort the first half */
		  m_sort(A, min, mid);
		  m_sort(A, mid+1, max);

		C = merge(A + min, lowerCount, A + mid + 1, upperCount,NULL);
	}
}
Esempio n. 17
0
void merge_sort(ElementType arr[], int len)
{
    ElementType* tmp_arr = malloc(len * sizeof(ElementType));
    if(tmp_arr != NULL){
        m_sort(arr, tmp_arr, 0, len - 1);
        free(tmp_arr);
    }
    else{
    }
}
Esempio n. 18
0
void m_sort(int numbers[], int temp[], int left, int right)
{
	int mid;

	if (right > left)
	{
		mid = (right + left) / 2;
		m_sort(numbers, temp, left, mid);
		m_sort(numbers, temp, mid+1, right		

		merge(numbers, temp, left, mid+1, right);
    }
Esempio n. 19
0
void m_sort(int numbers[], int temp[], int left, int right)
{
    if(right - left < MIN_PAR) {
        std::sort(numbers+left, numbers+right+1);
        return;
    }
    int mid;

    if (right > left)
    {
        mid = (right + left) / 2;
#ifdef PAR
        ctx_invoke(
           [=] { m_sort(numbers, temp, left, mid); },
           [=] { m_sort(numbers, temp, mid+1, right); }
        );
#else
        m_sort(numbers, temp, left, mid);
        m_sort(numbers, temp, mid+1, right);
#endif

        merge(numbers, temp, left, mid+1, right);
    }
}
Esempio n. 20
0
bool Personnage::utiliserSort(Personnage& cible)
{
	unsigned int degats = m_sort(&m_mana);

	cible.recevoirDegats(degats);

	if(degats > 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}
Esempio n. 21
0
int main()
{
	int i;
	int *p = m_arr;

	for (i = 0; i < MAX_ITEM; i++)
		p[i] = rand() % 100;

	m_sort(p, 0, MAX_ITEM - 1);

	for (i = 0; i < MAX_ITEM; i++)
		printf("val is %d\n", p[i]);

	return 0;
}
Esempio n. 22
0
main()
{
	int i,n;
	printf("\nEnter the limit for the array ");
	scanf("%d",&n);
	
	printf("\nEnter the array ");
	
	for(i=0;i<n;i++)
		scanf("%d",&a[i]);
	
	m_sort(0,n-1);
	
	printf("\nThe sorted array is now  ");
	
	for(i=0;i<n;i++)
		printf(" %d ",a[i]);
}
Esempio n. 23
0
int main(int argc,char** argv)
{
	int* a;
	int t,n,i;
	while(scanf("%d",&t)!=EOF)
	{
		while(t--)
		{
			scanf("%d",&n);
			a=(int*)malloc(n*sizeof(int));
			for(i=0;i<n;i++)
			{
				scanf("%d",a+i);
			}
			m_sort(a,n);
			printf("%lf\n",calc_score(a,n));
			free(a);
		}
	}
	return 0;
}
int main(int argc, char** argv) {
	int * data;
	int * other;
	int m,n=N;
	int id,p;
	int s = 0;
	int i;
	int chunk;
	int nt = 4;
	int cini[30]={0},cfin[30]={0};
	int part = nt;

    if(isdigit(argv[1])){
       p = atoi(argv[1]);
    if(p<1 || p>20)
        printf("# of threads should be between 1 and 20 - it runs 4 threads -> default");
    else
        nt = p;
    }

    data = (int *)malloc((n)*sizeof(int));
	other = (int *)malloc((n)*sizeof(int));

    read_data(data);

    omp_set_num_threads(nt);
	startT = clock();

    chunk = n / nt;

    #pragma omp parallel for
    for(i=0;i<nt;i++){
        cini[i] = i * chunk;
        cfin[i] = cini[i] + chunk - 1;
        if(i==nt-1 && chunk*nt!= n )
            cfin[i] = cfin[i] + (n - chunk*nt) ;

        m_sort(data, cini[i], cfin[i]);
    }


     int j,t=nt,x;
     do{
      j = t;
     #pragma omp parallel for
     for(i=0;i<t-1;i=i+2){
           merge(&data[cini[i]], cfin[i]-cini[i]+1, &data[cini[i+1]], cfin[i+1]-cini[i+1]+1, &other[MIN(cini[i],cini[i+1])]);
           cini[j]=MIN(cini[i],cini[i+1]);
           cfin[j]=MAX(cfin[i],cfin[i+1]);
           j++;
      }
      if(t%2!=0){
         cini[j]=cini[t-1];
         cfin[j]=cfin[t-1];
         j++;
      }
      if(j-t>1){
         for(i=t,x=0;i<j;i++,x++){
            cini[x]=cini[i];
            cfin[x]=cfin[i];
         }
      }
      t=j-t;
     }while(t>1);

	    stopT = clock();
		FILE * fout;

		printf("%d; %d processors; %f secs\n",s,nt,(stopT-startT)/CLOCKS_PER_SEC);

		fout = fopen("result.txt","w");
		for(i=0;i<n;i++){
			fprintf(fout,"%d\n",other[i]);
			//printf("%d \n",other[i]);
		}
		fclose(fout);

   return 0;
}
Esempio n. 25
0
void merge_sort(sq_list *p_L, MODE mode)
{
	m_sort(p_L->data, p_L->data, 1, p_L->length, mode);
}
Esempio n. 26
0
void mergeSort(int numbers[], int temp[], int array_size)
{
	m_sort(numbers, temp, 0, array_size - 1);
}
Esempio n. 27
0
/*-- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
 |	Subroutine:	merge_sort
 --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
*/
void merge_sort(int list[], size_t list_len){
	m_sort(list, 0, list_len-1);
}
Esempio n. 28
0
/**
 *  \brief  Puts an array in order with the merge sort.
 *  \par Details
 *          Divide and conquer !
 *      Sorts an array by splitting it recursively. Once it
 *      can't be more divided, it merges the two arrays by
 *      comparing values.
 *
 *  \param [in,out] array   int[] Array to sort.
 *  \param [in]     n       int   Number of elements.
 */
void merge_sort(int array[], const int n)
{
    m_sort(array, 0, n - 1);

}
Esempio n. 29
0
void mergeSort(int *numbers, int *temp, char *c, int array_size)
{
  m_sort(numbers, temp, c, 0, array_size - 1);
}
Esempio n. 30
0
main(int argc, char **argv)
{
	int * data;
	int * chunk;
	int * other;
	int m,n=N;
	int id,p;
	int s = 0;
	int i;
	int step;
	MPI_Status status;

	MPI_Init(&argc,&argv);
	MPI_Comm_rank(MPI_COMM_WORLD,&id);
	MPI_Comm_size(MPI_COMM_WORLD,&p);

	startT = MPI_Wtime();
	
/**************************** master task ************************************/
	if(id == MASTER)
	{
		int r;
		srandom(MPI_Wtime());
		s = n/p;
		r = n%p;
		data = (int *)malloc((n+s-r)*sizeof(int));
		for(i=0;i<n;i++)
			data[i] = random();
		if(r!=0)
		{
			for(i=n;i<n+s-r;i++)
				data[i]=0;
			s=s+1;
		}

		MPI_Bcast(&s,1,MPI_INT,0,MPI_COMM_WORLD);
		chunk = (int *)malloc(s*sizeof(int));
		MPI_Scatter(data,s,MPI_INT,chunk,s,MPI_INT,0,MPI_COMM_WORLD);
		m_sort(chunk, 0, s-1);
		/* showVector(chunk, s, id); */
	}

/**************************** worker task ************************************/
	else
	{
		MPI_Bcast(&s,1,MPI_INT,0,MPI_COMM_WORLD);
		chunk = (int *)malloc(s*sizeof(int));
		MPI_Scatter(data,s,MPI_INT,chunk,s,MPI_INT,0,MPI_COMM_WORLD);
		m_sort(chunk, 0, s-1);
		/* showVector(chunk, s, id);*/
	}



      /*data propagation in a tree fashion*/
	step = 1;
	while(step<p)
	{
		if(id%(2*step)==0)
		{
			if(id+step<p)
			{
				MPI_Recv(&m,1,MPI_INT,id+step,0,MPI_COMM_WORLD,&status);
				other = (int *)malloc(m*sizeof(int));
				MPI_Recv(other,m,MPI_INT,id+step,0,MPI_COMM_WORLD,&status);
				chunk = merge(chunk,s,other,m);
				s = s+m;
			} 
		}
		else
		{
			int near = id-step;
			MPI_Send(&s,1,MPI_INT,near,0,MPI_COMM_WORLD);
			MPI_Send(chunk,s,MPI_INT,near,0,MPI_COMM_WORLD);
			break;
		}
		step = step*2;
	}

	stopT = MPI_Wtime();
	if(id==0)
	{
		FILE * fout;

		printf("%d; %d processors; %f secs\n", s, p, (stopT-startT));

		fout = fopen("result","w");
		for(i=0;i<s;i++)
			fprintf(fout,"%d\n",chunk[i]);
		fclose(fout);
	}
	MPI_Finalize();
}