作业






快速排序
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int one_sort(int a[],int low,int high);
void insert(int a[],int low,int high);
void Out(int a[],int len);
int main(int argc,const char * argv[])
{
int a[]={9,8,7,6,5,4,3,2,1};
int len=sizeof(a)/sizeof(a[0]);
insert(a,0,len-1);
Out(a,len);
return 0;
}
void Out(int a[],int len)
{
for(int i=0;i<len;i++)
{
printf("%d\t",a[i]);
}
}
int one_sort(int a[],int low,int high)
{
int key=a[low];
while(low<high)
{
while(low<high && key<=a[high])
{
high--;
}
a[low]=a[high];
while(low<high && key>=a[low])
{
low++;
}
a[high]=a[low];
}
a[low]=key;
return low;
}
void insert(int a[],int low,int high)
{
if(low<high)
{
int mid=one_sort(a,low,high);
one_sort(a,low,mid-1);
one_sort(a,mid+1,high);
}
}
直接排序
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int insert(int *p,int n);
int main(int argc,const char * argv[])
{
int a[]={9,8,7,6,5,4,3,2,1};
int n=sizeof(a)/sizeof(a[0]);
int *p=a;
insert(p,n);
for(int i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
return 0;
}
int insert(int *p,int n)
{
int j=0;
for(int i=1;i<n;i++)
{
int t=*(p+i);
for(j=i-1;j>=0 && t<*(p+j);j--)
{
*(p+j+1)=*(p+j);
}
*(p+j+1)=t;
}
return 0;
}
二叉数的插入遍历
main.c
#include "head.h"
int main(int argc,const char * argv[])
{
Btree T=create();
printf("\n先序遍历的结果是:");
first(T);
printf("\n中序遍历的结果:");
mid(T);
printf("\n后续遍历的结果是:");
last(T);
return 0;
}
test.c
#include "head.h"
Btree create()
{
datetype e;
printf("请输入您要的数据:");
scanf(" %c",&e);
if(e=='#')
return NULL;
Btree T=(Btree)malloc(sizeof(struct Node));
if(T==NULL)
return NULL;
T->date=e;
puts("左子树");
T->left=create();
puts("右子树");
T->right=create();
return T;
}
void first(Btree T)
{
if(T==NULL)
return ;
printf("%c\t",T->date);//根
first(T->left);//左
first(T->right);//右
}
void mid(Btree T)
{
if(T==NULL)
return ;
mid(T->left);//左
printf("%c\t",T->date);//根
mid(T->right);//右
}
void last(Btree T)
{
if(T==NULL)
return ;
last(T->left);//左
last(T->right);//右
printf("%c\t",T->date);//根
}
head.h
#ifndef __HEAD_H__
#define __HEAD_H__
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef char datetype;
typedef struct Node
{
datetype date;
struct Node *left;
struct Node *right;
}*Btree;
Btree create();
#include "test.c"
#endif