/* This program accepts an integer n on the command line, then fills an array of size n with random integers. The integers are sorted using merge sort. The order of the elements is verified and a checksum mod 65536 compared to ensure the elements were sorted properly. The time taken to do the sort is printed to standard output. */ #include #include #include int *v1, *v2; /* global pointers for merge */ /* prototype for missing heapsort */ void heap_sort (int [], int); /* this function returns 1 iff v[0..n-1] is sorted */ int is_sorted (int v[], int n) { int i; for (i=1; i\n", argv[0]); exit (1); } /* get the number */ n = atoi (argv[1]); /* get enough storage for v, w, and the subarrays for merge sort */ v = (int *) malloc (n * sizeof (int)); w = (int *) malloc (n * sizeof (int)); v1 = (int *) malloc (n * sizeof (int)); v2 = (int *) malloc (n * sizeof (int)); /* woops, out of memory */ if (!v || !w || !v1 || !v2) { fprintf (stderr, "memory allocation error!\n"); exit (1); } /* fill v with random numbers */ for (i=0; i