zoukankan      html  css  js  c++  java
  • 一个时间效率为O(n)的排序算法

    题目:请实现一个排序算法,要求时间效率为O(n).(允许使用常量大小的辅助空间不得超过O(n))

    首先回顾一下自己实现了的几种常用的排序算法

    一、插入排序:

     1 #include<iostream>
     2 using namespace std;
     3 void Insert_sort(int A[], int size)
     4 {
     5     for (int j = 1; j < size; j++)
     6     {
     7         int key = A[j];
     8         int i = j - 1;
     9         while (i >= 0 && A[i] > key)
    10         {
    11             A[i + 1] = A[i];
    12             i--;
    13         }
    14         A[i + 1] = key;
    15     }
    16 }
    17 void print_Insert(int A[],int size)
    18 {
    19     for (int i = 0; i < size; i++)
    20     {
    21         cout << A[i] << "    ";
    22     }
    23     cout << endl;
    24 }
    25 int main2()
    26 {
    27     int A[10] = { 3, 2, 5, 7, 9, 4, 6, 3, 8, 10 };
    28     Insert_sort(A, 10);
    29     print_Insert(A, 10);
    30     system("pause");
    31     return 0;
    32 }

     二、快速排序

     1 #include<iostream>
     2 using namespace std;
     3 void Exchange(int &a, int &b)
     4 {
     5     int temp = a;
     6     a = b;
     7     b = temp;
     8 }
     9 int Partion(int A[], int p, int r)
    10 {
    11     int x = A[r];
    12     int i = p - 1;
    13     for (int j = p; j < r; j++)
    14     {
    15         if (A[j] <= x)
    16         {
    17             i++;
    18             Exchange(A[i], A[j]);
    19         }
    20     }
    21     Exchange(A[i + 1], A[r]);
    22     return i + 1;
    23 }
    24 void Quick_sort(int A[], int p, int r)
    25 {
    26     if (p < r){
    27         int q = Partion(A, p, r);
    28         Quick_sort(A, p, q - 1);
    29         Quick_sort(A, q + 1, r);
    30     }
    31 }
    32 void print_Quick(int A[], int size)
    33 {
    34     for (int i = 0; i < size; i++)
    35     {
    36         cout << A[i] << "    ";
    37     }
    38     cout << endl;
    39 }
    40 int main()
    41 {
    42     int A[10] = { 3, 2, 5, 7, 9, 4, 6, 3, 8, 10 };
    43     Quick_sort(A, 0, 9);
    44     print_Quick(A, 10);
    45     system("pause");
    46     return 0;
    47 }

    三、归并排序

     1 #include<iostream>
     2 using namespace std;
     3 void Merge(int A[], int p, int q, int r)
     4 {
     5     int n1 = q - p + 1;
     6     int n2 = r - q;
     7     int L[100] = { 0 };
     8     int R[100] = { 0 };
     9     for (int i = 0; i < n1; i++)
    10     {
    11         L[i] = A[p + i];
    12     }
    13     for (int i = 0; i < n2; i++)
    14     {
    15         R[i] = A[q + i + 1];
    16     }
    17     L[n1] = INT_MAX;
    18     R[n2] = INT_MAX;
    19     int i = 0;
    20     int j = 0;
    21     for (int k = p; k <=r; k++){
    22         if (L[i] < R[j]){
    23             A[k] = L[i];
    24             ++i;
    25         }
    26         else{
    27             A[k] = R[j];
    28             ++j;
    29         }
    30     }
    31 }
    32 void Merge_sort(int A[], int p, int r)
    33 {
    34     if (p < r)
    35     {
    36         int q = (p+r) / 2;
    37         Merge_sort(A, p, q);
    38         Merge_sort(A, q + 1, r);
    39         Merge(A, p, q, r);
    40     }
    41 }
    42 void print_Merge(int A[], int size)
    43 {
    44     for (int i = 0; i < size; i++)
    45     {
    46         cout << A[i] << "    ";
    47     }
    48     cout << endl;
    49 }
    50 int main()
    51 {
    52     int A[10] = { 3, 2, 5, 7, 9, 4, 6, 3, 8, 10 };
    53     Merge_sort(A, 0,9);
    54     print_Merge(A, 10);
    55     system("pause");
    56     return 0;
    57 }

    四、堆排序

     1 #include<iostream>
     2 #include<vector>
     3 using namespace std;
     4 void Exchange(int &a, int &b)
     5 {
     6     int temp = a;
     7     a = b;
     8     b = temp;
     9 }
    10 void Max_Heapify(vector<int> &A, int i, int size)
    11 {
    12     int  l = 2 * i;
    13     int r = 2 * i + 1;
    14     int largest;
    15     if (l <= size&& A[l]>A[i])
    16         largest = l;
    17     else
    18         largest = i;
    19     if (r <= size && A[r]>A[largest])
    20         largest = r;
    21     if (largest != i){
    22         Exchange(A[i], A[largest]);
    23         Max_Heapify(A, largest, size);
    24     }
    25 }
    26 void Build_MaxHeap(vector<int> &A, int size)
    27 {
    28     for (int i = size / 2; i >= 1; --i)
    29     {
    30         Max_Heapify(A, i, size);
    31     }
    32 }
    33 void Heap_Sort(vector<int> &A, int size)
    34 {
    35     Build_MaxHeap(A, size);
    36     for (int i = size; i >= 1; --i)
    37     {
    38         Exchange(A[1], A[i]);
    39         Max_Heapify(A, 1, i - 1);
    40     }
    41 }
    42 void print(vector<int> A, int size)
    43 {
    44     for (int i = 1; i <= size; i++)
    45     {
    46         cout << A[i] << " ";
    47     }
    48     cout << endl;
    49 }
    50 
    51 int main()
    52 {
    53         vector<int> A(100);
    54         int size;
    55         cout << "please input the size" << endl;
    56         cin >> size;
    57         cout << "please input the number" << endl;
    58         for (int i = 1; i <= size; i++)
    59         {
    60                 cin >> A[i];
    61         }
    62         print(A,size);
    63         Build_MaxHeap(A,size);
    64         print(A,size);
    65         Heap_Sort(A, size);
    66         print(A, size);
    67         system("pause");
    68         return 0;
    69 }                                                

    五、计数排序

     1 #include<iostream>
     2 using namespace std;
     3 void Counting_sort(int A[], int last, int B[], int k)
     4 {
     5     int C[100] = { 0 };
     6     for (int j = 1; j <= last; j++)
     7     {
     8         C[A[j]] = C[A[j]] + 1;
     9     }
    10     for (int i = 1; i <= k; i++)
    11     {
    12         C[i] = C[i] + C[i - 1];
    13     }
    14     for (int j = last; j >= 1; j--)
    15     {
    16         B[C[A[j]]] = A[j];
    17         C[A[j]] = C[A[j]] - 1;
    18     }
    19 }
    20 void print_count(int A[], int last)
    21 {
    22     for (int i = 1; i <= last; i++)
    23     {
    24         cout << A[i] << "    ";
    25     }
    26     cout << endl;
    27 }
    28 int main()
    29 {
    30     int A[9] = { 0, 2, 5, 3, 0, 2, 3, 0, 3 };
    31     int B[9] = { 0 };
    32     Counting_sort(A, 8, B, 5);
    33     print_count(B, 8);
    34     system("pause");
    35     return 0;
    36 }

    对于上述问题的理解:就是要对员工按照年龄来进行排序,显然年龄是一个较小的范围可以让我们用来做为常数级别的空间来存放,每个年龄段的人,然后根据年龄排序即可,故可以写出如下的代码:

     1 #include<iostream>
     2 using namespace std;
     3 void ages_sort(int age[], int length)
     4 {
     5     if (age == nullptr || length <= 0)
     6         return;
     7     const int largest_ages = 100;
     8     int TimeOfAge[largest_ages+1] = { 0 };
     9     for (int i = 1; i <= length; ++i)
    10     {
    11         if (age[i] < 0 || age[i]>100)
    12             throw exception("out of age");
    13         ++TimeOfAge[age[i]];
    14     }
    15     int index=0;
    16     for (int i = 0; i <= largest_ages; ++i)
    17     {
    18         for (int j = 0; j < TimeOfAge[i]; ++j)
    19         {
    20             age[index] = i;
    21             ++index;
    22         }
    23     }
    24 }
  • 相关阅读:
    struts2 标签的使用之二 s:iterator
    struts2 标签的使用之一 s:if
    java Tomcat数据库连接池
    JNDI:对java:comp/env的研究
    ***Tomcat7.0安装配置
    ***tomcat配置文件server.xml详解
    宜昌IT软件资源汇总
    tomcat安全设置
    tomcat部署javaweb项目的三种方式
    Tomcat在Linux上的安装与配置
  • 原文地址:https://www.cnblogs.com/General-up/p/5409547.html
Copyright © 2011-2022 走看看