選擇排序法&快速排序法


選擇排序法:每次遍歷整個數組,選出其中最小值。如果數組長度為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方法,就可以對一個數組進行從小到大的排序了。不過了解算法的本質應該也不是什么壞事吧。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM