From f5895071c6c225b13f435d97800cd5c554c28b79 Mon Sep 17 00:00:00 2001 From: jinshaohui Date: Fri, 19 Oct 2018 14:12:51 +0800 Subject: [PATCH] add by j00322883 for sort --- c-cpp/10_recursive/one_two_step.c | 57 ++++++++++++++ c-cpp/11_sorts/sorts_jinshaohui.c | 124 ++++++++++++++++++++++++++++++ 2 files changed, 181 insertions(+) create mode 100644 c-cpp/10_recursive/one_two_step.c create mode 100644 c-cpp/11_sorts/sorts_jinshaohui.c diff --git a/c-cpp/10_recursive/one_two_step.c b/c-cpp/10_recursive/one_two_step.c new file mode 100644 index 0000000..02d503e --- /dev/null +++ b/c-cpp/10_recursive/one_two_step.c @@ -0,0 +1,57 @@ +/************************************************************************* + > File Name: one_two_step.c + > Author: jinshaohui + > Mail: jinshaohui789@163.com + > Time: 18-10-19 + > Desc: + ************************************************************************/ +#include +#include +#include + +/*爬楼梯的问题,解决重复计算,采用数据保存方法*/ + +int helper(int n ,int *vlaue) +{ + + if(vlaue[n] != 0) + { + return vlaue[n]; + } + + vlaue[n] = helper(n - 1,vlaue) + helper(n - 2,vlaue); + + return vlaue[n]; +} + +int climbStaris(int n) +{ + int *vlaue = NULL; + int res = 0; + + vlaue = (int *)malloc(sizeof(int)*(n+1)); + if(vlaue == NULL) + { + return -1; + } + + memset(vlaue,0,sizeof(int)*(n + 1)); + vlaue[0] = 0; + vlaue[1] = 1; + vlaue[2] = 2; + res = helper(n,vlaue); + free(vlaue); + + return res; +} + +int main() +{ + + printf("\r\nnum%d ,%d",5,climbStaris(5)); + printf("\r\nnum%d ,%d",6,climbStaris(6)); + printf("\r\nnum%d ,%d",7,climbStaris(7)); + return 0; +} + + diff --git a/c-cpp/11_sorts/sorts_jinshaohui.c b/c-cpp/11_sorts/sorts_jinshaohui.c new file mode 100644 index 0000000..9e5675f --- /dev/null +++ b/c-cpp/11_sorts/sorts_jinshaohui.c @@ -0,0 +1,124 @@ +/************************************************************************* + > File Name: sorts_jinshaohui.c + > Author: jinshaohui + > Mail: jinshaohui789@163.com + > Time: 18-10-19 + > Desc: + ************************************************************************/ +#include +#include +#include + +#define SWAP(a,b) \ +do{\ + (a) ^= (b);\ + (b) ^= (a);\ + (a) ^= (b);\ +}while(0) + +/*冒泡排序*/ +void bubble_sort(int a[],int size) +{ + int i = 0; + int j = 0; + int swap_flg = 0; + + if (size < 1) + { + return; + } + + for (i = size - 1; i > 0; i--)/*排序的趟数*/ + { + swap_flg = 0;/*每次设置交换标识为0*/ + for (j = 0; j < i; j++)/*本趟排序的遍历元素个数*/ + { + if (a[j] > a[j + 1]) + { + SWAP(a[j],a[j+1]); + swap_flg = 1; + } + } + /*本趟数,无数据交换的话,说明已经有序,直接退出*/ + if (swap_flg == 0) + { + break; + } + } + return; +} + +/*插入排序*/ +void insert_sort(int a[],int size) +{ + int i = 0; + int j = 0; + int key = 0; + + for (i = 1; i < size; i ++)/*需要插入的元素个数*/ + { + key = a[i];/*保存插入的元素数据*/ + j = i - 1; + /* i 之前的元素都是有序的,找到比key小的插入到他后面, + * 比key大的,需要往后挪一个位置*/ + while((j >= 0) && (a[j] > key)) + { + a[j + 1] = a[j]; + j--; + } + a[j + 1] = key; + } + + return; +} +/*选择排序*/ +void select_sort(int a[],int size) +{ + int i = 0; + int j = 0; + int min = 0; + + for (i = 0; i < size - 1; i++) + { + min = i; + for (j = i + 1; j < size; j++) + { + if (a[j] < a[min]) + { + min = j; + } + } + + if (min != i) + { + SWAP(a[i],a[min]); + } + } + return; +} + +void dump(int a[],int size) +{ + int i = 0; + + printf("\r\n"); + for (i = 0; i < size; i++ ) + { + printf("%d ",a[i]); + } + printf("\r\n"); + return; +} + +int main() +{ + int a[10] = {9,11,4,15,16,3,20,44,5,10}; + + //bubble_sort(a,sizeof(a)/sizeof(int)); + //insert_sort(a,sizeof(a)/sizeof(int)); + select_sort(a,sizeof(a)/sizeof(int)); + + dump(a,sizeof(a)/sizeof(int)); + + return 0; +}