コード例 #1
0
ファイル: merge_sort.c プロジェクト: morgengc/algorithm
int main() 
{ 
#ifndef VERIFIER
	int data[] = {44, 12, 145, -123, -1, 0, 121, 88}; 
	const unsigned int n = sizeof(data)/sizeof(int);

	printArray(data, n);

	MERGE_SORT(data, 0, n-1); 

	printArray(data, n);
#else
	int *Array = NULL;
	int size = 0;
	int test_case = 0;
	for (; test_case<MAX_TEST_CASE; test_case++)
	{
		Array = FillArray(&size);
		if (!Array)
			return 0;

		MERGE_SORT(Array, 0, size-1);

		CheckArrayElement(Array, size);

		ReleaseArray(Array);
	}
	printf("\n");
#endif

	return 0; 
} 
// 归并排序
void MERGE_SORT(int* A, int p, int r) {
    if (p < r) {
        int q = (p + r)/2;
        MERGE_SORT(A, p, q);
        MERGE_SORT(A, q+1, r);
        MERGE(A, p, q, r);
    }
}
コード例 #3
0
ファイル: merge1.c プロジェクト: ourzizz/learnc
void MERGE_SORT(int *A,int start,int end)
{
    if(start<end)
    {
        int q = (end+start)/2;
        MERGE_SORT(A,start,q);
        MERGE_SORT(A,q+1,end);
        merge(A,start,q,end);
    }
}
コード例 #4
0
ファイル: MERGE_SORT_2.cpp プロジェクト: huanle543/Algorithms
void MERGE_SORT(int a[], int p, int r)
{
    if (p<r)
    {
        int q = (p + r) / 2;                   //直接强制转换到int取整
        MERGE_SORT(a, p, q);
        MERGE_SORT(a, q + 1, r);
        MERGE(a, p, q, r);
    }
}
コード例 #5
0
ファイル: merge_sort.c プロジェクト: morgengc/algorithm
void MERGE_SORT(int A[], const unsigned int p, const unsigned int r) 
{ 
	if(p < r) 
	{ 
		int q = (p + r) / 2; 
		MERGE_SORT(A, p, q); 
		MERGE_SORT(A, q + 1, r);

		MERGE(A, p, q, r); 
	} 
} 
コード例 #6
0
ファイル: MERGE_SORT_2.cpp プロジェクト: huanle543/Algorithms
int _tmain(int argc, _TCHAR* argv[])
{
    int aArray[] = {100, 1, 555, 3, 11, 99, 7, 4, 6 };
    MERGE_SORT(aArray, 0, (sizeof(aArray) / 4) - 1);     //未-1导致数组超出范围,结果出现随机数
    for (int i = 0; i<sizeof(aArray) / 4; ++i)
    {
        cout << aArray[i] << endl;
    }
    return 0;
}
コード例 #7
0
int main() {
	int i;
	int array[10]= {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
	MERGE_SORT(array, 0, 9);
	
	for (i = 0; i<10; i++) {
		printf("%d ", array[i]);
	}
	printf("\n");
	return 0;
}
コード例 #8
0
ファイル: merge_sort.cpp プロジェクト: MosBest/code-about-OJ
int main(void)
{
	int A[10]={1,3,5,6,9,2,4,5,7,8};
	
	MERGE_SORT(A,0,9);
	for(int i=0;i<10;i++)
	{
		printf("%d ",A[i]);
	}
	return 0;
}
int main () {
	count = 0;

	int n, A[100], i;
	printf("Size of array(<= 100): ");
	scanf("%d", &n);
	for (i = 0; i < n; ++i) 
		scanf("%d", &A[i]);
	MERGE_SORT(A, 0, n-1);
	printf("%d\n", count);
}
コード例 #10
0
ファイル: Mergesort.c プロジェクト: Aries0331/cmput379
void MERGE_SORT(int array[], int begin, int end)
{
    int mid;
    int lpid, rpid;
    
    if(begin < end) {
        mid = (begin + end) / 2;
        
        /* create the first child */
        lpid = fork();
        if(lpid < 0) {
            perror("fork");
            exit(1);
        }
        else if(lpid == 0) {
            MERGE_SORT(array,begin,mid);
            shmdt(shmem_array);
            exit(0);
        }
        else {
            waitpid(lpid,NULL,0);
            /* create the second child */
            rpid = fork();
            if(rpid < 0) {
                perror("fork");
                exit(1);
            }
            else if(rpid == 0) {
                MERGE_SORT(array,mid+1,end);
                shmdt(shmem_array);
                exit(0);
            }
        }
        
        /* The parent process has to wait for both children to finish their work */
        waitpid(rpid,NULL,0);
        
        MERGE(array,begin,mid,end);
    }

}
コード例 #11
0
ファイル: merge1.c プロジェクト: ourzizz/learnc
int main(void)
{
    int a[7]={7,5,4,20,10,9,8};
    int size=sizeof(a)/sizeof(int);
    MERGE_SORT(a,0,size-1);
    int i;
    for (i = 0; i < size; ++i) {
        printf("%d ",a[i] );
    }
    printf("\n");

    return 0;
}
コード例 #12
0
ファイル: Mergesort.c プロジェクト: Aries0331/cmput379
int main(int argc, char *argv[])
{
    FILE * input;
    int i, j, data, length;
    int * array;
    
    length = 0;
    
    /* allocate a new memory space for the array for the first time */
    array = malloc(sizeof(int));
    
    /* Read the file from the command line */
    input = fopen(argv[1], "r");
    
    /* check if the file is successfully read */
    if(input == NULL) {
        printf("Could not open the file.");
        exit(EXIT_FAILURE);
    }
    
    printf("Sorting file: %s\n", argv[1]);
    
    /* put all the numbers read from the input file into the array */
    while(fscanf(input, "%d", &data) != -1) {
        ++length;
        /* realloc the memory space to fit the unlimited amount of numbers */
        array = realloc(array, length * sizeof(int));
        array[length-1] = data;
    }
    
    printf("%d elements read\n\n", length);
    printf("Input Numbers:\n");
    
    for(j=0;j<length;j++) {
        printf("%d ",array[j]);
    }
    printf("\n\n");
    
    shmem_size = length * sizeof(int);
    
    /* allocate a shared memory segment */
    shmem_id = shmget(IPC_PRIVATE, shmem_size, IPC_CREAT | 0666);
    if(shmem_id == -1) {
        perror("shemget error");
        exit(1);
    }
    
    /* attach the shared memory segment */
    shmem_array = shmat(shmem_id, NULL, 0);
    if(shmem_array == (void *)-1) {
        perror("shemat error");
        exit(1);
    }
    
    /* write the original array to be sorted into the shared memory */
    for(i=0;i<length;i++) {
        shmem_array[i] = array[i];
    }
    
    /* mergesort algorithm */
    MERGE_SORT(shmem_array,0,length-1);
    
    /* Print out the sorted array */
    printf("Sorted Numbers:\n");
    for(int i=0;i<length;i++) {
        printf("%d ",shmem_array[i]);
    }
    printf("\n");
    
    fclose(input);
    free(array);
    
    /* detach the shared memory segment */
    if(shmdt(shmem_array) == -1) {
        perror("shmdt");
        exit(1);
    }
    
    /* remove the shared memory segment */
    if(shmctl(shmem_id, IPC_RMID, NULL) == -1) {
        perror("shmctl");
        exit(1);
    }

    return 0;
}