示例#1
0
void split_and_sort(int a[], int low, int high){
	if(high > low){
		int mid = (high + low)/2;
		split_and_sort(a, low, mid);
		split_and_sort(a, mid+1, high);
		merge(a, low, mid, high);
	}
}
示例#2
0
int main(int argc, char const *argv[]){
	
	int to_be_sort[] = {2,3,1,6,4,7,9};

	split_and_sort(to_be_sort, 0, 6);

	int i=0;
	for(;i<7;i++){
		printf("%d\n", to_be_sort[i]);
	}

	return 0;
}
示例#3
0
int main(int argc, char ** argv)
{
    if (argc != 3) {
        fprintf(stderr, "usage: %s <input> <output>\n", argv[0]);
        return EXIT_FAILURE;
    }

    try {
        size_t size = get_available_mem_size();
        void *p = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
        if (p==MAP_FAILED) {
            throw std::runtime_error(
                format_message_with_errno(
                    errno,
                    "Allocating %zu bytes of memory", size));
        }
        sort_element::base = p;

        mem_chunk available_mem = mem_chunk(p, size).aligned();

        file_id_t src_file = file_id::create_with_path(argv[1]);
        file_id_t dest_file = file_id::create_with_path(argv[2]);

        dest_file->set_auto_unlink(true);

        std::deque<file_id_t> transient_files;
        split_and_sort(available_mem, src_file, dest_file, transient_files);
        merge_sorted(available_mem, src_file, dest_file, transient_files);

        dest_file->set_auto_unlink(false);

        return EXIT_SUCCESS;
    }
    catch (const std::logic_error &e) {
        fprintf(stderr, "%s: Internal error: %s\n", argv[0], e.what());
        return EXIT_FAILURE;
    }
    catch (const std::exception &e) {
        fprintf(stderr, "%s: %s\n", argv[0], e.what());
        return EXIT_FAILURE;
    }
}