• 选择排序法&快速排序法


    选择排序法:每次遍历整个数组,选出其中最小值。如果数组长度为n,则需要(n-1)+(n-2)+...+2+1次操作,则用大O表示法表示应该为O(n*n/2),但是大O表示法省略诸如1/2这样的常数,因此该方法的大O表示为O(n^2)。

    Python代码:

    >>> def findSmallest(arr):
        smallest = arr[0]
        smallest_index = 0
        for i in range(1, len(arr)):
            if arr[i] < smallest:
                smallest = arr[i]
                smallest_index = i
        return smallest_index
    
    >>> def selectionSort(arr):
        newArr = []
        for i in range(len(arr)):
            smallest = findSmallest(arr)
            newArr.append(arr.pop(smallest))
        return newArr

    测试:

    >>> selectionSort([5,3,6,2,10])
    [2, 3, 5, 6, 10]
    >>> 

    C#代码:

    namespace Algorithms
    {
        public static class SelectionSort
        {
            public static List<double> Selection_Sort(List<double> list)
            {
                List<double> newList = new List<double>();
                double smallest;
                int count = list.Count;
                for (int i = 0; i < count; i++)
                {
                    smallest = list[FindSmallest(list)];
                    list.Remove(smallest);
                    newList.Add(smallest);
                }
                return newList;
            }
    
            private static int FindSmallest(List<double> list)
            {
                double smallest = list[0];
                int smallest_index = 0;
                for (int i = 1; i < list.Count; i++)
                {
                    if(list[i] < smallest)
                    {
                        smallest = list[i];
                        smallest_index = i;
                    }
                }
                return smallest_index;
            }
        }
    }

    快速排序法:请先熟悉“递归”的相关知识。https://www.cnblogs.com/larissa-0464/p/10630300.html

    分而治之(divide and conquer, D&C)的思想:1.找出简单的基线条件;2.确定如何缩小问题的规模,使其符合基线条件。

    那么将D&C思想应用于排序任务中,其思路应如下:

    基线条件就是只有一个元素的数组,这样的数组顺序就是自己。在数组中任取一个元素作为基准值,那么该数组将会被划分为三部分

      小于基准值的子数组 + 基准值 + 大于基准值的子数组

    这样就会不断地缩小数组的规模,直到只剩一个元素为止。

    Python代码:

    >>> def quicksort(arr):
        if len(arr) < 2:
            return arr
        else:
            pivot = arr[0]
            less = [i for i in arr[1:] if i <= pivot]
            greater = [i for i in arr[1:] if i > pivot]
            return quicksort(less) + [pivot] + quicksort(greater)
    
        
    >>> arr = [3,5,1,9,7]
    >>> quicksort(arr)
    [1, 3, 5, 7, 9]
    >>> 

    C#代码:

    namespace Algorithms
    {
        public static class QuickSort
        {
            public static List<double> Quick_Sort(List<double> array)
            {
                if (array.Count < 2)
                    return array;
                else
                {
                    double pivot = array[0];
                    List<double> less = new List<double>();
                    List<double> greater = new List<double>();
                    for (int i = 1; i < array.Count; i++)
                    {
                        if (array[i] <= pivot)
                            less.Add(array[i]);
                        else
                            greater.Add(array[i]);
                    }
                    return Quick_Sort(less).Union(new List<double>() { pivot }).Union(Quick_Sort(greater)).ToList();
                }
            }
        }
    }

     其实,排序的方法已经包含在各种语言中了,比如Python和C#都是使用Sort方法,就可以对一个数组进行从小到大的排序了。不过了解算法的本质应该也不是什么坏事吧。

  • 相关阅读:
    [读书笔记]SQLSERVER企业级平台管理实践读书笔记02
    [工作相关] 一个虚拟机上面的SAP4HANA的简单使用维护
    vCenter机器查找功能不可用的解决
    [日常工作]GS使用安装盘修改密码后的处理
    [读书笔记]SQLSERVER企业级平台管理实践读书笔记01
    [书摘]架构真经--可扩展性规则的利益与优先级排行榜
    2017年总结2018年规划
    Windows 通过命令行设置固定ip地址
    ACDsee的安装过程
    [工作相关] GS产品使用LInux下Oracle数据库以及ASM存储时的数据文件路径写法.
  • 原文地址:https://www.cnblogs.com/larissa-0464/p/10624203.html
Copyright © 2020-2023  润新知