博客首页 注册 建议与交流 排行榜 加入友情链接
推荐 投诉 搜索: 帮助

ypxing

学而不思则罔,思而不学则殆

见贤思齐焉,见不贤而内自省也

人不知而不愠,不亦君子乎?

   ypxing.cublog.cn
关于作者  
姓名:星云鹏 (Yunpeng Xing)
职业:IT相关
年龄:28
位置:北京
个性介绍:
Love me, feed me, 
never leave me.
失败只有一种, 那就是半途而废

我的分类  




快排序

//快排序的平均时间复杂度为O(nlgn)

//最坏时间复杂度为O(n^2)

//空间复杂度为O(logn), 递归造成的

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#define ARRAY_SIZE 10

int partitionArray(int* sortArray, int st, int end)
{
  int i, j, temp;
  j = st+1; // j is from st+1 through end

  i = st;    // i is the exact one before any elements larger than pivot

  for (;j<=end;j++)
    if (*(sortArray+j)<=*(sortArray+st))
    {
      i++;
      if (i!=j)
      {
        temp = *(sortArray+i);
        *(sortArray+i) = *(sortArray+j);
        *(sortArray+j) = temp;
      }
    }
  temp = *(sortArray+i);
  *(sortArray+i) = *(sortArray+st);
  *(sortArray+st) = temp;
  return i;
}


void qSort(int* sortArray, int st, int end)
{
  int pivot;
  if (st<end)
  {
    pivot = partitionArray(sortArray, st, end);
    qSort(sortArray, st, pivot-1);
    qSort(sortArray, pivot+1, end);
  }
  
}



int main()
{
  int sortArray[ARRAY_SIZE]={30, 29, 45, 33, 21, 3, 108, 75, 99, 66};
  int i;
  qSort(sortArray, 0, 9);
  for(i=0;i<10;i++)
    printf("%d ", sortArray[i]);
  printf("\n");
}


其他排序方法

 发表于: 2007-07-18,修改于: 2007-11-15 14:54 已浏览447次,有评论0条 推荐 投诉

  网友评论

  发表评论



Copyright © 2001-2006 ChinaUnix.net All Rights Reserved

感谢所有关心和支持过ChinaUnix的朋友们
页面生成时间:0.01664

京ICP证041476号