【1】插入排序:
是一个对少量元素进行排序的有效算法。实现比较简单。时间复杂度:O(n^2),空间复杂度:O(1)。是稳定的排序方法。
代码:
- //insertion sort
- #include <iostream>
- using namespace std;
- //insertion sort
- void InsertionSort(int *a,int n)
- {
- int temp;
- for(int i = 1;i < n;++i)
- {
- temp = *(a + i);
- int j = i - 1;
- while(j >= 0 && *(a + j) > temp)
- {
- *(a + j + 1) = *(a + j);
- --j;
- }
- *(a + j + 1) = temp;
- }
- }
- int main()
- {
- int n,temp;
- cout<<"please input the number of the values that need to sort:"<<endl;
- cin>>n;
- int *a = (int*)malloc(n * sizeof(int));
- cout<<"please input each value:"<<endl;
- for(int i = 0;i < n;++i)
- {
- cin>>temp;
- *(a + i) = temp;
- }
- /*
- //insertion sort
- for(int i = 1;i < n;++i)
- {
- temp = *(a + i);
- int j = i - 1;
- while(j >= 0 && *(a + j) > temp)
- {
- *(a + j + 1) = *(a + j);
- --j;
- }
- *(a + j + 1) = temp;
- }*/
- InsertionSort(a,n);
- cout<<"the values after sort:"<<endl;
- for(int i = 0;i < n;++i)
- cout<<*(a + i)<<" ";
- free(a);
- }
数据测试:
上述代码可以改进的一个地方是:在查找插入位置的时候可以采用二分查找,但是这样依然不可以把时间复杂度降低为O(nlogn),因为移动元素的复杂度没有降低。所以时间复杂度仍然是O(n^2)。
做此改进需要添加函数InsertLoc用于二分查找需要插入的位置,以及修改函数InsertionSort的实现。具体如下:
- //改进:用二分查找来找到插入的位置
- //在数组a[low]---a[high]查找val插入的位置
- int InsertLoc(int *a,int low,int high,int val)
- {
- if(low == high)
- {
- if(val > *(a + low))return (low + 1);
- else
- return low;
- }
- int mid = (low + high) / 2;
- if(val > *(a + mid) && val > *(a + mid + 1))
- return InsertLoc(a,mid + 1,high,val);
- else if(val < *(a + mid) && val < *(a + mid + 1))
- return InsertLoc(a,low,mid,val);
- else
- return mid;
- }
- void InsertionSort(int *a,int n)
- {
- int temp,insert_location;
- for(int i = 1;i < n;++i)
- {
- temp = *(a + i);
- int j = i - 1;
- insert_location = InsertLoc(a,0,j,temp);
- cout<<"insert_location:"<<insert_location<<endl;
- while(j >= insert_location)
- {
- *(a + j + 1) = *(a + j);
- --j;
- }
- *(a + insert_location) = temp;
- for(int m = 0;m <= i;++m)
- cout<<*(a + m)<<" ";
- cout<<endl;
- }
- }
【2】选择排序
第一次找出A中最小的元素,与A[0]交换,接着,找出A中次小得元素,与A[1]互换。对A中头n-1个元素执行这一过程。时间复杂度:O(n^2),不需要辅助空间。是稳定的排序方法。
代码:
- //选择排序
- #include <iostream>
- using namespace std;
- void ChoseSort(int* a,int n)
- {
- int temp;
- for(int i = 0;i < n - 1;++i)
- {
- for(int j = i + 1;j < n;++j)
- {
- if(*(a + i) > *(a + j))
- {
- temp = *(a + i);
- *(a + i) = *(a + j);
- *(a + j) = temp;
- }
- }
- }
- }
- int main()
- {
- int n,temp;
- cout<<"please input the number of the values that need to sort:"<<endl;
- cin>>n;
- int *a = (int*)malloc(n * sizeof(int));
- cout<<"please input each value:"<<endl;
- for(int i = 0;i < n;++i)
- {
- cin>>temp;
- *(a + i) = temp;
- }
- ChoseSort(a,n);
- cout<<"the values after sort:"<<endl;
- for(int i = 0;i < n;++i)
- cout<<*(a + i)<<" ";
- free(a);
- }
【3】合并排序
采用分治法。将n个元素分成各含n/2个元素的子序列,用合并排序法对两个子序列递归的排序(子序列长度为1时递归结束),最后合并两个已排序的子序列得到结果。时间复杂度:O(nlogn),空间复杂度:O(n)。不是稳定的排序
代码:
- //合并排序
- #include <iostream>
- using namespace std;
- #define MAX_VALUE 100000//用于设置哨兵,避免检查是否每一个堆都是空的
- //合并两个子数组的函数
- void Merge(int *a,int p,int q,int r)
- {
- int num1,num2;
- num1 = q - p + 1;
- num2 = r - q;
- int *a1 = (int*)malloc((num1 + 1) * sizeof(int));
- int *a2 = (int*)malloc((num2 + 1) * sizeof(int));
- for(int i = 0;i < num1;++i)
- *(a1 + i) = *(a + p + i);
- *(a1 + num1) = MAX_VALUE;//设置哨兵元素
- for(int i = 0;i < num2;++i)
- *(a2 + i) = *(a + q + 1 + i);
- *(a2 + num2) = MAX_VALUE;//设置哨兵元素
- //进行排序
- int index1 = 0;
- int index2 = 0;
- for(int i = p;i <= r;++i)
- {
- if(*(a1 + index1) < *(a2 + index2))
- {
- *(a + i) = *(a1 + index1);
- ++index1;
- }
- else
- {
- *(a + i) = *(a2 + index2);
- ++index2;
- }
- }
- free(a1);
- free(a2);
- }
- //递归合并排序算法
- void MergeSort(int *a,int p,int r)
- {
- if(p < r)
- {
- int q = (p + r) / 2;
- MergeSort(a,p,q);
- MergeSort(a,q + 1,r);
- Merge(a,p,q,r);
- }
- }
- int main()
- {
- int n,temp;
- cout<<"please input the number of the values that need to sort:"<<endl;
- cin>>n;
- int *a = (int*)malloc(n * sizeof(int));
- cout<<"please input each value:"<<endl;
- for(int i = 0;i < n;++i)
- {
- cin>>temp;
- *(a + i) = temp;
- }
- MergeSort(a,0,n - 1);
- cout<<"the values after sort:"<<endl;
- for(int i = 0;i < n;++i)
- cout<<*(a + i)<<" ";
- free(a);
- }
如果不使用哨兵元素,需要修改Merge函数,如下:
- //合并两个子数组的函数(不使用哨兵元素)
- void Merge(int *a,int p,int q,int r)
- {
- int num1,num2;
- num1 = q - p + 1;
- num2 = r - q;
- int *a1 = (int*)malloc(num1 * sizeof(int));
- int *a2 = (int*)malloc(num2 * sizeof(int));
- for(int i = 0;i < num1;++i)
- *(a1 + i) = *(a + p + i);
- for(int i = 0;i < num2;++i)
- *(a2 + i) = *(a + q + 1 + i);
- //进行排序
- int index1 = 0;
- int index2 = 0;
- int index = p;
- while(index1 < num1 && index2 <num2)
- {
- if(*(a1 + index1) < *(a2 + index2))
- {
- *(a + index) = *(a1 + index1);
- ++index;
- ++index1;
- }
- else{
- *(a + index) = *(a2 + index2);
- ++index;
- ++index2;
- }
- }
- while(index1 < num1)
- {
- *(a + index) = *(a1 + index1);
- ++index;
- ++index1;
- }
- while(index2 < num2)
- {
- *(a + index) = *(a2 + index2);
- ++index;
- ++index2;
- }
- free(a1);<pre class="cpp" name="code"> free(a2);
- }
【4】冒泡排序
每一趟都比较相邻两个元素,若是逆序的,则交换。结束的条件应该是“在一趟排序过程中没有进行过交换元素的操作”。时间复杂度:O(n^2),不需要空间复杂度。是不稳定的排序。
- #include <iostream>
- using namespace std;
- void BubbleSort(int *a,int n)
- {
- int flag,temp;//标记是否进行过交换操作
- for(int i = 0;i < n - 1;++i)
- {
- flag = 0;
- for(int j = 0;j < n - 1 - i;++j)
- {
- if(*(a + j) > *(a + j + 1))
- {
- temp = *(a + j);
- *(a + j) = *(a + j + 1);
- *(a + j + 1) = temp;
- flag = 1;
- }
- }
- if(flag == 0)break;
- }
- }
- int main()
- {
- int n,temp;
- cout<<"please input the number of the values that need to sort:"<<endl;
- cin>>n;
- int *a = (int*)malloc(n * sizeof(int));
- cout<<"please input each value:"<<endl;
- for(int i = 0;i < n;++i)
- {
- cin>>temp;
- *(a + i) = temp;
- }
- BubbleSort(a,n);
- cout<<"the values after sort:"<<endl;
- for(int i = 0;i < n;++i)
- cout<<*(a + i)<<" ";
- <pre class="cpp" name="code"> free(a);
- }
【5】快速排序
它是对冒泡排序的一种改进。它的基本思想是:通过一趟排序将待排序元素分成两个部分,其中一部分元素比另一部分元素小。再分别对这两部分元素进行排序。以达到整个元素序列有序。时间复杂度:O(nlogn),空间复杂度O(1),是不稳定的算法。
代码:
- #include <iostream>
- using namespace std;
- int Partition(int *a,int low,int high)
- {
- int PivotKey = *(a + low);//用第一个元素做枢轴
- while(low < high)
- {
- while(low < high && *(a + high) > PivotKey)--high;
- *(a + low) = *(a + high);
- while(low < high && *(a + low) < PivotKey)++low;
- *(a + high) = *(a + low);
- }
- *(a + low) = PivotKey;
- return low;
- }
- void QuickSort(int *a,int low,int high)
- {
- if(low < high)
- {
- int PivotLoc = Partition(a,low,high);
- QuickSort(a,low,PivotLoc - 1);
- QuickSort(a,PivotLoc + 1,high);
- }
- }
- int main()
- {
- int n,temp;
- cout<<"please input the number of the values that need to sort:"<<endl;
- cin>>n;
- int *a = (int*)malloc(n * sizeof(int));
- cout<<"please input each value:"<<endl;
- for(int i = 0;i < n;++i)
- {
- cin>>temp;
- *(a + i) = temp;
- }
- QuickSort(a,0,n - 1);
- cout<<"the values after sort:"<<endl;
- for(int i = 0;i < n;++i)
- cout<<*(a + i)<<" ";
- free(a);
- }