1. 链表
创建链表和输出链表:
#include<stdio.h>
#include<stdlib.h>
struct Linklist{
int data;
struct Linklist *next;
};
int n;
struct Linklist *creat()
{
int i;
struct Linklist *head=(struct Linklist *)malloc(sizeof(struct Linklist));
head->next=NULL;
struct Linklist *p;
p=head;
scanf("%d",&n);
for(i=0;i<n;i++)
{
struct Linklist *s=(struct Linklist *)malloc(sizeof(struct Linklist));
scanf("%d",&s->data);
p->next=s;
s->next=NULL;
p=s;
}
return head;
}
void print(struct Linklist *p)
{
while(p->next!=NULL)
{
printf("%-3d",p->next->data);
p=p->next;
}
}
int main()
{
struct Linklist *head;
head=creat();
print(head);
putchar('\n');
printf("%d",n);
return 0;
}
2. 排序
2.1 冒泡排序
#include <stdio.h>
int main()
{
//冒泡排序,也是一种简单直观的排序算法。
//它重复地走访过要排序的数列,一次比较两个元素,
//如果他们的顺序错误就把他们交换过来。
int a[10]={1,2,3,4,5,6,7,8,9,0},i,j,t;
for(i=0;i<9;i++)
for(j=0;j<9;j++)
{
if(a[j]>a[j+1])
t=a[j],a[j]=a[j+1],a[j+1]=t;
}
for(i=0;i<10;i++)
printf("%2d",a[i]);
return 0;
}
2.2 选择排序
#include <stdio.h>
int main()
{
//选择排序
//首先在未排序序列中找到最小(大)元素,
//存放到排序序列的起始位置。
//再从剩余未排序元素中继续寻找最小(大)元素,
//然后放到已排序序列的末尾。
//重复第二步,直到所有元素均排序完毕
int a[10]={1,2,3,4,5,6,7,8,9,0},i,j,t,max;
for(i=0;i<10;i++)
{
max=i;
for(j=i+1;j<10;j++)
if(a[j]>a[max])
max=j;
t=a[i],a[i]=a[max],a[max]=t;
}
for(i=0;i<10;i++)
printf("%2d",a[i]);
return 0;
}
2.3 快速排序
#include <stdio.h>
void print(int *a,int len)
{
int i;
for(i=0;i<len;i++)
printf("%3d",a[i]);
putchar('\n');
}
void swap(int *a,int *b)
{
int t;
t=*a;
*a=*b;
*b=t;
}
void merge_sort_recursive(int *arr,int start,int end)
{
if(start>=end)
return;
int mid=arr[end];
int left=start,right=end-1;
while(left<right)
{
while(arr[left]<mid && left<right)
left++;
while(arr[right]>=mid && left<right)
right--;
swap(&arr[left],&arr[right]);
}
if(arr[left]>=arr[end])
swap(&arr[left],&arr[end]);
else
left++;
print(arr,10);
if(left)
merge_sort_recursive(arr,start,left-1);
merge_sort_recursive(arr,left+1,end);
}
void merge_sort(int *arr,int len)
{
merge_sort_recursive(arr,0,len-1);
}
int main()
{
int a[10]={3,6,2,7,1,3,7,1,2,7},i,j,len,max,key,gap;
len=10;
print(a,10);
merge_sort(a,10);
print(a,10);
}
2.4 希尔排序
#include <stdio.h>
void print(int a[],int len)
{
int i;
for(i=0;i<len;i++)
printf("%2d",a[i]);
printf("\n");
}
void shell(int a[],int len)
{
int i,j,gap,temp;
for(gap=len/2;gap>0;gap/=2)
{
for(i=gap;i<len;i++)
{
temp = a[i];
for(j=i-gap;j>=0 && a[j]>temp;j-=gap)
a[j+gap] = a[j];
a[j+gap] = temp;
}
print(a,len);
}
}
int main()
{
int a[10]={1,8,3,6,5,4,7,2,9,0};
print(a,10);
shell(a,10);
return 0;
}
2.5 归并排序
#include <stdio.h>
#include <math.h>
void print(int *a,int len)
{
int i;
for(i=0;i<len;i++)
printf("%3d",a[i]);
putchar('\n');
}
void merge_sort_recursive(int *a,int *arr,int start,int end)
{
if(start>=end)
return;
int mid=(end-start)/2+start;
int start1=start,end1=mid;
int start2=mid+1,end2=end;
merge_sort_recursive(a,arr,start1,end1);
merge_sort_recursive(a,arr,start2,end2);
int k=start;
while(start1 <= end1 && start2 <= end2)
{
if(a[start1]<a[start2])
arr[k++]=a[start1++];
else
arr[k++]=a[start2++];
}
while(start1 <= end1)
arr[k++]=a[start1++];
while(start2 <= end2)
arr[k++]=a[start2++];
while(start<=end)
{
a[start]=arr[start];
start++;
}
print(a,10);
}
void merge_sort(int *a,int len)
{
int arr[10];
merge_sort_recursive(a,arr,0,len-1);
}
int main()
{
int a[10]={3,6,2,7,1,2,7,1,2,7};
print(a,10);
merge_sort(a,10);
print(a,10);
}
2.6 堆排序
#include <stdio.h>
void swap(int *a,int *b){
int t;
t=*a;
*a=*b;
*b=t;
}
void print(int *a,int len){
int i;
for(i=0;i<len;i++)
{
printf("%-3d",a[i]);
}
putchar('\n');
}
// a 数组 len 数组长度 i待维护节点的下标
void heapify(int *a,int len,int i)
{
int largest=i;
int ld=i*2+1,rd=i*2+2;
if(ld<len && a[largest]<a[ld])
largest=ld;
if(rd<len && a[largest]<a[rd])
largest=rd;
if(largest != i)
{
swap(&a[i],&a[largest]);
heapify(a,len,largest);
}
}
void heap_sort(int *a,int len)
{
int i;
//建堆
for(i=len/2-1;i>=0;i--)
heapify(a,len,i);
//排序
for(i=len-1;i>0;i--)
{
swap(&a[i],&a[0]);
heapify(a,i,0);
}
}
int main(){
int a[10]={4,5,6,7,9,8,1,2,3,0};
print(a,10);
heap_sort(a,10);
print(a,10);
}
3. 查找
3.1 二分查找
#include<stdio.h>
void swap(int *a,int *b)
{
int t;
t=*a;
*a=*b;
*b=t;
}
//二分查找的数组必须是有序的
int bin_serch(int *a,int len,int target)
{
int left=0,right=len-1,mid;
while(left<=right)
{
mid=(right-left)/2+left;
if(target<a[mid])
right=mid-1;
else if(target>a[mid])
left=mid+1;
else
return mid;
}
return left;
}
int main(){
int a[10]={0,1,2,3,4,5,6,7,8,9},i,n;
printf("%d",bin_serch(a,10,3));
}
3.2 选择查找算法大综合
#include <stdio.h>
#include <string.h>
void swap(int *a,int *b){
int t;
t=*a;
*a=*b;
*b=t;
}
void print(int *a,int len){
int i;
for(i=0;i<len;i++)
printf("%-3d",a[i]);
putchar('\n');
}
void bubble(int *a,int len){
int i,j;
for(i=0;i<len-1;i++)
for(j=0;j<len-1-i;i++)
if(a[j]>a[j+1])
swap(&a[j],&a[j+1]);
}
void select(int *a,int len){
int i,j,min;
for(i=0;i<len;i++)
{
min=i;
for(j=i+1;j<len;j++)
if(a[min]>a[j])
min=j;
swap(&a[min],&a[i]);
}
}
void insert(int *a,int len){
int i,j,key;
for(i=1;i<len;i++)
{
key=a[i];
j=i-1;
while(j>=0 && a[j]>key)
{
a[j+1]=a[j];
j--;
}
a[j+1]=key;
}
}
void shell(int *a,int len){
int i,j,temp,gap;
for(gap=len/2;gap>0;gap/=2)
for(i=gap;i<len;i++)
{
temp=a[i];
for(j=i-gap;j>=0 && a[j]>temp;j-=gap)
a[j+gap]=a[j];
a[j+gap]=temp;
}
}
void merge_sort_recursive(int *a,int *arr,int start,int end){
if(start>=end)
return;
int mid=(end-start)/2+start;
int start1=start,end1=mid;
int start2=mid+1,end2=end;
merge_sort_recursive(a,arr,start1,end1);
merge_sort_recursive(a,arr,start2,end2);
int k=start;
while(start1<=end1 && start2<=end2)
{
if(a[start1]<a[start2])
arr[k++]=a[start1++];
else
arr[k++]=a[start2++];
}
while(start1<=end1)
arr[k++]=a[start1++];
while(start2<=end2)
arr[k++]=a[start2++];
k=start;
while(k<=end)
{
a[k]=arr[k];
k++;
}
}
void quick_sort_recursive(int *a,int start,int end){
if(start>=end)
return;
int mid=a[end];
int left=start,right=end-1;
while(left<right)
{
while(a[left]<mid && left<right)
left++;
while(a[right]>=mid && left<right)
right--;
swap(&a[left],&a[right]);
}
if(a[left]>=a[end])
swap(&a[left],&a[end]);
else
left++;
if(left)
quick_sort_recursive(a,start,left-1);
quick_sort_recursive(a,left+1,end);
}
int bin_search(int *a,int len,int target){
int left=0,right=len-1;
while(left<=right)
{
int mid=(right-left)/2+left;
if(target>a[mid])
left=mid+1;
else if(target<a[mid])
right=mid-1;
else
return mid;
}
return left;
}
int main(){
int a[10]={35,34,33,32,31,15,14,13,12,11},arr[10];
print(a,10);
// bubble(a,10);
// select(a,10);
// insert(a,10);
// shell(a,10);
// merge_sort_recursive(a,arr,0,9);
quick_sort_recursive(a,0,9);
print(a,10);
printf("%d的位置%d",34,bin_search(a,10,34));
}