2017-04-05 105 views
0

我想排序与计数排序双位数。试图将所有双打转换为整数,但由于某种原因没有任何反应。我的代码整理了整数。排序与计数排序

public static void CountingSort(DataArray items) { 
    // O(1) 
    int max = items[0]; 

    // O(N) 
    for (int i = 0; i < items.Length; i++) { 
     if (items[i] > max) { 
      max = items[i]; 
     } 
    } 

    // Space complexity O(N+K) 
    int[] counts = new int[max + 1]; 

    // O(N) 
    for (int i = 0; i < items.Length; i++) { 
     counts[items[i]]++; 
    } 
    // O(N+K) 
    int j = 0; 
    int c = items.Length - 1; 
    int current, previous; 
    for (int i = 0; i < counts.Length; i++) { 
    while (counts[i] > 0) { 
     while (c > j) { 
      if (items[c] == i) { 
      current = items[c]; 
      while (c != j) 
     { 
     previous = items[c - 1]; 
     items.Swap(c, current, previous); 
     c--; 
     } 
    } 
    c--; 
    } 
    j++; 
    counts[i]--; 
    c = items.Length - 1; 
    } 
    } 
} 

是否有可能通过计数排序来排序双数位?

回答

0

要排序double阵列,使用

double[] doubleArray = new double[5] { 8.1, 10.2, 2.5, 6.7, 3.3 }; 
doubleArray = Array.Sort(doubleArray); 

之后,使用一个循环计数。