zoukankan      html  css  js  c++  java
  • C# 插入排序 冒泡排序 选择排序 高速排序 堆排序 归并排序 基数排序 希尔排序

    C# 插入排序 冒泡排序 选择排序 高速排序 堆排序 归并排序 基数排序 希尔排序
    以下列出了数据结构与算法的八种基本排序:插入排序 冒泡排序 选择排序 高速排序 堆排序 归并排序 基数排序 希尔排序,然后是測试的样例。代码位置:http://download.csdn.net/detail/luozuolincool/8040027
    排序类:
    public class Sortings
        {
            //插入排序
            public void insertSort(int[] array)
            {
                int temp = 0;
                int index = 0;
                for (int i = 0; i < array.Length; i++)
                {
                    for (int j = 0; j < i; j++)
                    {
                        if (array[i] < array[j])//从j到i之间的数总体右移动。将i数据放到j位置
                        {
                            index = i;
                            temp = array[i];
                            while (index > j)
                            {
                                array[index] = array[index - 1];
                                index--;
                            }
                            array[j] = temp;
                            break;
                        }
                    }
                }
                printArray(array, "插入排序:");
            }
            public void printArray(int[] array, String type)
            {
                Console.WriteLine(type);
                for (int i = 0; i < array.Length; i++)
                {
                    Console.Write(array[i] + ",");
                }
                Console.WriteLine();
            }
            //冒泡排序
            public void bubbleSort(int[] array)
            {
                int temp = 0;
                bool exchanged = true;
                for (int i = 0; i < array.Length; i++)
                {
                    if (!exchanged)
                        break;
                    for (int j = array.Length - 1; j > i; j--)
                    {
                        exchanged = false;
                        if (array[j] < array[j - 1])//后面的数比前面的小就交换
                        {
                            temp = array[j];
                            array[j] = array[j - 1];
                            array[j - 1] = temp;
                            exchanged = true;
                        }
                    }
                }
                printArray(array, "冒泡排序:");
            }
            //选择排序:从前到后依次选择最小的放在最前面,第二小的放在第二个位置
            public void selectionSort(int[] array)
            {
                int minIndex = 0;
                int temp = 0;
                for (int i = 0; i < array.Length; i++)
                {
                    minIndex = i;
                    for (int j = i; j < array.Length; j++)
                    {
                        if (array[j] < array[minIndex])
                            minIndex = j;
                    }
                    //将i到j之间最小的数放到位置i
                    temp = array[minIndex];
                    array[minIndex] = array[i];
                    array[i] = temp;
                }
                printArray(array, "选择排序:");
            }
            //高速排序
            public void quickSort(int[] array)
            {
                quicksort1(array, 0, array.Length - 1);
                printArray(array, "高速排序:");
            }
            public void quicksort1(int[] array, int start, int end)
            {
                if (start >= end)
                    return;
                int i = start, j = end;
                int k = array[i];
                while (i < j)
                {
                    while (array[j] >= k && i < j)
                        j--;
                    array[i] = array[j];
                    while (array[i] < k && i < j)
                        i++;
                    array[j] = array[i];
                }
                array[i] = k;
                quicksort1(array, start, i -1);
                quicksort1(array, i +1, end);
            }
            //堆排序
            public void stackSort(int[] array)
            {
                MyHeap h = new MyHeap();
                h.buildHeap(array);
                h.HeapSort();
                h.printHeap();
            }
            //归并排序
            public void mergeSort(int[] array)
            {
                mergeSort1(array, 0, array.Length - 1);
                printArray(array, "归并:");
            }
            private void mergeSort1(int[] array ,int start,int end)
            {
                if (start >= end)
                    return;
                mergeSort1(array, start, (start+end) / 2);
                mergeSort1(array, (start + end) / 2+1,end);
                merge(array, start, (start + end) / 2, end);
                
            }
            private void merge(int[] array,int start,int mid,int end)
            {
                Queue<int> q = new Queue<int>();
                int i = start, j = mid + 1;
                while (i <=mid && j <=end)
                {
                    if (array[i] < array[j])
                    {
                        q.Enqueue(array[i]);
                        i++;
                    }
                    else
                    {
                        q.Enqueue(array[j]);
                        j++;
                    }
                }
                while (i <= mid)
                    q.Enqueue(array[i++]);
                while (j <= end)
                    q.Enqueue(array[j++]);
               
                for (i = start; i <= end; i++)
                    array[i] = q.Dequeue();
            }
            //基数排序
            public void radixSort(int[] array)
            {
                int maxlength=0;//数据最大位数
                //申请空间用于存放数据
                List<List<int>> lists=new List<List<int>>();
                //申请10个桶,用于存放0-9
                for (int i = 0; i < 10; i++)
                    lists.Add(new List<int>());
                //获取数据的最大位数
                for (int i = 0; i < array.Length; i++)
                    maxlength = maxlength < array[i].ToString().Length ?

    array[i].ToString().Length : maxlength;
                for (int i = 0; i < maxlength; i++)
                {
                    //数据入桶
                    for (int j = 0; j < array.Length; j++)
                    {
                        lists[array[j] / (int)(Math.Pow(10, i)) - array[j] / (int)(Math.Pow(10, i+1))*10].Add(array[j]);
                    }
                    int t = 0;
                    //将桶里面的数据又一次放入数组
                    for (int k = 0; k < 10; k++)
                    {
                        
                        foreach (int item in lists[k])
                            array[t++] = item;
                    }
                    //清空桶里面的数据
                    for (int k = 0; k < 10; k++)
                    {
                        lists[k].Clear();
                    }


                }
                printArray(array, "基数排序");
            }
            //希尔排序
            public void shellSort(int[] array)
            {
                int step = array.Length / 2;
                while (step > 0)
                {
                    shellInsert(array, step);
                    Console.WriteLine();
                    printArray(array, "希尔");
                    step = step / 2;
                }
                printArray(array, "希尔");
            }
            private void shellInsert(int[] array,int step)
            {
                int temp = 0;
                int index = 0;
                for (int i = 0; i < array.Length; i=i+step)
                {
                    for (int j = 0; j < i; j=j+step)
                    {
                        if (array[i] < array[j])//从j到i之间的数总体右移动,将i数据放到j位置
                        {
                            index = i;
                            temp = array[i];
                            while (index > j)
                            {
                                array[index] = array[index - 1];
                                index--;
                            }
                            array[j] = temp;
                            break;
                        }
                    }
                }
            }
        }

    測试代码:
     static void Main(string[] args)
            {
                int[] array = { 12,3,23,4,21,44,2,3,11};
                Console.WriteLine("待排序数组:");
                for (int i = 0; i < array.Length; i++)
                {
                    Console.Write(array[i]+",");
                }
                Console.WriteLine();
               // insertSort(array);
               // bubbleSort(array);
               // selectionSort(array);
              //  (new Sortings()).quickSort(array);
              //  (new Sortings()).stackSort(array);
               // (new Sortings()).mergeSort(array);
                //(new Sortings()).shellSort(array);
                (new Sortings()).radixSort(array);
                Console.Read();
            }
  • 相关阅读:
    面试问题整理Andorid版本 date: 2017-1-12 21:14:36 categories: 技术
    轻量级的C++插件框架
    C++程序在Windows平台上各种定位内存泄漏的方法,并对比了它们的优缺点
    Facebook App 的头文件会有更多的收获
    合并Excel工作薄中成绩表的VBA代码,非常适合教育一线的朋友_python
    使用python在校内发人人网状态(人人网看状态)_python
    使用PYTHON创建XML文档_python
    优秀的缓存请求库,快速请求接口和图片:WTRequestCenter
    让读者快速了解RocketMQ消息中间件需要解决哪些问题
    编绎调试HotSpot JVM及在Eclipse里调试HotSpot一些步骤
  • 原文地址:https://www.cnblogs.com/jhcelue/p/7016756.html
Copyright © 2011-2022 走看看