如何仅使用一个添加的整数变量对整数列表进行排序?
时间:2020-03-06 14:42:50 来源:igfitidea点击:
如何仅使用一个变量对值列表进行排序?
编辑:根据@Igor的评论,我改了题。
解决方案
你不知道,它已经被排序了。 (由于问题很模糊,我将假定变量是对象的同义词)
我们可以为每种可能的列表大小生成/编写很多排序网络。在排序网络内部,我们使用单个变量进行交换操作。
我不建议我们在软件中执行此操作,但是仍然可以。
这是C中最多n个所有n的排序例程
// define a compare and swap macro
#define order(a,b) if ((a)<(b)) { temp=(a); (a) = (b); (b) = temp; }
static void sort2 (int *data)
// sort-network for two numbers
{
int temp;
order (data[0], data[1]);
}
static void sort3 (int *data)
// sort-network for three numbers
{
int temp;
order (data[0], data[1]);
order (data[0], data[2]);
order (data[1], data[2]);
}
static void sort4 (int *data)
// sort-network for four numbers
{
int temp;
order (data[0], data[2]);
order (data[1], data[3]);
order (data[0], data[1]);
order (data[2], data[3]);
order (data[1], data[2]);
}
void sort (int *data, int n)
{
switch (n)
{
case 0:
case 1:
break;
case 2:
sort2 (data);
break;
case 3:
sort3 (data);
break;
case 4:
sort4 (data);
break;
default:
// Sorts for n>4 are left as an exercise for the reader
abort();
}
}
显然,我们需要为每个可能的N提供一个排序网络代码。
更多信息在这里:
http://en.wikipedia.org/wiki/Sorting_network
我怀疑我正在为我们做作业,但是嘿,这是一个有趣的挑战。这是Icon中的解决方案:
procedure mysort(thelist)
local n # the one integer variable
every n := (1 to *thelist & 1 to *thelist-1) do
if thelist[n] > thelist[n+1] then thelist[n] :=: thelist[n+1]
return thelist
end
procedure main(args)
every write(!mysort([4,7,2,4,1,10,3]))
end
输出:
1 2 3 4 4 7 10
C语言的解决方案:
#include <stdio.h>
int main()
{
int list[]={4,7,2,4,1,10,3};
int n; // the one int variable
startsort:
for (n=0; n< sizeof(list)/sizeof(int)-1; ++n)
if (list[n] > list[n+1]) {
list[n] ^= list[n+1];
list[n+1] ^= list[n];
list[n] ^= list[n+1];
goto startsort;
}
for (n=0; n< sizeof(list)/sizeof(int); ++n)
printf("%d\n",list[n]);
return 0;
}
输出当然与Icon程序的输出相同。
在Java中:
import java.util.Arrays;
/**
* Does a bubble sort without allocating extra memory
*
*/
public class Sort {
// Implements bubble sort very inefficiently for CPU but with minimal variable declarations
public static void sort(int[] array) {
int index=0;
while(true) {
next:
{
// Scan for correct sorting. Wasteful, but avoids using a boolean parameter
for (index=0;index<array.length-1;index++) {
if (array[index]>array[index+1]) break next;
}
// Array is now correctly sorted
return;
}
// Now swap. We don't need to rescan from the start
for (;index<array.length-1;index++) {
if (array[index]>array[index+1]) {
// use xor trick to avoid using an extra integer
array[index]^=array[index+1];
array[index+1]^=array[index];
array[index]^=array[index+1];
}
}
}
}
public static void main(final String argv[]) {
int[] array=new int[] {4,7,2,4,1,10,3};
sort(array);
System.out.println(Arrays.toString(array));
}
}
实际上,通过使用Nils提出的技巧,我们甚至可以消除剩下的一个int分配,尽管这当然会添加到堆栈中...
如果我们有一个列表((1 5 3 7 4 2))和一个变量v,则可以通过首先将3赋给v,然后赋值来交换列表的两个值,例如3和7 7到3的位置,最后将v的值赋给7的原始位置。之后,我们可以将" v`重复用于下一次交换。为了排序,我们只需要一种算法即可告诉我们要交换的值。我们可以在http://en.wikipedia.org/wiki/Sorting_algorithm上寻找合适的算法。
在红宝石中:
[1、5、3、7、4、2] .sort

