摘要:折半查找的前提是有序的,与中间值进行比较,如果有序的则返回;否则,改变始末位置,再进行查找实现方法:1 循环实现 2 递归实现1 循环实现#include<stdio.h>int binary_search(int *array,int n,int data);int main(){int array[10]={1,2,3,4,5,6,7,8,9,10};printf("please input the number that you want search\n");int num;scanf("%d",&num);int resul
阅读全文
摘要:#include<stdio.h>#include<stdlib.h>typedef struct list{int data;struct list*next;}List;List*hebin(List*head1,List*head2){List*head,*rear;if(head1==NULL)return head2;if(head2==NULL)return head1;if(head1->data<head2->data){rear=head=head1;head1=head1->next;}else{ rear=head=head
阅读全文
摘要:#include<stdlib.h>#include<stdio.h>typedef struct list{int data;struct list*next;} List;List* insert_list_last();List*reverse(List*head);int main(){List*head;List*p;head=insert_list_last();p=head->next;while(p){printf("%d\n",p->data);p=p->next;}printf("affter reve
阅读全文
摘要:归并排序的前提是:归并前两个数组是有序的。归并排序的思路是先分成两半使用归并排序,然后比较大小,从小到大复制到一个零时数组中去;如果比较后,一方有剩余,那么将剩下的复制到临时数组,最后将排序好的数组拷贝回原数组。归并排序的时间复杂度是:nlogn#include<stdio.h>#include<stdlib.h>#define N 1000000int array[N];int temp[N];void init_array(int a[],int n);void print_array(int a[],int n);void guibing_sort(int a[]
阅读全文
摘要:这种快速排序的思路是:首先以a[start]为轴,不停得从数组的两端开始比较。从最右端开始,如果有比a[start]小的,那么赋值a[i];从最左端开始,如果有比a[start]大的,那么赋值a[j],以此往复,渐渐有序;注意的是递归跳出的条件,只有一个数就不需要排序,即start>=end#include<stdio.h>#include<stdlib.h>#define N 1000000int array[N];void init_array(int a[],int n);void print_array(int a[],int n);void quick_
阅读全文
摘要:快速排序的思路是:首先拿a[start]作为轴,将原数组中比a[start]小的放small数组,将原数组中比a[start]大的放big数组,最后在将small数组 和a[start]值和big数组中的数复制回原数组。以此递归,使数组逐渐有序。快速排序的平均时间复杂度是nlogn。#include<stdio.h>#include<stdlib.h>#define N 1000000int array[N];int small[N];int big[N];void init_array(int a[],int n);void print_array(int a[],i
阅读全文
摘要:插入排序的思路是:新插入的数与比它前面的数进行比较,如果新插入的数比它前面的数小,那么比它前面的数后移;否则,就找到了新插入的数的位置插入排序的时间复杂度是:O(n^2)#include<stdio.h>#include<stdlib.h>#define N 100int array[N];void init_array(int a[],int n);void print_array(int a[],int n);void insert_sort(int a[],int n);int main(){init_array(array,N);insert_sort(arra
阅读全文
摘要:选择排序的思路是:先假定一个数十最小的,后面的数与之比较,如果比它小,那么记录下标,替换最小值;依次往复,则排序完毕选择排序的时间复杂度是o(n^2)#include<stdio.h>#include<stdlib.h>#define N 100int buffer[N];void init_array(int a[],int n);void print_array(int a[],int n);void select_array(int a[],int n);int main(){init_array(buffer,N);printf("before sor
阅读全文
摘要:冒泡排序的思路是将最大的数或者最小的数移到最右端或者最左端第一层for循环的意思是一共比较多少趟,第二层for循环的意思是每次需要比较多少次冒泡排序的时间复杂度是o(n^2)#include<stdio.h>#include<stdlib.h>#define N 100int table[N];void init_array(int array[],int n);void print_array(int array[],int n);void bubble_sort(int array[],int n);int main(){init_array(table,N);pr
阅读全文
摘要:#include<stdio.h>#include<stdlib.h>typedef struct list{int data;struct list*left;struct list*right;}List;void insert_list_2nd(List*head,int data);void insert_list_last(List*head,int data);void insert_list_order(List*head,int data);void print_list(List*head);void delete_list(List*head,int
阅读全文
摘要:#include<stdio.h>#include<stdlib.h>typedef struct list{int data;struct list*next;}List;List*insert_list_2nd(List*head,int data);List*insert_list_last(List*head,int data);List*insert_list_order(List*head,int data);void print_list(List*head);List*delete_list(List*head,int value);int main()
阅读全文
摘要://链表有序插入和删除最重要的是预判,就是判断下一个是否满足要求,因为如果只是判断当前,那么当你找到要操作的节点时,已经过了指向该节点的指针//删除的时候注意释放空间#include<stdio.h>#include<stdlib.h>typedef struct list{int data;struct list*next;}List;void insert_list_2nd(List*head,int data);//表头插入 void insert_list_last(List*head,int data);//表尾插入 void insert_list_orde
阅读全文
摘要://以让数组得到充分的利用,所以采用循环数组//队列为空,则front==rear 如果队列满了,(rear+1)%N=front 数组中最后一个元素未利用 是为了区分空和满的情况 #include<stdio.h>#include<stdlib.h>#define N 100typedef struct queue{int store[N];int front;int rear;}Queue;void init_queue(Queue*q) {q->front=q->rear=0;}void inqueue(Queue*q,int data){if((q-
阅读全文
摘要:插入队列时,注意判断队列是否为空出队列时,注意判断:1 队列是否为空 2 是否是最后一个元素出队列 3 正常出队列#include<stdio.h>#include<stdlib.h>typedef struct list{int data;struct list*next;}List;typedef struct queue{List*front;List*rear;}Queue;void init_queue(Queue*q){q->front=q->rear=NULL;}void inqueue(Queue *q,int data){List*newn
阅读全文
摘要:#include<stdio.h>#include<stdlib.h>typedef struct list{int data;struct list*next;}List;typedef struct stack{List*top;}Stack;Stack*A;Stack*B;void init_stack(Stack*s){s->top=NULL;}void push_stack(Stack*s,int data){List*newnode=(List*)malloc(sizeof(List));newnode->data=data;newnode-&g
阅读全文
摘要:#include<stdio.h>#include<stdlib.h>#define N 100typedef struct stack{char store[N];int top;}Stack;void init_stack(Stack *s);void push_stack(Stack *s,char data);char pop_stack(Stack *s);int match(char x, char y);int main(){char *table;int flag=1;Stack*s=(Stack*)malloc(sizeof(Stack));init_
阅读全文
摘要:#include<stdio.h>#include<stdlib.h>struct node{int data;struct node*next;};struct Binode{int data;struct Binode*next;struct Binode*prev;};struct Binode*func(struct node*head);int main(){//新建衣蛾单链表struct node*head=NULL;struct node*temp;struct node*newnode;struct Binode*Bihead=NULL;struct B
阅读全文
摘要:链表实现栈#include<stdio.h>#include<stdlib.h>typedef struct list{int data;struct list *next;}List;typedef struct stack{List*top;}Stack;void init_stack(Stack*s){s->top=NULL;}void push_stack(Stack *s,int data){List *newnode=(List*)malloc(sizeof(List));newnode->data=data;newnode->next=s
阅读全文