排序算法

" Hello World !"

 1.冒泡排序
http://www.cnblogs.com/cj723/archive/2011/04/15/2016679.html
http://www.cnblogs.com/cj723/archive/2011/04/15/2016689.html

public void BubbleSort(int[] array)
       { 
//flag  是为了当当前数组是按正确顺序排列时就退出循环避免无谓的循环
            bool flag = true;
            for (int i = 0; i < array.Length && flag; i++)
            {
                flag = false;
//j>i  由于每次大循环之后都会将"当前"最小值排到前面所以下次时就无需比较这些最小值了
                for (int j = array.Length-1; j > i; j--)
                {
                    if (array[j-1]>array[j])
                    {
                        Swap(ref array[j],ref array[j - 1]);
//flag为true说明该次大循环有大小值的交换 进行了排序  需要进行下一次大循环
                        flag = true;
                    }
                }
            }
        }

 
2.简单选择排序
http://www.cnblogs.com/cj723/archive/2011/04/18/2019536.html

  public void SimpleSelectSort(int[] array)
      {
            int min;
            for (int i = 0; i < array.Length; i++)
            {
                min = i;  //暂时将当前下标的元素设为最小值的下标
//然后依次与该元素后面的元素比较,如果后面有元素比这个元素小,就将那个元素的下标设为最小值的下标,只是标记并没有交换
                for (int j = i + 1; j < array.Length; j++)
                {
                    if (array[min] > array[j])
                    {
                        min = j;
                    }
                }
//如果最小下标不与当前下标i相等,说明有比下标i代表的元素小的元素存在,交换,然后i增加1 对新元素进行同样操作
                if (i != min) 
                {
                    Swap(ref array[min], ref array[i]);
                }
            }
      }

 
3.直接插入排序
http://mintelong.iteye.com/blog/467832

public void StraightInsertSort(int[] array)
   {
         int i, j,tmp;
//从第2个数开始插入前面的有序数列
         for (i = 1; i < array.Length; i++)
         {
//将此时的数保存到一个临时变量里
            tmp = array[i];
//与此数之前的数做比较
            j = i - 1;
//不断与此数之前的数比较 直到遇到比此数小的数   j>=0要放在前面否则当j减到等于-1时会报错  如派这些数字时55,88,5,8,77,7,66,6,22,2,33,3   
            while (j>=0&&tmp<array[j])
            {
//将此数之前的 比此数大的数向后移
               array[j + 1] = array[j];
               j--;
             }
//将此数放在正确的位置上
            array[j+1] = tmp;
          }
     }

  
4.希尔排序

http://student.zjzk.cn/course_ware/data_structure/web/PAIXU/paixu8.2.2.1.htm

http://blog.sina.com.cn/s/blog_4b7bde1b0100ydcf.html

 public void ShellSort(int[] array)
        {
            int len = array.Length;
            for (int step = len / 2; step > 0; step /= 2)
            {
                for (int i = 0; i < step; i++)
                {
                    SortGroup(array, len, i, step);
                }
            }
        }
        public void SortGroup(int[] array, int len, int begin, int step)
        {
            for (int i = begin + step; i < len; i += step)
            {
                for (int j = begin; j < i; j += step)
                {
                    if (array[i] < array[j])
                    {
                        int temp = array[i];
                        //for (int k = i; k > j; k -= step)
                        //{
                        //    array[k] = array[k - step];
                        //}
                        array[i] = array[j];
                        array[j] = temp;
                    }
                }
            }
        }

 

5.堆排序

http://www.cnblogs.com/cj723/archive/2011/04/22/2024269.html

下面使用C#实现的,因为C#数组下标是从0开始的,所以与以上连接有所不同

public void myHeapSort(int[] array)
        {
            for (int i = array.Length / 2 - 1; i >= 0; i--)
            {  //初始化大顶堆(通用方法)
                HeapAdjust(array, i, array.Length - 1);
            }
            for (int i = array.Length - 1; i > 0; i--)
            { //将大顶堆最顶层的数(最大的数)与二叉树最后一个数交换,然后将剩下的数再次构成大顶堆,直到只剩下一个数(这个数就是最小的了)
                Swap(ref array[0], ref array[i]);
                HeapAdjust(array, 0, i - 1); //初始化大顶堆后才可以像这样构建大顶堆(不通用方法)
            }
        }
        public void HeapAdjust(int[] array,int s,int m) //创建大顶堆
        {
            int temp = array[s];
            for (int j =s*2+1; j <=m; j=j*2+1)
            {
                if (j<m&&array[j]<array[j+1])
                {
                    j++;
                }
                if (temp>array[j])
                {
                    break;
                }
                array[s] = array[j];
                s = j;
            }
            array[s] = temp;
        }

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值