收藏 (0) +1 (0) +1 (0) +1
收藏成功查看收藏>>

正在阅读:温故而知新:C++常用排序算法温故而知新:C++常用排序算法

2004-11-16 10:07 出处:CSDN 作者:yuguanglou 责任编辑:linjixiong

  

  while (leftPass<=rightPass)
  {
  //从左边及右边开始扫描子表
  smallIndex=leftPass;
  largeIndex=rightPass;
  
  //j和i遍历整个子表arr[LeftPass]~arr[rightPass]
  for (i=leftPass+1;i<rightPass;i++)
  //如果找到更小的元素,则将该位置赋值给smallIndex
  if (arr[i]<arr[smallIndex])
  smallIndex=i;
  
  //如果smallIndex和leftPass不在相同的位置
  //则将子表中的最小项与arr[pass]交换
  if (smallIndex!=leftPass)
  {
  temp=arr[leftPass];
  arr[leftPass]=arr[smallIndex];
  arr[smallIndex]=temp;
  }
  
  for (j=rightPass-1;j>leftPass;j--)
  if(arr[j]>arr[largeIndex])
  largeIndex=j;
  
  if(largeIndex!=rightPass)
  {
  temp=arr[rightPass];
  arr[rightPass]=arr[largeIndex];
  arr[largeIndex]=temp;
  }
  
  //从两头收缩子表
  leftPass++;
  rightPass--;
  }
  }

  //自编冒泡法排序算法函数bubbleSort()的实现


  template <typename T>
  int bubbleSort(T arr[],int n)
  {
  bool exchanged=false; //是否发生交换
  int i,j;              //用于遍历子表的下标
  T temp;               //用于交换元素的临时变量


察看评论详细内容 我要发表评论
作者笔名 简短内容 发表时间
:

键盘也能翻页,试试“← →”键

关注我们

最新资讯离线随时看 聊天吐槽赢奖品
手机访问回到顶部