插入排序
1.直接插入排序
原理:将数组分为无序区和有序区两个区,然后不断将无序区的第一个元素按大小顺序插入到有序区中去,最终将所有无序区元素都移动到有序区完成排序。
要点:设立哨兵,作为临时存储和判断数组边界之用。
实现:
[liul@test algorithms]$ more InsertSort.c
- #include<stdio.h>
- void InsertSort(int L[],int length)
- {
- int i,j,Tmp;
- for(i=1;i<length;i++)
- {
- j=i+1;
- if(L[j]<L[i])
- {
- Tmp=L[j];
- while(L[0]<L[i])
- {
- L[i+1]=L[i];
- i--;
- }
- L[i+1]=Tmp;
- }
- }
- }
- main()
- {
- int i,length=7;
- int L[7]={4,7,6,9,8,7,9};
- InsertSort(L,5);
- for(i=0;i<length;i++)
- {
- printf("%d ",L[i]);
- }
- printf("\n");
- }
- [liul@test algorithms]$ gcc InsertSort.c -o InsertSort
- [liul@test algorithms]$ ./InsertSort
- 4 6 7 7 8 9 9
2.希尔排序
原理:又称增量缩小排序。先将序列按增量划分为元素个数相同的若干组,使用直接插入排序法进行排序,然后不断缩小增量直至为1,最后使用直接插入排序完成排序。
要点:增量的选择以及排序最终以1为增量进行排序结束。
实现:
- [liul@test algorithms]$ more ShellSort.c
- #include<stdio.h>
- void ShellSort(int L[],int length)
- {
- int i,j,Tmp,k=length-1;
- while(k>0)
- {
- k/=2;
- for(i=k;i<length;i++)
- {
- Tmp=L[i];
- j=i-k;
- while(j>=0 && Tmp<L[j])
- {
- L[j+k]=L[j];
- j=j-k;
- }
- L[j+k]=Tmp;
- }
- }
- }
- int main()
- {
- int i,length=8;
- int L[8]={4,7,6,9,8,7,9,2};
- ShellSort(L,8);
- for(i=0;i<length;i++)
- {
- printf("%d ",L[i]);
- }
- printf("\n");
- }
- [liul@test algorithms]$ gcc ShellSort.c -o ShellSort
- [liul@test algorithms]$ ./ShellSort
- 2 4 6 7 7 8 9 9
- [liul@test algorithms]$
交换排序
1.冒泡排序
原理:将序列划分为无序和有序区,不断通过交换较大元素至无序区尾完成排序。
要点:设计交换判断条件,提前结束以排好序的序列循环。
实现:
- [liul@test algorithms]$ more BubbleSort.c
- #include<stdio.h>
- void BubbleSort(int L[],int length)
- {
- int i,j,Tmp;
- for(i=0;i<length;i++)
- for(j=0;j<length-i;j++)
- {
- if(L[j]>L[j+1])
- {
- Tmp=L[j];
- L[j]=L[j+1];
- L[j+1]=Tmp;
- }
- }
- }
- main()
- {
- int i,length=7;
- int L[7]={4,7,6,9,8,7,9};
- BubbleSort(L,5);
- for(i=0;i<length;i++)
- {
- printf("%d ",L[i]);
- }
- printf("\n");
- }
- [liul@test algorithms]$ gcc BubbleSort.c -o BubbleSort
- [liul@test algorithms]$ ./BubbleSort
- 4 6 7 7 8 9 9
- [liul@test algorithms]$
2.快速排序
原理:不断寻找一个序列的中点,然后对中点左右的序列递归的进行排序,直至全部序列排序完成,使用了分治的思想。
要点:递归、分治
实现:
选择排序
1.直接选择排序
原理:将序列划分为无序和有序区,寻找无序区中的最小值和无序区的首元素交换,有序区扩大一个,循环最终完成全部排序。
要点:
实现:
Void SelectSort(Node L[])
{
Int i,j,k;//分别为有序区,无序区,无序区最小元素指针
For(i=0;i<length;i++)
{
k=i;
For(j=i+1;j<length;j++)
{
If(L[j]<L[k])
k=j;
}
If(k!=i)//若发现最小元素,则移动到有序区
{
Int temp=L[k];
L[k]=L[i];
L[i]=L[temp];
}
}
}
2.堆排序
原理:利用大根堆或小根堆思想,首先建立堆,然后将堆首与堆尾交换,堆尾之后为有序区。
要点:建堆、交换、调整堆
实现:
Void HeapSort(Node L[])
{
BuildingHeap(L);//建堆(大根堆)
For(int i=n;i>0;i--)//交换
{
Int temp=L[i];
L[i]=L[0];
L[0]=temp;
Heapify(L,0,i);//调整堆
}
}
Void BuildingHeap(Node L[])
{ For(i=length/2 -1;i>0;i--)
Heapify(L,i,length);
}
归并排序
原理:将原序列划分为有序的两个序列,然后利用归并算法进行合并,合并之后即为有序序列。
要点:归并、分治
实现:
- Void MergeSort(Node L[],int m,int n)
- {
- Int k;
- If(m<n)
- {
- K=(m+n)/2;
- MergeSort(L,m,k);
- MergeSort(L,k+1,n);
- Merge(L,m,k,n);
- }
- }
- [liul@test algorithms]$ more MergeSort.c
- #include<stdio.h>
- #include<stdlib.h>
- void Merge(int L[],int p,int q,int r)
- {
- int i,k;
- int begin1,end1,begin2,end2;
- int * Tmp = (int *)malloc((r-p+1)*sizeof(int));
- begin1=p;
- end1=q;
- begin2=q+1;
- end2=r;
- k=0;
- while((begin1<=end1)&&(begin2<=end2))
- {
- if(L[begin1]<L[begin2])
- {
- Tmp[k]=L[begin1];
- begin1++;
- }
- else
- {
- Tmp[k]=L[begin2];
- begin2++;
- }
- k++;
- }
- while(begin1<=end1)
- {
- Tmp[k++]=L[begin1++];
- }
- while(begin2<=end2)
- {
- Tmp[k++]=L[begin2++];
- }
- for(i=0;i<(r-p+1);i++)
- L[p+i]=Tmp[i];
- free(Tmp);
- }
- void MergeSort(int L[],int first,int last)
- {
- int mid=0;
- if(first<last)
- {
- mid=(first+last)/2;
- MergeSort(L,first,mid);
- MergeSort(L,mid+1,last);
- Merge(L,first,mid,last);
- }
- }
- int main()
- {
- int i,length=8;
- int L[8]={4,7,6,9,8,7,9,2};
- MergeSort(L,0,7);
- for(i=0;i<length;i++)
- {
- printf("%d ",L[i]);
- }
- printf("\n");
- }
- [liul@test algorithms]$ gcc MergeSort.c
- [liul@test algorithms]$ gcc MergeSort.c -o MergeSort
- [liul@test algorithms]$ ./MergeSort
- 2 4 6 7 7 8 9 9
- [liul@test algorithms]$
基数排序
原理:将数字按位数划分出n个关键字,每次针对一个关键字进行排序,然后针对排序后的序列进行下一个关键字的排序,循环至所有关键字都使用过则排序完成。
要点:对关键字的选取,元素分配收集。
实现:
Void RadixSort(Node L[],length,maxradix)
{
Int m,n,k,lsp;
k=1;m=1;
Int temp[10][length-1];
Empty(temp); //清空临时空间
While(k<maxradix) //遍历所有关键字
{
For(int i=0;i<length;i++) //分配过程
{
If(L[i]<m)
Temp[0][n]=L[i];
Else
Lsp=(L[i]/m)%10; //确定关键字
Temp[lsp][n]=L[i];
n++;
}
CollectElement(L,Temp); //收集
n=0;
m=m*10;
k++;
}
}