关于排序算法的折腾

Posted interim

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于排序算法的折腾相关的知识,希望对你有一定的参考价值。

 

折腾了许久了

主要是折腾 冒泡排序

 

抓个图说话吧

 

技术分享图片

 

数据重复多,

组团冒泡比冒泡好些,

但是用完全的版本,用链表List,不行,需要自己做个链表试试,先这样吧

 

2018年11月8日

 

 

 

 

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

public class Example
{

    public class 链
    {
        public 链()
        {

        }
        public 链(int a)
        {
            值 = a;
        }
        public int 值;
        public 链 下一元素 = null;
    }

    public static void Main()
    {


        var len = Convert.ToInt32(Console.ReadLine());
        var list = Console.ReadLine().Split( );


        list = System.IO.File.ReadAllText("h:\\a.txt").Split( );


        int K = 1;
        int[] data = new int[len * K];

        int i = 0;
        for (int k = 0; k < K; k++)
        {
            foreach (var item in list)
            {
                data[i++] = Convert.ToInt32(item);
            }
        }





        System.Diagnostics.Stopwatch s = new System.Diagnostics.Stopwatch();
        s.Start();
        Console.WriteLine(组团冒泡排序不完全(deapCopy(data)));
        Console.WriteLine(组团冒泡排序不完全List版(deapCopy(data)));
        Console.WriteLine(组团冒泡排序List(deapCopy(data)));
        Console.WriteLine(经典冒泡排序(deapCopy(data)));
        Console.WriteLine(经典冒泡List(deapCopy(data)));

        s.Stop();
        Console.WriteLine(s.ElapsedMilliseconds);
    }

    static int[] deapCopy(int[] data)
    {
        int[] mydata = new int[data.Length];
        int i = 0;
        foreach (var item in data)
        {
            mydata[i] = data[i];
            i++;
        }
        return mydata;
    }
    static string 链插入排序(int[] data)
    {

        StringBuilder sb = new StringBuilder();
        链 sll = new 链();
        for (int j = 0; j < data.Length; j++)
        {
            var item = data[j];

            var tempSll = sll;
            AAA:
            {
                if (tempSll.下一元素?.值 > item)
                {
                    var temp = tempSll.下一元素;

                    tempSll.下一元素 = new 链(item);

                    tempSll.下一元素.下一元素 = temp;
                }
                else
                {
                    if (tempSll.下一元素 == null)
                    {
                        tempSll.下一元素 = new 链(item);

                    }
                    else
                    {
                        tempSll = tempSll.下一元素;
                        goto AAA;
                    }
                }

            }


        }
        var temp2 = sll.下一元素;
        while (temp2 != null)
        {
            sb.Append(temp2.值 + " ");
            temp2 = temp2.下一元素;
        }
        return sb.ToString().Trim();
    }


    static string 数组插入排序(int[] data)
    {

        var len = data.Length;

        int[] myData = new int[len];
        myData[0] = data[0];

        int count = 1;
        for (int i = 1; i < len; i++)
        {
            for (int j = 0; j <= count && count < len; j++)
            {
                if (data[i] < myData[j])
                {
                    var tempC = count;
                    while (tempC > j)
                    {
                        myData[tempC] = myData[tempC - 1];
                        tempC--;
                    }

                    myData[j] = data[i];
                    count++;
                    break;
                }
                else
                {

                    if (j == count - 1)
                    {
                        j++;
                        myData[j] = data[i];
                        count++;
                        break;
                    }
                }

            }

        }
        StringBuilder sb = new StringBuilder();
        foreach (var item in myData)
        {
            sb.Append(item + " ");

        }
        return sb.ToString().Trim();

    }

    static string 组团冒泡排序List(int[] data)
    {
        //数据准备阶段
        List<List<int>> myData = new List<List<int>>();

        List<List<int>> myData2 = new List<List<int>>();

        foreach (var item in data)
        {
            myData2.Add(new List<int> { item });
            myData.Add(new List<int> { item });


        }
        //数据准备完成,计划用 List.Sort的 速度,与我的这个算法相比.

        System.Diagnostics.Stopwatch a = new System.Diagnostics.Stopwatch();
        a.Start();

        myData2.Sort((c, b) => { if (c[0] < b[0]) return 1; return 0; });

        a.Stop();
        Console.WriteLine(a.ElapsedMilliseconds);

        a.Restart();
        StringBuilder sb = new StringBuilder();
        var len = myData.Count;

        for (int i = 0; i < len - 1; i++)
        {

            bool haveSW = false;
            for (int j = len - 1; j > i; j--)
            {
                if (myData[j][0] < myData[j - 1][0])
                {
                    var temp = myData[j - 1];
                    myData[j - 1] = myData[j];
                    myData[j] = temp;
                    haveSW = true;

                }
                else if (myData[j] == myData[j - 1])
                {
                    myData[j - 1].AddRange(myData[j]);
                    myData.RemoveAt(j);
                }
            }
            if (haveSW == false)
            {
                break;
            }
        }
        a.Stop();

        Console.WriteLine(a.ElapsedMilliseconds);



        foreach (var d in myData)
        {
            foreach (var item in d)
            {
                sb.Append(item + " ");
            }



        }
        return "";// sb.ToString().Trim();
    }
    static string 组团冒泡排序不完全List版(int[] data)
    {  //数据准备阶段
        List<List<int>> myData = new List<List<int>>();


        foreach (var item in data)
        {
            myData.Add(new List<int> { item });


        }
        //数据准备完成
        System.Diagnostics.Stopwatch a = new System.Diagnostics.Stopwatch();
        a.Start();


        StringBuilder sb = new StringBuilder();
        var len = data.Length;



        for (int i = 0; i < len - 1; i++)
        {
            var X = 0;
            bool haveSW = false;

            for (int j = len - 1; j > i; j--)
            {
                if (myData[j][0] < myData[j - 1][0])
                {
                    var temp = myData[j - 1];
                    myData[j - 1] = myData[j + X];
                    myData[j + X] = temp;
                    haveSW = true;
                    X = 0;
                }
                else if (myData[j][0] == myData[j - 1][0])
                {
                    X++;
                }
                else
                {
                    X = 0;
                }
            }
            if (haveSW == false)
            {
                break;
            }
        }


        a.Stop();
        Console.WriteLine(a.ElapsedMilliseconds);

        foreach (var item in myData)
        {
            sb.Append(item + " ");

        }
        return "";// sb.ToString().Trim();

    }
    static string 经典冒泡List(int[] data)
    { //数据准备阶段
        List<List<int>> myData = new List<List<int>>();


        foreach (var item in data)
        {
            myData.Add(new List<int> { item });


        }
        //数据准备完成
        System.Diagnostics.Stopwatch a = new System.Diagnostics.Stopwatch();
        a.Start();


        StringBuilder sb = new StringBuilder();
        var len = data.Length;



        for (int i = 0; i < len - 1; i++)
        {

            bool haveSW = false;

            for (int j = len - 1; j > i; j--)
            {
                if (myData[j][0] < myData[j - 1][0])
                {
                    var temp = myData[j - 1];
                    myData[j - 1] = myData[j];
                    myData[j] = temp;
                    haveSW = true;

                }
            }
            if (haveSW == false)
            {
                break;
            }
        }


        a.Stop();
        Console.WriteLine(a.ElapsedMilliseconds);

        foreach (var item in myData)
        {
            sb.Append(item + " ");

        }
        return "";// sb.ToString().Trim();

    }

    static string 组团冒泡排序不完全(int[] data)
    {
        System.Diagnostics.Stopwatch a = new System.Diagnostics.Stopwatch();
        a.Start();


        StringBuilder sb = new StringBuilder();
        var len = data.Length;
        var myData = data;


        for (int i = 0; i < len - 1; i++)
        {
            var X = 0;
            bool haveSW = false;

            for (int j = len - 1; j > i; j--)
            {
                if (myData[j] < myData[j - 1])
                {
                    var temp = myData[j - 1];
                    myData[j - 1] = myData[j + X];
                    myData[j + X] = temp;
                    haveSW = true;
                    X = 0;
                }
                else if (myData[j] == myData[j - 1])
                {
                    X++;
                }
                else
                {
                    X = 0;
                }
            }
            if (haveSW == false)
            {
                break;
            }
        }


        a.Stop();
        Console.WriteLine(a.ElapsedMilliseconds);

        foreach (var item in myData)
        {
            sb.Append(item + " ");

        }
        return "";// sb.ToString().Trim();

    }
    static string 经典冒泡排序(int[] data)
    {
        System.Diagnostics.Stopwatch a = new System.Diagnostics.Stopwatch();
        a.Start();

        StringBuilder sb = new StringBuilder();
        var len = data.Length;
        var myData = data;
        for (int i = 0; i < len - 1; i++)
        {

            bool haveSW = false;
            for (int j = len - 1; j > i; j--)
            {
                if (myData[j] < myData[j - 1])
                {
                    var temp = myData[j - 1];
                    myData[j - 1] = myData[j];
                    myData[j] = temp;
                    haveSW = true;
                }
            }
            if (haveSW == false)
            {
                break;
            }
        }


        a.Stop();
        Console.WriteLine(a.ElapsedMilliseconds);
        foreach (var item in myData)
        {
            sb.Append(item + " ");

        }


        return "";//  sb.ToString().Trim();

    }

}

 

以上是关于关于排序算法的折腾的主要内容,如果未能解决你的问题,请参考以下文章

如何正确地将多个片段添加到片段过渡?

Atom编辑器折腾记_(15)JS代码片段补全(插件:javascript-snippets)

关于常用排序算法的一个总结

7种基本排序算法的Java实现

算法排序之堆排序

关于选择排序并去重的几种算法分析