最近看了算法的东西,将一些查找和排序的算法罗列一下,尽量做到不用查baidu,直接记在脑子里面。
1.排序:
1.1冒泡排序
#include<stdio.h>
//bubble sort
void bubSearch(int a[]){
int i,j;
int tmp;
for(i=0;i<10;i++){
for(j=0;j<10-i;j++){
if(a[j]>a[j+1]){
//swap(&a[j],&a[j+1]);
tmp = a[j];
a[j] = a[j+1];
a[j+1] = tmp;
}
}
}
}
int main(int argc, char* argv[])
{
int array[10]={1,3,2,4,9,5,0,8,6,7};
int i=0;
while(i<10){
printf("%d",array[i++]);
}
printf("\n");
bubSearch(array);
i=0;
while(i<10){
printf("%d",array[i++]);
}
return 0;
}
1.2 插入排序
void inSort(int a[], int n)
{
int i,j;
for(i=1;i<n;i++)
{
int tmp=a[i];
for(j=i;j>0&&tmp<a[j-1];j--)
a[j]=a[j-1];
a[j] = tmp;
}
}
1.3 归并排序
1.4 快速排序
void swap(int* x, int* y)
{
int tmp;
tmp = *x;
*x = *y;
*y = tmp;
}
int mid(int a[], int left, int right){
int mid = (left+right)/2;
if(a[left]>a[right])
swap(&a[left],&a[mid]);
if(a[left]>a[mid])
swap(&a[left],&a[right]);
if(a[mid]>a[right])
swap(&a[mid],&a[right]);
swap(&a[mid],&a[right-1]);
return a[right-1];
}
void qSort(int a[], int left, int right)
{
int i,j;
if(left<=right){
int pivot = mid(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]);
qSort(a,left,i-1);
qSort(a,i+1,right);
}
}
void qsort(int a[], int n)
{
int i, last;
if(n<=1)
return;
swap(&a[0],&a[rand()%n]);
last = 0;
for(i=0;i<n;i++)
if(a[i]<a[0])
swap(&a[i],&a[++last]);
swap(&a[last],&a[0]);
qsort(a,last);
qsort(a+last+1,n-last-1);
}
中值快排-从大到小
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
void quicksort(int arr[],int l,int r);
void swap(int &a, int &b){
int tmp;
tmp = a;
a = b;
b = tmp;
}
int middle(int arr[], int l, int r){
int mid = (l+r)/2;
if(arr[l]<arr[mid]) swap(arr[l],arr[mid]);
if(arr[l]<arr[r]) swap(arr[l],arr[r]);
if(arr[mid]<arr[r]) swap(arr[mid],arr[r]);
swap(arr[l],arr[mid]);
return arr[l];
}
int main(int argc, int* argv){
int array[20]={1,2,2,4,2,6,7,2,9,10,10,10,4,5};
quicksort(array,0,18);
for(int i=0;i<20;i++)
printf("%d ",array[i]);
}
void quicksort(int arr[], int l, int r){
int i,j,pivot;
if(l<r){
while(1){
i = l;j=r;
pivot = middle(arr,l,r);
while(arr[++i]>=pivot);
while(arr[--j]<pivot);
if(i<j)
swap(arr[i],arr[j]);
else
break;
}
swap(arr[l],arr[j]);
quicksort(arr,l,j-1);
quicksort(arr,j+1,r);
}
}
/* NOTE
* 1.int middle() 大小符号改变
* 2.while(arr[++i]>=pivot); while(arr[--j]<pivot); 大小符号改变,注意=号要保留
* 从小到大的排序可通过上述两个改变实现。
*/
2. 查找
2.1 二分查找
int bisearch(int a[], int n, int key)
{
int left,right,mid;
left = 0;
right = n-1;
while(left<=right)
{
mid = (left+right)/2;
if (a[mid] == key)
return mid;
else if (key< a[mid])
right = mid-1;
else
left = mid+1;
}
return -1;
}
2.2 哈希查找
hash:最大的问题是应该考虑如何解决冲突,是用链表,还是再哈希
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
#include<windows.h>
#include<assert.h>
#include<algorithm>
#define MAX_NUM 10
typedef struct{
int num;
char name[20];
}ElemType;
typedef struct{
ElemType *elem;
int count;
int index;
}HashTable;
int hash(int num){
return num%5;
}
void InitHash(HashTable *h){
h->elem = (ElemType*)malloc(MAX_NUM*sizeof(ElemType));
h->count = 0;
h->index = 0;
for(int i=0;i<MAX_NUM;i++)
h->elem[i].num = 0;
}
int searchHash(HashTable *h, int key, int *p){
int hkey = hash(key);
hkey = *p;
while(h->elem[hkey].num!=key && h->elem[hkey].num!=0){
//this is the hash collision
//re-hash again
//.....
}
if(h->elem[hkey].num == key)
return 1;//already exist
else
return 0;//not exist
}
void insertHash(HashTable *h, ElemType e){
int p;
searchHash(h,e.num,&p);
h->elem[p]=e;
++h->count;
}
int main(int argc, char* argv[])
{
if(!searchHash())
insertHash();
else
printf("already exist\n");
printf("\n");
return 0;
}
3.算法设计
1.分治
1.1 X的N次方___O(LogN)
int qpow(int x, int n)
{
if (n==1) return x;
int v = qpow(x,n/2);
if(n%2) return v*v*x;
else return v*v;
}