1) 快速排序
#include <stdio.h>
int partition(int *arr,int low,int high)
{
int pivot=arr[high];
int i=low-1;
int j,tmp;
for(j=low;j<high;++j)
if(arr[j]<pivot){
tmp=arr[++i];
arr[i]=arr[j];
arr[j]=tmp;
}
tmp=arr[i+1];
arr[i+1]=arr[high];
arr[high]=tmp;
return i+1;
}
void quick_sort(int *arr,int low,int high)
{
if(low<high){
int mid=partition(arr,low,high);
quick_sort(arr,low,mid-1);
quick_sort(arr,mid+1,high);
}
}
//test
int main()
{
int arr[10]={1,4,6,2,5,8,7,6,9,12};
quick_sort(arr,0,9);
int i;
for(i=0;i<10;++i)
printf("%d ",arr[i]);
}
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#define PARENT(i) (i)/2
#define LEFT(i) 2*(i)+1
#define RIGHT(i) 2*(i+1)
void swap(int *a,int *b)
{
*a=*a^*b;
*b=*a^*b;
*a=*a^*b;
}
void max_heapify(int *arr,int index,int len)
{
int l=LEFT(index);
int r=RIGHT(index);
int largest;
if(l<len && arr[l]>arr[index])
largest=l;
else
largest=index;
if(r<len && arr[r]>arr[largest])
largest=r;
if(largest != index){//将最大元素提升,并递归
swap(&arr[largest],&arr[index]);
max_heapify(arr,largest,len);
}
}
void build_maxheap(int *arr,int len)
{
int i;
if(arr==NULL || len<=1)
return;
for(i=len/2+1;i>=0;--i)
max_heapify(arr,i,len);
}
void heap_sort(int *arr,int len)
{
int i;
if(arr==NULL || len<=1)
return;
build_maxheap(arr,len);
for(i=len-1;i>=1;--i){
swap(&arr[0],&arr[i]);
max_heapify(arr,0,--len);
}
}
int main()
{
int arr[10]={1,4,6,2,5,8,7,6,9,12};
int i;
heap_sort(arr,10);
for(i=0;i<10;++i)
printf("%d ",arr[i]);
system("pause");
}
3)归并排序
#include <stdio.h>
#include <stdlib.h>
#define MAX_INT ~(1<<31)//最大整数
//arr[p,q] arr[q+1,r]
void merge(int *arr,int p,int q,int r)
{
if(arr==NULL)
return;
int n1=q-p+1;
int n2=r-q;
int *L=(int*)malloc((n1+1)*sizeof(int));
int *R=(int*)malloc((n2+1)*sizeof(int));
int i,j;
for(i=0;i<n1;++i)
L[i]=arr[p+i];
for(j=0;j<n2;++j)
R[j]=arr[q+j+1];
//哨兵元素赋值
L[n1]=MAX_INT;
R[n2]=MAX_INT;
int k;
i=0,j=0;
for(k=p;k<=r;++k){
if(L[i]<=R[j])
arr[k]=L[i++];
else
arr[k]=R[j++];
}
free(L);
free(R);
}
void merge_sort(int *arr,int p,int r)
{
if(p<r){
int q=(r+p)/2;
merge_sort(arr,p,q);//分治
merge_sort(arr,q+1,r);
merge(arr,p,q,r);//合并结果
}
}
int main()
{
int arr[8]={32,3,4,5,6,7,9,106};
merge_sort(arr,0,7);
for (int i=0;i<8;i++)
{
printf("%d ",arr[i]);
}
system("pause");
}
4)二分查找
//算法所操作的区间,是左闭右开区间,还是左闭右闭区间,这个区间,需要在循环初始化,
//循环体是否终止的判断中,以及每次修改left,right区间值这三个地方保持一致,否则就可能出错.
//二分查找实现一
int BinarySearch(int array[], int n, int v)
{
int left, right, middle;
left = 0, right = n - 1;
while (left <= right)
{
middle = left + (right - left) / 2;
if (array[middle] > v)
{
right = middle - 1;
}
else if (array[middle] < v)
{
left = middle + 1;
}
else
{
return middle;
}
}
return -1;
}
//二分查找实现二
int BinarySearch(int array[], int n, int v)
{
int left, right, middle;
left = 0, right = n;
while (left < right)
{
middle = left + (right - left) / 2;
if (array[middle] > v)
{
right = middle;
}
else if (array[middle] < v)
{
left = middle + 1;
}
else
{
return middle;
}
}
return -1;
}
5)线性查找
//QuickSelect 将第k小的元素放在 a[k-1]
void QuickSelect( int a[], int k, int left, int right )
{
int i, j;
int pivot;
if( left + cutoff <= right )
{
pivot = median3( a, left, right );
//取三数中值作为枢纽元,可以很大程度上避免最坏情况
i = left; j = right - 1;
for( ; ; )
{
while( a[ ++i ] < pivot ){ }
while( a[ --j ] > pivot ){ }
if( i < j )
swap( &a[ i ], &a[ j ] );
else
break;
}
//重置枢纽元
swap( &a[ i ], &a[ right - 1 ] );
if( k <= i )
QuickSelect( a, k, left, i - 1 );
else if( k > i + 1 )
QuickSelect( a, k, i + 1, right );
}
else
InsertSort( a + left, right - left + 1 );
}