一、简单排序算法
1、冒泡排序
核心思想:相邻关键字两两比较。
//冒泡排序法
int bubbleSort(int* a,int n)
{
if(a==NULL||n<=0){
return -1;
}
for(int i = 0;i<n;i++)
{
for(int k = n-1;k>i;k--)
{
if(a[k]<a[k-1])
{
swap(&a[k],&a[k-1]);
}
}
}
}
选择排序法
2、简单选择排序
核心思想:每次循环选择记录中的最小值,并将该值插在队列前面
//选择排序法
void selectSort(int* a,int n)
{
for(int i = 0;i<n;i++)
{
int min = i;//每次寻找最小值所在的位置,然后交换。否则数组的每一个值都会被同一最小值填充,例如6 5 4 3 2 1排序结果为1 1 1 1 1 1
for(int k = i+1;k<n;k++)
{
if(a[k]<a[min])
{
min = k;
}
}
swap(&a[i],&a[min]);
}
}
3、插入排序
核心思想:通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应的位置并插入。这里不涉及两两交换。需要一个记录的辅助空间。
void insertSort(int* a,int length)
{
int i,j;
//假定第一个元素被放到了正确的位置上
//这样,仅需遍历length-1次
for(i=1;i<length;i++){
if(a[i]<a[i-1]){
int target = a[i];//辅助空间
j = i;
while(j>0&&a[j-1]>target){
a[j] = a[j-1];
j--;
}
a[j] = target;
}
}
}
二、改进过的排序算法
1、希尔排序
核心思想:分组插入排序
void shellSort(int* a,int length)
{
int i,gap;
for(gap = length/2;gap>0;gap/=2)//步长
{
for(i=gap;i<length;i++)//直接插入排序
{
if(a[i]<a[i-gap])
{
int temp = a[i];
int j = i-gap;
while(j>=0&&a[j]>temp)
{
a[j+gap] = a[j];
j-=gap;
}
a[j+gap] = temp;
}
}
}
}
2、归并排序
核心思想:分治,归并的含义是,把两个已序的数组合并成一个已序的大数组。
void mergeArray(int* arr,int l,int mid,int r,int* aux)
{
<span style="white-space:pre"> </span>int i = l, j = mid + 1;
<span style="white-space:pre"> </span>int k = 0;
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>while (i <= mid && j <= r)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>if (arr[i] <= arr[j])
<span style="white-space:pre"> </span>aux[k++] = arr[i++];
<span style="white-space:pre"> </span>else
<span style="white-space:pre"> </span>aux[k++] = arr[j++];
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>while (i <= mid){
<span style="white-space:pre"> </span>aux[k++] = arr[i++];
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>while (j <= r){
<span style="white-space:pre"> </span>aux[k++] = arr[j++];
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>for (i = 0; i < k; i++)
<span style="white-space:pre"> </span>arr[l + i] = aux[i];
}
void mergeSort_r(int* arr,int l,int r,int* aux)
{
if(l<r)
{
int mid = (l+r)/2;
mergeSort_r(arr,l,mid,aux);
mergeSort_r(arr,mid+1,r,aux);
mergeArray(arr,l,mid,r,aux);
}
}
void mergeSort(int* arr,int length)
{
int *aux = (int*)malloc(sizeof(int)*(length));
mergeSort_r(arr,0,length-1,aux);
free(aux);
}
3、快速排序
核心思想:本质和冒泡法一样,两两交换。
void quickSort(int* a,int length){
qSort(a,0,length-1);
}
void qSort(int *a,int low,int high){
int pivot;//pivot is the location of pivot;
if(low<high){
pivot = partition(a,low,high);
qSort(a,low,pivot-1);
qSort(a,pivot+1,high);
}
}
int partition(int *a,int low,int high){
int pivotKey = a[low];//pivotkey is the value of pivot, it won't change in a circle
while(low<high){
while(low<high&&a[high]>=pivotKey){
high--;
}
swap(&a[low],&a[high]);
while(low<high&&a[low]<=pivotKey){
low++;
}
swap(&a[low],&a[high]);
return low;
}
}
4、堆排序
选择排序的改进--堆排序法
void HeapAdjust(int *a,int i,int size) //调整堆
{
int lchild = 2*i;
int rchild = 2*i+1;
int max = i;
if(i<=size/2)
{
if(a[lchild]>a[max]&&lchild<=size)
{
max = lchild;
}
if(a[rchild]>a[max]&&rchild<=size)
{
max = rchild;
}
if(max != i)
{
swap(&a[i],&a[max]);
HeapAdjust(a,max,size);//important
}
}
}
void BuildHeap(int* a,int size)
{
int i;
for(i = size/2;i>=0;i--)
{
HeapAdjust(a,i,size);
}
}
void HeapSort(int* a,int size)//无序数组构成一个有序堆
{
int i;
BuildHeap(a,size);
for(i = size;i>=0;i--)
{
swap(&a[0],&a[i]);
HeapAdjust(a,0,i-1);
}
}
三、总结