Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <string.h>
- inline
- void merge(int *left, int l_len, int *right, int r_len, int *out)
- {
- int i, j, k;
- for (i = j = k = 0; i < l_len && j < r_len; )
- out[k++] = left[i] < right[j] ? left[i++] : right[j++];
- while (i < l_len) out[k++] = left[i++];
- while (j < r_len) out[k++] = right[j++];
- }
- /* inner recursion of merge sort */
- void recur(int *buf, int *tmp, int len)
- {
- int l = len / 2;
- if (len <= 1) return;
- /* note that buf and tmp are swapped */
- recur(tmp, buf, l);
- recur(tmp + l, buf + l, len - l);
- merge(tmp, l, tmp + l, len - l, buf);
- }
- /* preparation work before recursion */
- void merge_sort(int *buf, int len)
- {
- /* call alloc, copy and free only once */
- int *tmp = malloc(sizeof(int) * len);
- memcpy(tmp, buf, sizeof(int) * len);
- recur(buf, tmp, len);
- free(tmp);
- }
- int main(int argc, char *argv[])
- {
- int n;
- clock_t ini, final;
- double total;
- //========
- if(argc == 1)
- {
- /* cout << "Al menos inserte un argumento" << endl;*/
- return 0;
- }
- char numero[12];
- char* p, *q;
- for(p = argv[1], q = numero; *p != '\0'; ++p)
- {
- if(*p == '.')
- {
- break;
- }
- else
- {
- *q = *p;
- ++q;
- }
- }
- *q = '\0';
- n = atol(numero);
- /*printf("%d \n", n);*/
- int *x = malloc(sizeof(int)* n);
- FILE *inFile;
- inFile = fopen(argv[1], "r" );
- if (!inFile)
- {
- /* cout << "Al menos inserte un argumento" << endl;*/
- return 0; //exit(1); // terminate with error
- }
- int k;
- for (k = 0; k < n; k++)
- {
- fscanf(inFile, "%i", &x[k]);
- /* printf("%i \n", x[k]); */
- }
- fclose(inFile);
- //cout << "Inicia Mergesort" << endl;*/
- /* //=========== */
- /*
- puts("before sort:");
- for (k = 0; k < n; k++) printf("%d ", x[k]);
- putchar('\n');
- */
- ini = clock();
- merge_sort(x, n);
- final=clock();
- total=((double)(final - ini)) / CLOCKS_PER_SEC;
- /*
- puts("===after sort===:");
- for (k = 0; k < n; k++) printf("%d ", x[k]);
- putchar('\n');
- */
- printf ("%d \t",n);
- printf ("%lf \n", total);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment