在文件中对整数排序 [英] Sorting integers in a file

查看:82
本文介绍了在文件中对整数排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友,我正在尝试访问一个包含10万数据的文件,我想对文件中的值进行排序(文件包含整数),我该怎么办?请帮帮我...

Hi friends i am trying to access a file with some one lakh data i want to sort the values in the file(file contains integers) how can i do it? pls help me out...

推荐答案

读取整个数据到数组或列表中.对数组或列表进行排序.覆盖文件.
Read the whole data to array or list. Sort the array or list. Overwrite the file.


Baji Jabbar 已经为您提供了正确的答案,但是看起来您仍然有麻烦.您对此有何疑问?
您是否能够读取文件并填充数组(您可能需要动态地(重新)分配内存以使所有整数适合数组,请参见 qsort [
Baji Jabbar already gave you the right answer, however it looks like you are still in trouble. What are your doubts about?
Are you able to read the file and fill the array (you may need to dynamicalli (re)allocate memory to fit all the integers into the array, see realloc at MSDN[^])?
Are you able to sort the array (you may use qsort[^] for the purpose)?

If C++ usage is an option then your task would be easier.
:)


确切的操作方式取决于数据的性质及其存储方式.所以...

-如果您的数据适合内存,请读取整个文件,对其进行排序并写回

-如果您的数据不适合内存,但适合您进程的地址空间,并且格式正确(适合您的处理器),请打开一个内存映射文件并对其进行排序

-如果您的数据处于文本模式,则必须先做些准备.考虑读取每个整数并将其输入频率表和/或根据数据分布对它们进行散列

如果您的数据真的很大,那么请开始考虑使用一些数据库样式的技术,例如在读入数据时对数据进行分区,对这些分区进行排序并再次写回.如果发现要处理的分区不适合内存,则可能需要考虑调整分区的大小.但是,这需要多次遍历源文件.

干杯,

Exactly how you do this depends on the nature of your data and how it''s stored. So...

- if your data will fit into memory read the entire file, sort it and write it back

- if your data won''t fit into memory but will fit into the address space of your process and is in binary form of the correct endianess for your processor open a memory mapped file and sort that

- if your data is in text mode then you''ll have to cook it a bit first. Consider reading each integer and enter them into a frequency table and/or hash them depending on the distribution of your data

If your data gets really big then start considering using some database style techniques like partioning the data as you read it in, sorting those partitions and writing them back out again. You might have to consider resizing the partitions if it turns out the one you''re processing won''t fit into memory. This requires multiple passes over the source file though.

Cheers,

Ash


这篇关于在文件中对整数排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆