Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <assert.h>
- #include "introprog_input_merge_sort.h"
- void merge(int* array, int first, int middle, int last)
- {
- int length = last - first + 1;
- int* b = (int*) calloc(length, sizeof(int));
- int k = first;
- int m = middle + 1;
- int i = 0;
- while (k <= middle && m <= last)
- {
- if (array[k] <= array[m]){
- b[i] = array[k];
- k += 1;
- }else {
- b[i] = array[m];
- m += 1;
- }
- i += 1;
- }
- while (k <= middle)
- {
- b[i] = array[k];
- k += 1;
- i += 1;
- }
- while (m <= last)
- {
- b[i] = array[m];
- m += 1;
- i += 1;
- }
- int j = 0;
- while (j < i)
- {
- array[first + j] = b[j];
- j += 1;
- }
- free(b);
- }
- void merge_sort(int* array, int first, int last)
- {
- if (first < last)
- {
- int v = (first + last)/2;
- int middle = v;
- merge_sort(array, first, middle);
- merge_sort(array, middle+1, last);
- merge(array, first, middle, last);
- }
- }
- int main (int argc, char *argv[])
- {
- if (argc!=3){
- printf ("usage: %s <maximale anzahl> <dateipfad>\n", argv[0]);
- exit(2);
- }
- char *filename = argv[2];
- int* array = (int*) calloc(atof(argv[1]), sizeof(int));
- /* int *array = (int*)malloc(atof(argv[1]) * sizeof(int));
- valgring fehler */
- int len = read_array_from_file(array, atoi(argv[1]), filename);
- printf("Eingabe:\n");
- print_array(array, len);
- merge_sort(array, 0, len-1);
- printf("Sortiert:\n");
- print_array(array, len);
- free(array);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement