Disk File Sorting Programming

Source: Internet
Author: User

I started to look at programming. The first problem was the sorting of disks. I thought it was just merging and sorting, but the question required 1 MB of memory. This algorithm is not feasible. Programming Pearl writes to the use of Bitmap (two operations can be successfully achieved, less than 1 MB of memory). For details, see the first chapter of programming Pearl.

Question: give 10 ^ 7 data and sort big data. The memory size is only 1 MB, which is acceptable in a short time.

Solution: 1. Merge multiple channels. 2. Bitmap operations.

I read July's article here, and it's really good. Recommended: http://blog.csdn.net/v_JULY_v/article/details/6451990

A program that generates different random numbers is included below:

1 # include <iostream> 2 # include <time. h> 3 # include <assert. h> 4 using namespace STD; 5 6 const int size = 10000000; 7 int num [size]; 8 9 int main () 10 {11 int N; 12 file * fp = fopen ("data.txt", "W"); 13 assert (FP); 14 15 for (n = 1; n <= size; n ++) 16 // n = 0; n <size. As a result, the test data of a small program is 0, which is hereby corrected. 17 num [N] = N; 18 srand (unsigned) Time (null); 19 int I, j; 20 21 for (n = 0; n <size; N ++) 22 {23 I = (RAND () * rand_max + rand () % 10000000; 24 J = (RAND () * rand_max + rand () % 10000000; 25 swap (Num [I], num [J]); 26} 27 28 for (n = 0; n <size; n ++) 29 fprintf (FP, "% d", num [N]); 30 fclose (FP); 31 return 0; 32}

The bitmap program sorts the data by sorting the first 5000000 (each number corresponds to a bit) and the remaining 5000000 (each time it is smaller than 1 m). The Code is as follows (simulated twice ):

1 # include <iostream> 2 # include <bitset> 3 # include <assert. h> 4 # include <time. h> 5 using namespace STD; 6 7 const int max_each_scan = 5000000; 8 9 int main () 10 {11 clock_t begin = clock (); 12 bitset <max_each_scan> bit_map; 13 bit_map.reset (); 14 15 // open the file with the unsorted data 16 file * fp_unsort_file = fopen ("data.txt", "R"); 17 assert (fp_unsort_file ); 18 int num; 19 20 // The first Time scan to sort the data between 0-4999999 21 While (fscanf (fp_unsort_file, "% d", & num )! = EOF) 22 {23 if (Num <max_each_scan) 24 bit_map.set (Num, 1); 25} 26 27 file * fp_sort_file = fopen ("sort.txt", "W "); 28 assert (fp_sort_file); 29 int I; 30 31 // write the sorted data into file 32 for (I = 0; I <max_each_scan; I ++) 33 {34 if (bit_map [I] = 1) 35 fprintf (fp_sort_file, "% d", I ); 36} 37 38 // The second time scan to sort the data between 5000000-9999999 39 int result = fseek (F P_unsort_file, 0, seek_set); 40 if (result) 41 cout <"fseek failed! "<Endl; 42 else 43 {44 bit_map.reset (); 45 while (fscanf (fp_unsort_file," % d ", & num )! = EOF) 46 {47 If (Num> = max_each_scan & num <10000000) 48 {49 num-= max_each_scan; 50 bit_map.set (Num, 1 ); 51} 52} 53 for (I = 0; I <max_each_scan; I ++) 54 {55 if (bit_map [I] = 1) 56 fprintf (fp_sort_file, "% d", I + max_each_scan); 57} 58} 59 60 clock_t end = clock (); 61 cout <"bitmap method, time consumed:" <Endl; 62 cout <(end-begin)/clk_tck <"S" <Endl; 63 fclose (fp_sort_file); 64 fclose (fp_unsort_file); 65 return 0; 66}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.