快速排序,效率挺高,平均时间复杂度O(n*log(n)),用C#写了一个

public class Algorithm
{
  private int[] team = new int[] { 0, 7, 9, 4, 1, 6, 22, 58, 74, 3, 2 }; 
  public void QuickSort(int[] team,int left,int right)
  {
    if (left >= right) return;
    int stand = team[left], i = left, j = right, flag = 0, temp;
    while (i != j)
    {
      //左移 ← ←
      if (flag == 0)
      {
        if (team[j] < stand) flag = 1;
        else j--;
      }
      // 右移 → →
      if (flag == 1)
      {
        if (team[i] > stand) flag = 2;
        else i++;
      }
      //交换
      if (flag == 2)
      {
        temp = team[j];
        team[j] = team[i];
        team[i] = temp;
        flag = 0;
      }
    }
    temp = team[left];
    team[left] = team[j];
    team[j] = temp;
    QuickSort(team, left, j-1);
    QuickSort(team, j+1, right);
  }

  public void Action()
  {
    QuickSort(team, 0, team.Length - 1);
    foreach (var item in team)
    {
      Console.WriteLine(item);
    }
  }
}

posted on 2014-12-11 16:08  catch_you  阅读(118)  评论(0编辑  收藏  举报