钻石阵C语言排序

钻石阵C语言排序

本文介绍了钻石阵C语言排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的家庭作业在C基本上,我需要一种方法,而不是一个解决方案。

I have the following homework assignment in C. I basically need an approach rather than a solution.

我们有一个13×13阵列。在数组中,我们有我们需要考虑一个菱形。这颗钻石以外的一切被初始化为-1(不重要)。实施例5×5阵列下方 -

We have a 13 x 13 array. In the array, we have a diamond shape that we need to consider. Everything outside this diamond is initialized to -1 (unimportant). Example 5 x 5 array below-

x x 1 x x
x 2 2 2 x
3 3 3 3 3
x 4 4 4 x
x x 5 x x

x=-1

现在该数组中,我们已经在钻石每个条目的值包含11位。 5 LSB包含一个数据(色调),以及其他6包含另一个数据(直径)。我们需要的数据逐行逐列进行排序,单调的色彩,然后为直径,单调。

Now in this array, the values we have in the diamond for each entry contains 11 bits. 5 lsb contains one data (hue), and other 6 contains another data (diameter). We need to sort the data row-wise, monotonically for the hue, and then column-wise for the diameter, monotonically.

什么是这样做的最有效和最节省内存的方式?因为我们需要保护这一点,这是最好的,如果该项目被交换围绕,而不是创造另一个数组。最后,我们会最终有一个排序的钻石阵列(仍与-1s)。感谢很多提前家伙!

What would be the most efficient and memory conserving way of doing this? Since we need to conserve this, it's best if the entries are swapped around rather than creating another array. In the end, we will end up with a sorted diamond array (still with the -1s). Thanks a lot in advance guys!

推荐答案

我不明白你想究竟如何重新排序的元素

I didn't understand how exactly you want to reorder the elements

行明智的,单调的色彩,然后列明智的直径,单调

但这里有一些想法,你可能能够使用。

but here are some ideas you might be able to use.

  • 您的阵列是13x13(169元);出的是,几乎整整一半(​​84)是空的,所以你可以把它们作为临时存储(用于如基数-sort )。
  • 您的值有11个有意义的位;在实际的计算机数量有16或32位 - 这样你就可以使用5(或21,取决于你的系统)最显著位作为临时存储
  • 一个使用高5位可能是很好的方法是把5 LSB(色调)的副本出现。这将正常操作的整数比较(使色调比直径更显著)时反转两个部分的意义
  • Your array is 13x13 (169 elements); out of that, almost exactly half (84) are empty, so you can use them as temporary storage (for e.g. radix-sort).
  • Your values have 11 meaningful bits; numbers in real computers have either 16 or 32 bits - so you can use the 5 (or 21, depending on your system) most significant bits as temporary storage.
  • One possibly good way to use the upper 5 bits is putting a copy of the 5 LSB (hue) there. This will reverse the significance of the two parts when doing normal integer comparison (making hue more significant than diameter)

这篇关于钻石阵C语言排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 19:28