In "Programming Zhu Ji Nanxiong" (2nd Edition) of chapter 1th (P4), the author describes a problem as follows:
1. Description of the problem
input: a file with a maximum of n positive integers, each of which is less than N, where n=10^7. If any integer in the input file recurs, it is a fatal error. No other data is associated with the integer.
output: a list of input integers sorted in ascending order.
constraints: up to (approximately) 1MB of memory space is available and sufficient disk storage space is available. Running time of up to a few minutes and running time of 10 seconds does not require further optimization.
2. Problem analysis
1MB has about 8 million bits, if the amount of data that needs to be sorted is less than or just equal to 1MB (the maximum value of a single data cannot exceed 1MB, and any two data is not equal), can we directly use this 1MB bit to represent the number that needs to be sorted? The problem is of course possible. The method is the same as the five-Eratosthenes screening method of several methods of judging prime numbers in a blog post. The result of this is that, while occupying space, it has a very time advantage.
3. Workaround
The author's approach in the book is to write a program in three stages, given the bitmap data structure of an integer set in a file:
1) Set all the bits to 0, thereby initializing the set to null;
2) Set the set by reading each integer in the file and set each corresponding bit to 1;
3) test each bit, if the bit is 1, output the corresponding integer, resulting in an ordered output file.
4. Code implementation
I implemented the C + + code as follows (if you already know the upper limit of the input, it is not necessary to determine the maximum value of the input vector, see the following code):
1vector<int> Sortnumbers::sort (vector<int>nums)2 {3 intSZ =nums.size ();4 intMax =0;5 for(inti =0; I < sz; i++)6 {7 if(Nums[i] >max)8Max =Nums[i];9 }Ten Onevector<BOOL>isexist; A for(inti =0; I < Max +1; i++) -Isexist.push_back (false); - the for(inti =0; I < sz; i++) -Isexist[nums[i]] =true; - -vector<int>Sorted; + for(inti =0; I < Max +1; i++) - { + if(Isexist[i]) A Sorted.push_back (i); at } - - returnSorted; -}
The complete Code and unit test code are as follows:
Source:
1 #ifndef Sortnumbers_h2 #defineSortnumbers_h3 4#include <iostream>5#include <vector>6 using namespacestd;7 8 classsortnumbers9 {Ten Public: One sortnumbers (); A Virtual~sortnumbers (); -vector<int> Sort (vector<int>nums); - the }; - - sortnumbers::sortnumbers () - { + - } + Asortnumbers::~sortnumbers () at { - - } - -vector<int> Sortnumbers::sort (vector<int>nums) - { in intSZ =nums.size (); - intMax =0; to for(inti =0; I < sz; i++) + { - if(Nums[i] >max) theMax =Nums[i]; * } $ Panax Notoginsengvector<BOOL>isexist; - for(inti =0; I < Max +1; i++) theIsexist.push_back (false); + A for(inti =0; I < sz; i++) theIsexist[nums[i]] =true; + -vector<int>Sorted; $ for(inti =0; I < Max +1; i++) $ { - if(Isexist[i]) - Sorted.push_back (i); the } - Wuyi returnSorted; the } - Wu - #endif
sortnumbers.hpp
Boost Unit test code (only proven correct):
1 #defineBoost_test_module Sortnumbers_test_module2 3#include"stdafx.h"4#include".. /SORTNUMBERS/SORTNUMBERS.HPP"5 6 structsortnumbers_fixture7 {8 sortnumbers_fixture ()9 {TenTest =Newsortnumbers; One } A~sortnumbers_fixture () - { - Delete test; the } - -Sortnumbers *test; - }; + - Boost_fixture_test_suite (Sortnumbers_test_suite, sortnumbers_fixture) + A boost_auto_test_case (Sort) at { - intin_array[ the] = { A, One, -,4,5, -, -,1, -,2, -, -,222, -, + }; - intout_array[ the] = {1,2,3,4,5, One, -, -, -, A, -, +, -,222, - }; -vector<int>inch, out; - for(inti =0; I < the; i++) - inch. Push_back (In_array[i]); in - out= Test->sort (inch); to for(inti =0; I < the; i++) +Boost_require ( out[I] =out_array[i]); - the } * $ Panax NotoginsengBoost_auto_test_suite_end ()
BoostUnitTest.cpp
"Chapter One": Sorting integers