树的创建,遍历与插入;快速排序,直接插入排序

head.h

#ifndef __HEAD_H__
#define __HEAD_H__

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef char datatype;
typedef struct Node{
    //数据域
    datatype data;
    //指针域
    struct Node *left;
    struct Node *right;
}*BTree;

BTree creat();
void first(BTree T);
void mid(BTree T);
void last(BTree T);
#endif

main.c

#include"head.h"
int main(int argc,const char *argv[])
{
    BTree T=creat();
    printf("先序遍历:");
    first(T);
    printf("\n");
    printf("中序遍历:");
    mid(T);
    printf("\n");
    printf("后序遍历:");
    last(T);
    printf("\n");
    return 0;
}

txt.c

#include"head.h"

/*
 *function :  树的创建并输入
 *@param [in]:  无参数
 *@param [out]:
 *@return :  成功返回首地址,失败返回NULL
 */
BTree creat(){
    datatype e;
    printf("请输入一个字符:");
    scanf("%c",&e);
    getchar();
    if(e=='#')
        return NULL;
    BTree T=(BTree)malloc(sizeof(struct Node));
    if(T==NULL)
        return NULL;
    T->data=e;
    T->left=creat();
    T->right=creat();
    return T;
}

/*
 *function :  先序遍历
 *@param [in]:  树
 *@param [out]:
 *@return :  无返回值
 */
void first(BTree T){
    if(T==NULL)
        return ;
    printf("%c\t",T->data);
    first(T->left);
    first(T->right);
}
/*
 *function :  中序遍历
 *@param [in]:  树
 *@param [out]:
 *@return :  无返回值
 */
void mid(BTree T){
    if(T==NULL)
        return ;
    mid(T->left);
    printf("%c\t",T->data);
    mid(T->right);
}
/*
 *function :  后序遍历
 *@param [in]:  树
 *@param [out]:
 *@return :  无返回值
 */
void last(BTree T){
    if(T==NULL)
        return ;
    last(T->left);
    last(T->right);
    printf("%c\t",T->data);
}

快速排序和直接插入排序

head.h

#ifndef __HEAD_H__
#define __HEAD_H__

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void Output(int *p,int n);
void direct_insert_sort(int *p,int n);
int one_sort(int *p,int low,int high);
void quick_sort(int *p,int low,int high);
void Bubble(int *p,int n);
void easy_sort(int *p,int n);

#endif

main.c

#include "head.h"
int main(int argc,const char *argv[])
{
    int arr[]={4,2,6,4,9,1,6,5};
    int len=sizeof(arr)/sizeof(arr[0]);
    Output(arr,len);
//    direct_insert_sort(arr,len);
//    quick_sort(arr,0,len-1);
//    Bubble(arr,len);
    easy_sort(arr,len);
    Output(arr,len);
    return 0;
}

txt.c

#include "head.h"

/*
 *function :  遍历数组
 *@param [in]:  数组,数组长度
 *@param [out]:
 *@return :  无返回值
 */
void Output(int *p,int n){
    for(int i=0;i<n;i++){
        printf("%d\t",*(p+i));
    }
    printf("\n");
}

/*
 *function :  直接插入排序,升序
 *@param [in]:  数组,数组长度
 *@param [out]:
 *@return :  无返回值
 */
void direct_insert_sort(int *p,int n){
    int j;
    for(int i=1;i<n;i++){
        int temp=*(p+i);
        for(j=i-1;j>=0&&temp<*(p+j);j--){
            *(p+j+1)=*(p+j);
        }
        *(p+j+1)=temp;
    }
}

/*
 *function :  快排的一次排序
 *@param [in]:  数组,最小下标,最大下标
 *@param [out]:
 *@return :  返回基准值第下标
 */
int one_sort(int *p,int low,int high){
    int key=*(p+low);//保存基准值
    while(low<high){
        while(low<high&&key<=*(p+high)){
            high--;
        }
        *(p+low)=*(p+high);
        while(low<high&&key>=*(p+low)){
            low++;
        }
        *(p+high)=*(p+low);
    }
    *(p+low)=key;
    return low;
}

/*
 *function :  快速排序,升序
 *@param [in]:  数组,最小下标,最大下标
 *@param [out]:
 *@return :  无返回值
 */
void quick_sort(int *p,int low,int high){
    if(low<high){
        int mid=one_sort(p,low,high);
        //递归左边
        quick_sort(p,low,mid-1);
        //递归右边
        quick_sort(p,mid+1,high);
    }
}

/*
 *function :  冒泡排序
 *@param [in]:  数组,数组长度
 *@param [out]:
 *@return :  无返回值
 */
void Bubble(int *p,int n){
    for(int i=1;i<n;i++){
        int count=0;
        for(int j=0;j<n-i;j++){
            if(*(p+j)>*(p+j+1)){
                int temp=*(p+j);
                *(p+j)=*(p+j+1);
                *(p+j+1)=temp;
                count++;
            }
        }
        if(count==0)
            break;
    }
}

/*
 *function :  简单选择排序
 *@param [in]:  数组,数组长度
 *@param [out]:
 *@return :  无返回值
 */
void easy_sort(int *p,int n){
    for(int i=0;i<n-1;i++){
        int mini=i;
        for(int j=i+1;j<n;j++){
            if(*(p+j)<*(p+mini)){
                mini=j;
            }
        }
        if(mini!=i){
            int temp=*(p+mini);
            *(p+mini)=*(p+i);
            *(p+i)=temp;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值