Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import std.stdio;
- import std.string;
- import std.file;
- import std.conv;
- import std.algorithm.iteration;
- import std.algorithm.sorting;
- import std.array;
- import std.functional;
- alias toInt = compose!(to!int, stripLeft);
- alias readInts = compose!(array, map!toInt , lineSplitter ,chomp, readText);
- T[] mergesort(T=int)(T[] arr) {
- T[] merge(T)(T[] a, T[]b) {
- T[] result = new T[a.length + b.length];
- auto frame = result[0..$];
- while ((a.length >0) && (b.length > 0)) {
- if (a[0] < b[0]) {
- frame[0] = a[0];
- a = a[1..$];
- }
- else {
- frame[0] = b[0];
- b = b[1..$];
- }
- frame = frame[1..$];
- }
- a ~= b;
- result[$-a.length..$] = a;
- return result;
- }
- return (arr.length < 2) ? arr : merge(mergesort(arr[0..$/2]), mergesort(arr[$/2..$]));
- }
- int main(string[] args)
- {
- int[] values = readInts(args[1]);
- int[] sorted = mergesort(values);
- sort(values);
- return (values == sorted) ? 0 : -1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement