/* recursively split array to small chunks and merge back */ void split_merge(int S[], int tmp_S[], int begin, int end) { int size, middle; size = end - begin; middle = begin + (size / 2); if (size < 2) return; split_merge(S, tmp_S, begin, middle); split_merge(S, tmp_S, middle, end); merge(S, tmp_S, begin, middle, end); }
// create random array and pass to mergesort main() { int random_arr[ARRLEN]; int tmp_arr[ARRLEN]; int i, number; i=0; FILE* fp = fopen("IntegerArray.txt", "r"); while(fscanf(fp, "%i", & number)==1 && i<100000) random_arr[i++] = number; //for(i = 0; i<ARRLEN; ++i) // random_arr[i] = rand() % (ARRLEN * 5); printf("starting with random array:\n"); print_array(random_arr, 0, ARRLEN); split_merge(random_arr, tmp_arr, 0, ARRLEN); printf("rearranged to order:\n"); print_array(random_arr, 0, ARRLEN); return 0; }
static int extract(int argc, char *argv[], int ispatch, int which) { /* extract a branch of a diff or diff3 or merge output * We need one file */ struct stream f, flist[3]; if (argc == 0) { fprintf(stderr, "%s: no file given for --extract\n", Cmd); return 2; } if (argc > 1) { fprintf(stderr, "%s: only give one file for --extract\n", Cmd); return 2; } f = load_file(argv[0]); if (f.body == NULL) { fprintf(stderr, "%s: cannot load file '%s' - %s\n", Cmd, argv[0], strerror(errno)); return 2; } if (ispatch) { if (split_patch(f, &flist[0], &flist[1]) == 0) { fprintf(stderr, "%s: No chunk found in patch: %s\n", Cmd, argv[0]); return 0; } } else { if (!split_merge(f, &flist[0], &flist[1], &flist[2])) { fprintf(stderr, "%s: merge file %s looks bad.\n", Cmd, argv[0]); return 2; } } if (flist[which-'1'].body == NULL) { fprintf(stderr, "%s: %s has no -%c component.\n", Cmd, argv[0], which); return 2; } else { if (write(1, flist[which-'1'].body, flist[which-'1'].len) != flist[which-'1'].len) return 2; } return 0; }
static int do_merge(int argc, char *argv[], int obj, int reverse, int replace, int ignore, int show_wiggles, int quiet) { /* merge three files, A B C, so changed between B and C get made to A */ struct stream f, flist[3]; struct file fl[3]; int i; int chunks1 = 0, chunks2 = 0, chunks3 = 0; char *replacename = NULL, *orignew = NULL; struct csl *csl1, *csl2; struct ci ci; FILE *outfile = stdout; switch (argc) { case 0: fprintf(stderr, "%s: no files given for --merge\n", Cmd); return 2; case 3: case 2: case 1: for (i = 0; i < argc; i++) { flist[i] = load_file(argv[i]); if (flist[i].body == NULL) { fprintf(stderr, "%s: cannot load file '%s' - %s\n", Cmd, argv[i], strerror(errno)); return 2; } } break; default: fprintf(stderr, "%s: too many files given for --merge\n", Cmd); return 2; } switch (argc) { case 1: /* a merge file */ f = flist[0]; if (!split_merge(f, &flist[0], &flist[1], &flist[2])) { fprintf(stderr, "%s: merge file %s looks bad.\n", Cmd, argv[0]); return 2; } break; case 2: /* a file and a patch */ f = flist[1]; chunks2 = chunks3 = split_patch(f, &flist[1], &flist[2]); break; case 3: /* three separate files */ break; } if (reverse) { f = flist[1]; flist[1] = flist[2]; flist[2] = f; } for (i = 0; i < 3; i++) { if (flist[i].body == NULL) { fprintf(stderr, "%s: file %d missing\n", Cmd, i); return 2; } } if (replace) { int fd; replacename = xmalloc(strlen(argv[0]) + 20); orignew = xmalloc(strlen(argv[0]) + 20); strcpy(replacename, argv[0]); strcpy(orignew, argv[0]); strcat(orignew, ".porig"); if (open(orignew, O_RDONLY) >= 0 || errno != ENOENT) { fprintf(stderr, "%s: %s already exists\n", Cmd, orignew); return 2; } strcat(replacename, "XXXXXX"); fd = mkstemp(replacename); if (fd == -1) { fprintf(stderr, "%s: could not create temporary file for %s\n", Cmd, replacename); return 2; } outfile = fdopen(fd, "w"); } if (obj == 'l') { fl[0] = split_stream(flist[0], ByLine); fl[1] = split_stream(flist[1], ByLine); fl[2] = split_stream(flist[2], ByLine); } else { fl[0] = split_stream(flist[0], ByWord); fl[1] = split_stream(flist[1], ByWord); fl[2] = split_stream(flist[2], ByWord); } if (chunks2 && !chunks1) csl1 = pdiff(fl[0], fl[1], chunks2); else csl1 = diff(fl[0], fl[1]); csl2 = diff_patch(fl[1], fl[2]); ci = make_merger(fl[0], fl[1], fl[2], csl1, csl2, obj == 'w', ignore, show_wiggles); print_merge(outfile, &fl[0], &fl[1], &fl[2], obj == 'w', ci.merger); if (!quiet && ci.conflicts) fprintf(stderr, "%d unresolved conflict%s found\n", ci.conflicts, ci.conflicts == 1 ? "" : "s"); if (!quiet && ci.ignored) fprintf(stderr, "%d already-applied change%s ignored\n", ci.ignored, ci.ignored == 1 ? "" : "s"); if (replace) { fclose(outfile); if (rename(argv[0], orignew) == 0 && rename(replacename, argv[0]) == 0) /* all ok */; else { fprintf(stderr, "%s: failed to move new file into place.\n", Cmd); return 2; } } return (ci.conflicts > 0); }