【问题标题】:How do I sort a list of integers using only one additional integer variable?如何仅使用一个额外的整数变量对整数列表进行排序?
【发布时间】:2008-09-25 10:27:18
【问题描述】:

如何仅使用一个变量对值列表进行排序?

编辑:根据@Igor 的评论,我重新命名了这个问题。

【问题讨论】:

  • 您能补充一点信息吗?什么清单?什么变量?由于我不是 PHP 程序员,我不知道这些是否显而易见。只是一个建议。
  • 我只想使用一个附加变量对整数列表进行排序。有可能吗?
  • 可以,但是很麻烦。

标签: c# java algorithm sorting puzzle


【解决方案1】:

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 程序相同。

【讨论】:

    【解决方案2】:

    我怀疑我正在为你做作业,但这是一个有趣的挑战。这是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
    

    【讨论】:

      【解决方案3】:

      您可以为每个可能的列表大小生成/编写大量排序网络。在排序网络中,您使用单个变量进行交换操作。

      我不建议您在软件中执行此操作,但仍有可能。

      这是 C 中所有 n 到 4 的排序例程

      // 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

      【讨论】:

      • 很有趣,但似乎有点不切实际。
      • 这是不切实际的。如果您进行硬件设计,排序网络很有用,因为您可以并行/同时进行多个比较和交换,但在软件中,循环几乎总是更快。甚至 vor 小 N
      • 这也超出了原始挑战的范围:您正在使用堆栈。
      【解决方案4】:

      在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));
          }
      }
      

      实际上,通过使用技巧proposed by Nils,您甚至可以消除剩余的一个 int 分配 - 当然这会添加到堆栈中...

      【讨论】:

        【解决方案5】:

        在红宝石中: [1, 5, 3, 7, 4, 2].排序

        【讨论】:

          【解决方案6】:

          你没有,它已经排序了。 (由于问题含糊不清,我假设变量是对象的同义词)

          【讨论】:

            【解决方案7】:

            如果您有一个列表(1 5 3 7 4 2) 和一个变量v,您可以交换列表的两个值,例如3 和7,首先将3 分配给v,然后将7 分配给位置3,最后把v的值赋值给原来的7的地方,之后就可以重复使用v进行下一次交换。为了排序,您只需要一个算法来告诉交换哪些值。您可以在 http://en.wikipedia.org/wiki/Sorting_algorithm 上寻找合适的算法。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-10-09
              • 2012-04-09
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多