快速排序(n 个数组应视为 1,并根据需要重新映射值)
我有一个数组链接列表(结构位于帖子底部)
每个数组可能具有如下例所示的值
Array1[] = {6,36,8,23};
Array2[] = {8,23,5,73};
Array3[] = {2,5,1,9};
我需要对这些值进行排序,以便所有 3 个数组都被视为 1 个大数组...
我需要使用快速排序,以便它使用就地处理...我正在处理非常大的数组,无法使用额外的内存..
结果应该是这样的
Array1[] = {1,2,5,5};
Array2[] = {6,8,8,9};
Array3[] = {23,23,36,73};
目前我只能单独对每个数组进行排序...但这并不完全是我想要的需要 :(
struct iSection {
unsigned long Section_Count; // Total # of points in this block of memory
int *Section_Arr; // Point cloud for current block of memory
struct iSection *Next; // Pointer to next section
} iSection;
struct iDatabase {
struct iSection *First_Section;
struct iSection *Last_Section;
} iDatabase;
I have a linked list of arrays (struct at bottom of post)
Each array may have values like the below example
Array1[] = {6,36,8,23};
Array2[] = {8,23,5,73};
Array3[] = {2,5,1,9};
I need to sort these so that all 3 arrays are treated as 1 large array...
I need to use quicksort so that it uses in-place processing... I am working with very large arrays and cannot afford to use additional memory..
The result should be something like this
Array1[] = {1,2,5,5};
Array2[] = {6,8,8,9};
Array3[] = {23,23,36,73};
Currently i am only able to sort each array individually... but thats not exactly what i need :(
struct iSection {
unsigned long Section_Count; // Total # of points in this block of memory
int *Section_Arr; // Point cloud for current block of memory
struct iSection *Next; // Pointer to next section
} iSection;
struct iDatabase {
struct iSection *First_Section;
struct iSection *Last_Section;
} iDatabase;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这并不难,更多的是接口问题而不是算法问题。
编写一个包装器容器,提供用于访问成员和写入的接口(例如 C++ 中的
operator[]
),并在内部将size_t index
参数映射到正确的数组。尽管这个包装类确实需要每个数组的大小才能正确映射索引。伪代码 operator[] 的示例如下:
然后使用此包装类,就像在快速排序算法中使用普通容器一样。
It's not that hard, more an interfacing issue then an algorithmics issue.
Write a wrapper container that provides an interface for accessing members and writing (say
operator[]
in C++) and internally it maps thesize_t index
argument to the right array. This wrapper class does need the size of every array though to be able to correctly map the index.An example pseudocode operator[] would be:
Then use this wrapper class as you would use a normal container in your Quicksort algorith.
如果您可以确保
Array1、Array2 和 Array3
相继声明并且处于连续的内存布局中,那么您可以在sort()
并给出所有数组的组合大小。要检查连续对齐,您可以使用以下技巧。
用法,
这是3个数组的例子,你可以根据自己的需要来制作。您可以根据您的机器传递 Array1,2,3 或 Array3,2,1。
If you can make sure that
Array1, Array2, and Array3
are declared one after another and in continuous memory layout, then you can give theArray1
(the first one) in thesort()
and give the combined size of all the arrays.To check the continuous alignment you can use following trick.
Usage,
This is an example for 3 arrays, you can make it according to your need. And you can pass Array1,2,3 or Array3,2,1 depending on your machine.
仅在我的大脑中进行了测试:
所以如果你有类似的东西:
Tested only in my brain:
so if you have something like: