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-] > arraySort[j])
{
swap(arraySort[j-], 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+; 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+; i<hightIndex; ++i)
{
Type tempValue = arraySort[i];
j = i-;
while (j>= && temValue<arraySort[j])
{
arraySort[j+] = arraySort[j];
--j;
}
arraySort[j+] = 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+, hightIndex);
QuickSort(arraySort, lowIndex; i-);
} 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+;
int n1 = midIndex-leftIndex+;
int n2 = rightIndex-midIndex;
int k = ; 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=; i<n1; ++i)
{
arraySort[leftIndex++] = tempArray[i];
}
tempIndex = midIndex+;
for (int i=; 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)/;
MergeSort(arraySort, leftIndex, midIndex);
MergeSort(arraySort, midIndex+, rightIndex);
Merge(arraySort, leftIndex, midIndex, rightIndex);
}
}

最新文章

  1. c#面向对象基础技能——学习笔记(三)基于OOP思想研究对象的【方法】
  2. iPhone系列设备媒体查询:
  3. 使用opencv自带的融合函数
  4. The Brain vs Deep Learning Part I: Computational Complexity — Or Why the Singularity Is Nowhere Near
  5. Sql Server 2008 还原数据库 3154错误
  6. ALTIUM 10 过孔设置开窗、不开窗
  7. Android JNI入门第四篇——Android.mk文件分析
  8. 自己用 Netty 实现一个简单的 RPC
  9. [2019.03.20]Linux Shell 执行传参数和expr
  10. SpringCloud应对高并发的思路
  11. source insight如何删除没用的project 及其常见问题
  12. ArcGis恢复初始设置(默认设置、出厂设置)的方法
  13. (转)volatile 的理解
  14. Anatomy of a Database System学习笔记 - 概论、并发控制
  15. 【Data Structure】-NO.117.DS.1 -【Tree-23树】
  16. Excel--按内容分页打印
  17. SAP 官网中文帮助文件&amp;BP中文资料汇总
  18. 利用PCA进行故障监测
  19. 用户授权的Sql脚本
  20. &quot;characterEncoding&quot; must end with the &#39;;&#39; delimiter.

热门文章

  1. asp.net core新特性(1):TagHelper
  2. 利用powershell反弹shell到metasploit
  3. 网络爬虫——针对任意主题批量爬取PDF
  4. 字符串(String)
  5. Mongoose基础入门
  6. Java并发编程(2):线程中断(含代码)
  7. 本地存储之cookie、localStorage、sessionStorage
  8. 2017年1月1日 App Store中的所有应用都必须启用 App Transport Security安全功能
  9. 机器学习 —— 基础整理(八)循环神经网络的BPTT算法步骤整理;梯度消失与梯度爆炸
  10. Java之IO流补充