C/Visual C - por favor ayudenme!!!

 
Vista:

por favor ayudenme!!!

Publicado por jose eduardo espinoza (2 intervenciones) el 30/09/2002 00:41:28
hola!!
les escribo por que necesito saber como calcular el tiempo de ordenacion con TIME para burbuja y shell. ademas quisiera saber como ordeno un archivo binario de cadenas, con los metodos de burbuja y shell.

gracias!!!!!
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Alejandro

Cálculo del tiempo de ordenación y ordenación de archivos binarios

Publicado por Alejandro (913 intervenciones) el 24/03/2025 19:23:25

Solución para calcular el tiempo de ordenación con `TIME` para los algoritmos de burbuja y shell:



Para medir el tiempo de ejecución de los algoritmos de ordenación, puedes utilizar la función `clock()` de la biblioteca `<time.h>`. Aquí tienes un ejemplo de cómo hacerlo para ambos algoritmos:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
void bubbleSort(int arr[], int n) {
    for (int i = 0; i < n-1; i++)
        for (int j = 0; j < n-i-1; j++)
            if (arr[j] > arr[j+1]) {
                // Intercambiar arr[j] y arr[j+1]
                int temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
}
 
void shellSort(int arr[], int n) {
    for (int gap = n/2; gap > 0; gap /= 2) {
        for (int i = gap; i < n; i++) {
            int temp = arr[i];
            int j;
            for (j = i; j >= gap && arr[j - gap] > temp; j -= gap)
                arr[j] = arr[j - gap];
            arr[j] = temp;
        }
    }
}
 
int main() {
    int n = 1000; // Tamaño del arreglo
    int arr[n];
 
    // Inicializar el arreglo con valores aleatorios
    for (int i = 0; i < n; i++) {
        arr[i] = rand() % 1000;
    }
 
    // Medir tiempo de bubble sort
    clock_t start = clock();
    bubbleSort(arr, n);
    clock_t end = clock();
    double time_spent = (double)(end - start) / CLOCKS_PER_SEC;
    printf("Tiempo de bubble sort: %f segundos\n", time_spent);
 
    // Reiniciar el arreglo
    for (int i = 0; i < n; i++) {
        arr[i] = rand() % 1000;
    }
 
    // Medir tiempo de shell sort
    start = clock();
    shellSort(arr, n);
    end = clock();
    time_spent = (double)(end - start) / CLOCKS_PER_SEC;
    printf("Tiempo de shell sort: %f segundos\n", time_spent);
 
    return 0;
}

Solución para ordenar un archivo binario de cadenas utilizando los métodos de burbuja y shell:



Para ordenar un archivo binario que contiene cadenas, primero debes leer las cadenas del archivo, almacenarlas en un arreglo y luego aplicar los algoritmos de ordenación. Aquí tienes un ejemplo básico:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
#define MAX_STR_LEN 100
#define MAX_STR_COUNT 1000
 
void bubbleSort(char arr[][MAX_STR_LEN], int n) {
    for (int i = 0; i < n-1; i++)
        for (int j = 0; j < n-i-1; j++)
            if (strcmp(arr[j], arr[j+1]) > 0) {
                // Intercambiar arr[j] y arr[j+1]
                char temp[MAX_STR_LEN];
                strcpy(temp, arr[j]);
                strcpy(arr[j], arr[j+1]);
                strcpy(arr[j+1], temp);
            }
}
 
void shellSort(char arr[][MAX_STR_LEN], int n) {
    for (int gap = n/2; gap > 0; gap /= 2) {
        for (int i = gap; i < n; i++) {
            char temp[MAX_STR_LEN];
            strcpy(temp, arr[i]);
            int j;
            for (j = i; j >= gap && strcmp(arr[j - gap], temp) > 0; j -= gap)
                strcpy(arr[j], arr[j - gap]);
            strcpy(arr[j], temp);
        }
    }
}
 
int main() {
    FILE *file = fopen("cadenas.bin", "rb");
    if (!file) {
        perror("No se pudo abrir el archivo");
        return EXIT_FAILURE;
    }
 
    char arr[MAX_STR_COUNT][MAX_STR_LEN];
    int count = 0;
 
    // Leer cadenas del archivo
    while (fread(arr[count], sizeof(char), MAX_STR_LEN, file) == MAX_STR_LEN) {
        count++;
    }
    fclose(file);
 
    // Ordenar usando bubble sort
    bubbleSort(arr, count);
 
    // Guardar el resultado en un nuevo archivo
    file = fopen("cadenas_ordenadas_bubble.bin", "wb");
    fwrite(arr, sizeof(char), count * MAX_STR_LEN, file);
    fclose(file);
 
    // Reiniciar el arreglo
    file = fopen("cadenas.bin", "rb");
    fread(arr, sizeof(char), count * MAX_STR_LEN, file);
    fclose(file);
 
    // Ordenar usando shell sort
    shellSort(arr, count);
 
    // Guardar el resultado en un nuevo archivo
    file = fopen("cadenas_ordenadas_shell.bin", "wb");
    fwrite(arr, sizeof(char), count * MAX_STR_LEN, file);
    fclose(file);
 
    return 0;
}

Notas:


- Asegúrate de que el archivo binario `cadenas.bin` contenga cadenas de longitud máxima `MAX_STR_LEN`.
- Este código es un ejemplo básico y puede requerir ajustes según tus necesidades específicas.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar