ArrayHelper<T> for Reverse and Sort

imageI am looking for a best way… Can you advice me something better?

using System;
using System.Collections.Generic;

namespace ArraySandbox {
  class Program {
    static void Main(string[] args) {
      var array = new int[] { 1, 2, 3, 5, 5, 10, -10, 20, -50, 30000 };

      Console.WriteLine("An Array: {0}", string.Join(",",
        array));
      Console.WriteLine("Rrversed: {0}", string.Join(",",
        ArrayHelper<int>.Reversed(array)));
      Console.WriteLine("Sorted:   {0}", string.Join(",",
        ArrayHelper<int>.Sorted(array)));
      Console.ReadKey(true);
    }
  }

  public static class ArrayHelper<T> {
    public static IEnumerable<T> Reversed(T[] array) {
      for (var i = array.Length - 1; i >= 0; --i)
        yield return array[i];
    }
    public static IEnumerable<T> Sorted(T[] array) {
      var list = new List<T>(array);
      list.Sort();
      foreach (var value in list) {
          yield return value;
      }
    }
    public static IEnumerable<T> SortedBinaryTree(T[] array) {
      var sorter = new SortedList<T, int>();
      var counter = 0;
      foreach (var value in array)
        if (!sorter.TryGetValue(value, out counter))
          sorter.Add(value, 1);
        else
          sorter[value] = counter + 1;
      foreach (var sort in sorter)
        for(var i = 0; i < sort.Value; ++i)
          yield return sort.Key;
    }
    public static IEnumerable<int> SortedInts(int[] array, int min, int max) {
      var length = max - min;
      var zero = -min;
      var counters = new int[max - min];
      for (int i = 0; i < array.Length; ++i)
        ++counters[zero + array[i]];
      for (int i = 0; i < counters.Length; ++i) {
        var counter = counters[i];
        for (int j = 0; j < counter; ++j)
          yield return i - zero;
      }
    }
  }
}

P ;).

Leave a Reply

Your email address will not be published. Required fields are marked *

*