Last active
April 6, 2017 07:25
-
-
Save RicardoLara/518bc0739b49caa1101f4ab961e08470 to your computer and use it in GitHub Desktop.
Práctica 3 2/5[AA] - Daniel Cruz García
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include "tiempo.h" | |
void merge(int arr[], int l, int m, int r){ | |
int i, j, k; | |
int n1 = m - l + 1; | |
int n2 = r - m; | |
int L[n1], R[n2]; | |
for (i = 0; i < n1; i++) L[i] = arr[l + i]; | |
for (j = 0; j < n2; j++) R[j] = arr[m + 1+ j]; | |
i = 0; /* 1 */ j = 0; /* 2 */ k = l; // mergeado | |
while (i < n1 && j < n2){ | |
if (L[i] <= R[j]){ | |
arr[k] = L[i]; | |
i++; | |
} | |
else{ | |
arr[k] = R[j]; | |
j++; | |
} | |
k++; | |
} | |
while (i < n1){ | |
arr[k] = L[i]; | |
i++; | |
k++; | |
} | |
while (j < n2){ | |
arr[k] = R[j]; | |
j++; | |
k++; | |
} | |
} | |
void mergeSort(int arr[], int l, int r){ | |
if (l < r){ | |
int m = l+(r-l)/2; | |
mergeSort(arr, l, m); | |
mergeSort(arr, m+1, r); | |
merge(arr, l, m, r); | |
} | |
} | |
int main(){ | |
double utime0, stime0, wtime0,utime1, stime1, wtime1; //Variables para medición de tiempos | |
int j,i,n = 0; | |
for(j=0; j<50; j++){ | |
n += 20000; | |
printf("\n------------------- NUMERO %d --------------------- \n",j+1); | |
printf("Ordenando %d datos",n); | |
int k,A[n],s; k=0; | |
FILE *fp = fopen("./DatosAleatorios2.txt","r"); | |
fscanf(fp,"%d",&s); | |
while(k<n){ | |
A[k] = s; | |
fscanf(fp,"%d",&s); k++; | |
} | |
fclose(fp); | |
uswtime(&utime0, &stime0, &wtime0); // Medicion Mode: ON! :v | |
//printf("Arreglo Original: "); | |
//for(i=0; i<n; i++) printf("%d ",A[i]); printf("\n"); printf("\n"); | |
mergeSort(A, 0, n-1); | |
//printf("Arreglo Ordenado: "); | |
//for(i=0; i<n; i++) printf("%d ",A[i]); printf("\n"); | |
uswtime(&utime1, &stime1, &wtime1); | |
//Cálculo del tiempo de ejecución del programa | |
printf("\n"); | |
printf("real (Tiempo total) %.10f s\n", wtime1 - wtime0); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment