排序算法

来源:互联网 发布:贸易保护主义 知乎 编辑:程序博客网 时间:2024/05/16 19:26

一、插入排序—直接插入排序(Straight Insertion Sort)
时间复杂度:O(n^2).

#include <iostream>using namespace std;const int MAX_SIZE = 10;template<class T>void insert(T a[], int n);int main(int argc, char** argv) {    int s[10] = {9,6,4,8,5,3,1,0,3,2};    insert(s, MAX_SIZE);    for(int i = 0; i < MAX_SIZE; i++)        cout<< s[i] << " ";    cout<<endl;    return 0;}template<class T>void insert(T a[], int n){    //对数组a[0:n-1]进行插入排序     for(int i = 1; i < n; i++)    {        //把a[i]插入到a[0:i-1]         T t = a[i];        int j;        for(j = i-1; j >= 0 && t < a[j]; j--)            a[j+1] = a[j];        a[j+1] = t;    }}

选择排序
首先找出最大元素将其放入a[n-1],然后在剩下的元素中找出最大的元素放入a[n-2].
如此进行下去。直到剩下下一个元素。

及时终止:
在程序中加入bool值state可判断元素序列是否
已经有序。如果有序则停止,节约时间。

#include<iostream>using namespace std;const int MAX_SIZE = 10;template<class T>void selectionSort(T a[], int n);int main(){    int s[10] = {9,6,4,8,5,3,1,0,3,2};    selectionSort(s, MAX_SIZE);    for(int i = 0; i < MAX_SIZE; i++)        cout<< s[i] << " ";    cout<<endl;    return 0;}template<class T>void selectionSort(T a[], int n){    //及时终止的选择排序    bool state = false;    for(int i = n; !state && (i > 1); i--)    {        int MaxValue = 0;        state = true;        //查找最大元素        for(int j = 1; j < i; j++)            if(a[MaxValue] <= a[j])                MaxValue = j;            else                state = false;  //表明无序            swap(a[MaxValue], a[i-1]);    }}

冒泡排序:
每当两相邻的数比较后发现它们的排序与排序要求相反时,就将它们互换。

及时终止:
在程序中加入bool值state可判断元素序列是否
已经有序。如果有序则停止,节约时间。

#include<iostream>using namespace std;const int MAX_SIZE = 10;template<class T>void bubbleSort(T a[], int n);int main(){    int s[10] = {9,6,4,8,5,3,1,0,3,2};    bubbleSort(s, MAX_SIZE);    for(int i = 0; i < MAX_SIZE; i++)        cout<< s[i] << " ";    cout<<endl;    return 0;}template<class T>void bubbleSort(T a[], int n){    bool state = false;    for(int i = n; !state && (i > 0); i--)    {        state = true;        //把a[0:i]中最大元素移动到最右端         for(int j = 1; j < i; j++)            if(a[j] <= a[j-1])            {                swap(a[j], a[j-1]);                state = false;            }    }}

名次排序
名次计算(ranking):
一个元素在序列中的名词(rank)是所有比他小的元素个数加上
在他左边出现相同元素的个数之和,a[5]={4,3,9,3,7},个元素的
次词为r[5]={2,0,4,1,3};

#include<iostream>using namespace std;const int MAX_SIZE = 20;template<class T>void rank(T a[], int n, int r[]);template<class T>void ranksort(T a[], int n, int r[]);int main(){    int a[MAX_SIZE] = {5,9,3,7,4,3,7,9,1,6,8,1,3,4,8,5,2,1,4,5};    int r[MAX_SIZE];    rank(a, MAX_SIZE, r);    ranksort(a, MAX_SIZE, r);    for(int i = 0; i < MAX_SIZE; i++)        cout<< a[i] << " ";    cout<<endl;    return 0;}template<class T>void rank(T a[], int n, int r[]){    for(int i = 0; i < n; i++)        r[i] = 0;    //比较所有元素对    for(i =1; i < n; i++)        for(int j = 0; j < i; j++)        {            if(a[j] <= a[i])                r[i]++;            else                r[j]++;        }}template<class T>void ranksort(T a[], int n, int r[]){    T *u = new T [n];   //创建附加数组    //把a中元素移到u中的正确位置    for(int i = 0; i < n; i++)        u[r[i]] = a[i];    //u中元素移回a    for(i = 0; i < n; i++)        a[i] = u[i];    delete u;}
0 0
原创粉丝点击