C/C++ 知识点---排序实现

1.冒泡排序
冒泡排序是O(N^2)复杂度的排序算法,效率较低,需要N趟遍历,每次将候选集中最小的数通过交换浮到最上面;

template <typename Type>
void BubbleSort(vector<Type> &arraySort, int lowIndex, int hightIndex)
{
    bool bChange;
    for (int i=lowIndex; i<hightIndex; ++i)
    {
        bChange = false;
        for (int j=hightIndex; j>i; --j)
        {
            if (arraySort[j-1] > arraySort[j])
            {
                swap(arraySort[j-1], arraySort[j]);
                bChange = true;
            }
        }

        if (!bChange)
        {
            return;
        }
    }
}

2.选择排序
选择排序就是每次在候选集合中选择最小的数插入到候选结合的开头,并且不断的缩小候选集合的过程,从算法实现的角度讲,就是要进行N词遍历,每次在候选集合中选择最小的数放在候选集合前部的过程,并且不断的缩小候选集。

template <typename Type>
void SelectSort(vector<Type> &arraySort, int lowIndex, int hightIndex)
{
    Type tempMinIndex;
    for (int i=lowIndex; i<hightIndex; ++i)
    {
        tempMinIndex = i;
        for (int j=i+1; j<hightIndex; ++j)
        {
            if (arraySort[j] < arraySort[tempMinIndex])
            {
                tempMinIndex = j;
            }
        }

        if (tempMinIndex != i)
        {
            swap(arraySort[tempMinIndex], arraySort[i]);
        }
    }
}

3.插入排序
每次将一个数插入到有序的集合中,从算法实现的角度讲,就是进行N趟遍历,每次将第i个数插入到前面有序的集合中,最后达到有序。算法的复杂度为O(N^2),在实现时将第i个数与前面的i-1个数进行比较,如果小于就交换,最后插入到合适的位置。

template <tepename Type>
void InsertSort(vector<Type> &arraySort, int lowIndex, int hightIndex)
{
    for (int i=lowIndex+1; i<hightIndex; ++i)
    {
        Type tempValue = arraySort[i];
        j = i-1;
        while (j>=0 && temValue<arraySort[j])
        {
            arraySort[j+1] = arraySort[j];
            --j;
        }
        arraySort[j+1] = tempValue;
    }
}

4.快速排序
快速排序是最常用的也算是经典的排序算法,它是通过分治递归的方式实现,通过选取哨兵,并将元素与哨兵比较,按照大小将数组切分成两部分,并对这两部分按照同样的方式进行递归计算,最后达到有序。

template <typename Type>
void QuickSort(vector<Type> &arrarSort, int lowIndex, int hightIndex)
{
    int i = lowIndex;
    int j = hightIndex;
    Type tempValue = arraySort[lowIndex];

    while (i < j)
    {
        while (i<j && arraySort[j]>=tempValue)
        {
            j--;
        }    
        if (i < j)
        {
            arraySort[i++] = arraySort[j];
        }

        while (i<j && arraySort[i]<tempValue)
        {
            i++;
        }
        if (i < j)
        {
            arraySort[j--] = arraySort[i];
        }

        QuickSort(arraySort, i+1, hightIndex);
        QuickSort(arraySort, lowIndex; i-1);
    }

    arraySort[i] = tempValue;
}

5.归并排序
归并排序也是用分治递归的思想进行求解,想将小块进行排序,然后合并来实现,归并排序的算法复杂度是O(N*logN),空间复杂度是O(N)

template <typename Type>
void Merge(vector<Type> &arraySort, int leftIndex, int midIndex, int rightIndex)
{
    int i = leftIndex;
    int j = midIndex+1;
    int n1 = midIndex-leftIndex+1;
    int n2 = rightIndex-midIndex;
    int k = 0;

    Type *tempArray = new Type[n1+n2];
    while (i<=midIndex && j<=rightIndex)
    {
        if (arraySort[i] < arraySort[j])
        {
            tempArray[k++] = arraySort[i++];
        }
        else
        {
            tempArray[k++] = arraySort[j++];
        }
    }

    while (i <= midIndex)
    {
        tempArray[k++] = arraySort[i++];
    }
    while (j <= rightIndex)
    {
        tempArray[k++] = arraySort[j++];
    }

    for (int i=0; i<n1; ++i)
    {
        arraySort[leftIndex++] = tempArray[i];
    }
    tempIndex = midIndex+1;
    for (int i=0; i<n2; ++i)
    {
        arraySort[tempIndex++] = tempArray[n1+i];
    }

    delete []tempArray;
}

template <typename Type>
void MergeSort(vector<Type> &arraySort, int leftIndex, int rightIndex)
{
    if (leftIndex < rightIndex)
    {
        int midIndex = (leftIndex+rightIndex)/2;
        MergeSort(arraySort, leftIndex, midIndex);
        MergeSort(arraySort, midIndex+1, rightIndex);
        Merge(arraySort, leftIndex, midIndex, rightIndex);    
    }
}
原文地址:https://www.cnblogs.com/sz-leez/p/4482941.html