排序算法
1 归并排序
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void merge(int arr[],int left,int mid,int right,int* temp){
int i = left;
int j = mid+1;
int k = left;
while(i<=mid && j<=right){
if(arr[i]<arr[j]){
temp[k]=arr[i];
++k;
++i;
}else{
temp[k]=arr[j];
++k;
++j;
}
}
while(i<=mid){
temp[k]=arr[i];
++k;
++i;
}
while(j<=right){
temp[k]=arr[j];
++k;
++j;
}
for(i=left;i<=right;++i){
arr[i]=temp[i];
}
}
void mergeSort(int arr[],int left,int right,int* temp){
if(left>=right){
return;
}
int mid=left+(right-left)/2;
mergeSort(arr,left,mid,temp);//左归并
mergeSort(arr,mid+1,right,temp);//右归并
merge(arr,left,mid,right,temp);
}
int main(){
int arr[6]={4,2,5,3,7,6};
int temp[6]={0};
mergeSort(arr,0,5,temp);
for(int i=0;i<6;++i){
printf("%d ",arr[i]);
}
printf("\n");
return 0;
}
2 冒泡排序
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void swap(int arr[],int i,int j){
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
void BubbleSort(int arr[],int left,int right){
int i = 0;
int j = 0;
int flag = 1;//增加标志位,改进冒泡排序
for(i=left;i<=right && flag;++i){
flag=0;
for(j=right;i<j;--j){
if(arr[j]<arr[j-1]){
swap(arr,j,j-1);
flag=1;
}
}
}
}
int main(){
int arr[10]={9,4,6,8,5,0,1,3,2,7};
BubbleSort(arr,0,9);
for(int i=0;i<10;++i){
printf("%d\n",arr[i]);
}
return 0;
}
3 快速排序(版本一)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void swap(int arr[],int i,int j){
int temp = arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
void quickSort(int arr[],int left,int right){
if(left>=right){
return;
}
int i = left;
int j = right;
int pivot = arr[i];
while(i<j){
while(i<j && pivot <= arr[j]){
j--;
}
swap(arr,i,j);
while(i<j && pivot >= arr[i]){
i++;
}
swap(arr,i,j);
}
quickSort(arr,left,i-1);
quickSort(arr,i+1,right);
}
int main(){
int arr[10]={5,3,6,1,4,8,2,0,7,9};
quickSort(arr,0,9);
for(int i=0;i<10;++i){
printf("%d ",arr[i]);
}
printf("\n");
return 0;
}
快速排序(版本2)
由于是单向的,可为单向链表提供参考。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void swap(int arr[],int i,int j){
int temp = arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
void quickSort(int arr[],int left,int right){
if(left>=right)
return;
int pivot = arr[left];
int i = left +1;
int j = left +1;
while(j<=right){
if(pivot > arr[j]){
swap(arr,i,j);
++i;
}
++j;
}
swap(arr,left,i-1);
quickSort(arr,left,i-2);
quickSort(arr,i,right);
}
int main(){
int arr[10]={5,3,6,1,4,8,2,0,7,9};
quickSort(arr,0,9);
for(int i=0;i<10;++i){
printf("%d ",arr[i]);
}
printf("\n");
return 0;
}
4 字符串查找算法
KMP算法
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//计算next数组
void getNext(const char* substr,int* next){
next[0]=-1;
next[1]=0;
int i=2;
int k=0;
int size=strlen(substr);
while(i<size){
if(k==-1 || substr[i-1]==substr[k]){
next[i]=k+1;
++i;
++k;
}else{
k=next[k];//对k进行回溯
}
}
}
//KMP实现
int KMP(const char* str,const char* substr,int pos){
int next[255]={0};
getNext(substr,next);
int i = pos;
int j = 0;
int sizeStr = strlen(str);
int sizeSub = strlen(substr);
if(sizeStr==0 || sizeSub==0){
return -1;
}
if(pos<0 || pos>sizeStr)
return -1;
while(i<sizeStr && j<sizeSub){
if(j==-1 || str[i]==substr[j]){
++i;
++j;
}else{
j=next[j];//对j进行回溯
}
}
if(j>=sizeSub){
return i-j;
}else{
return -1;
}
}
int main(){
printf("%d\n",KMP("Helloworld","world",0));
return 0;
}