各种排序

 

//Insertsort

template <typename Type)

void Insertsort(Type k[],int n)

{

    int i;

    int j;

    Type temp;

    for(i = 1; i<n; i++)

    {

        temp = k[i];

        j = i-1;

        while(j>=0 && temp<k[j])

        {

            k[j+1] = k[j--];

        }

        k[j+1] = temp;

    }

}

 

//SelectSort

template <typename Type>

void SelectSort(Type k[], int n)

{

    int i,j,d;

    type temp;

    for(i = 0; i< n; i++)

    {

        d = i;

        for(j=i+1; j<n; j++)

            if(k[j] > k[d])

                d = j;

        if(d != i)

        {

            temp = k[d];

            k[d] = k[i];

            k[i] = temp;

        }

    }

}

 

//Bubblesort

templatr <typename Type>

void Bubblesort(Type k[], int n)

{

    int i = n;

    int j;

    int flag = 1;

    Type temp;

    while(i>0 && flag == 1)

    {

        flag = 0;

        for(j = 0; j<n; j++)

        {

            if(k[j] < k[j+1])

            {

                 flag = 1;

                 temp = k[j];

                 k[j] = k[j+1];

                 k[j+1] = temp;

             }

        }

         i--;

    }

}

 

void quicksort(int num[], int lower, int upper)
{
  int pivot;
  int partition(int [], int, int);

  pivot = partition(num, lower, upper);

  if (lower < pivot)
    quicksort(num, lower, pivot - 1);
  if (upper > pivot)
    quicksort(num, pivot + 1, upper);
  return;
}

int partition(int num[], int left, int right)
{
  int pivot, temp;

  pivot = num[left];  /* "capture" the pivot value, which frees up one slot */
  while (left < right)
  {
    /* scan from right to left */
    while(num[right] >= pivot && left < right)  /* skip over larger or equal values */
      right--;
    if (right != left)
    {
      num[left] = num[right];   /* move the higher value into the available slot */
      left++;
    }
    /* scan from left to right */
    while (num[left] <= pivot && left < right) /* skip over smaller or equal values */
      left++;
    if (right != left)
    {
      num[right] = num[left];  /* move lower value into the available slot */
      right-- ;
    }
  }
  num[left] = pivot;  /* move pivot into correct position */
  return(left);       /* return the pivot index */
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值