个人专题

常见排序算法的C++实现

2018-03-31  本文已影响3人  dalalaa

选择排序

#include <iostream>
using namespace std;

template <typename T>
void select_sort(T lst[],int len)
{
    int i,j;
    for(i=0;i<=len-1;i++)
    {
        int min_index = i;
        for(j=i;j<=len-1;j++)
        {
            if(lst[j]<lst[min_index])
            {
                min_index = j;
            }
        }
        swap(lst[i],lst[min_index]);
    }
}

template <typename T>
void swap(T x, T y)
{
    T temp;
    temp = x;
    x = y;
    y = temp;
}


int main()
{
    float lst[] = {11,7,6,54,4,3,32,22,1,2,3,4,5,6,7};
    int len = (int) sizeof(lst)/sizeof(*lst);
    select_sort(lst,len);
    for (int i=0;i<len;i++)
    {
        cout << lst[i] << " ";
    }
    cout << endl;
    return 0;
}

冒泡排序

#include <iostream>
using namespace std;
template <typename T>

void bubble_sort(T lst[],int len)
{
    int i,j;
    T temp;
    for (i=0;i<len-1;i++)
    {
        for (j=0;j<len-1;j++)
        {
            if (lst[j]>lst[j+1])
            {
                swap(lst[j],lst[j+1]);
            }
        }
    }
}

template <typename T>//在每个使用到T类型的函数前都需要加template 
void swap(T x,T y)
{
    T temp;
    temp = x;
    x = y;
    y = temp;
}

int main()
{
    float lst[] = {11,7,6,54,4,3,32,22,1,2,3,4,5,6,7};
    int len = (int) sizeof(lst)/sizeof(*lst);
    bubble_sort(lst,len);
    for (int i=0;i<len;i++)
    {
        cout << lst[i] << " ";
    }
    cout << endl;
    return 0;
}

插入排序

#include <iostream>
using namespace std;

template <typename T>
void insert_sort(T lst[],int len)
{
    int i,j;
    T temp;
    for(i=0;i<=len-1;i++)
    {
        temp = lst[i];
        j = i - 1;
        while(j>=0&&lst[j]>temp)
        {
            lst[j+1] = lst[j];
            j--;
        }
        lst[j+1] = temp;
    }
} 

int main()
{
    float lst[] = {11,7,6,54,4,3,32,22,1,2,3,4,5,6,7};
    int len = (int) sizeof(lst)/sizeof(*lst);
    insert_sort(lst,len);
    for (int i=0;i<len;i++)
    {
        cout << lst[i] << " ";
    }
    cout << endl;
    return 0;
}

快速排序

#include <iostream>
using namespace std;

template <typename T>
int partition(T lst[],int low,int high)
{
    T pivot = lst[high-1];
    int small = low - 1;
    int i;
    for(i=low;i<high;i++)
    {
        if(lst[i]<pivot)
        {
            small ++;//记录最后一个小于pivot的元素的index 
            swap(lst[i],lst[small]);
        }
    }
    if(lst[small+1] > pivot)
    {
        swap(lst[small+1],lst[high-1]);
    }
    return small + 1;
}

template <typename T>
void quick_sort(T lst[],int low,int high)
{
    if(low < high)
    {
        int p = partition(lst,low,high);
        quick_sort(lst,low,p);
        quick_sort(lst,p+1,high);
    }
}

template <typename T>
void swap(T x,T y)
{
    T temp;
    temp = x;
    x = y;
    y = temp;
}

int main()
{
    float lst[] = {11,7,6,54,4,3,32,22,1,2,3,4,5,6,7};
    int len = (int) sizeof(lst)/sizeof(*lst);
    quick_sort(lst,0,len);
    for (int i=0;i<len;i++)
    {
        cout << lst[i] << " ";
    }
    cout << endl;
    return 0;
}

有兴趣转行机器学习的朋友可以加群:


机器学习-菜鸡互啄群
上一篇 下一篇

猜你喜欢

热点阅读