Chinaunix首页 | 论坛 | 博客
  • 博客访问: 367735
  • 博文数量: 62
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 557
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-01 14:04
文章分类

全部博文(62)

文章存档

2014年(1)

2013年(61)

分类: C/C++

2013-10-14 14:48:46

原文地址:排序_查找 _0310 作者:丫叩酱


点击(此处)折叠或打开

  1. //my_strchr

  2. #include <stdio.h>
  3. #include <stdlib.h>

  4. char *my_strchr(char *s, int c)
  5. {
  6.     int i;

  7.     for(i = 0; s[i]; i++)
  8.     {
  9.         if(s[i] == c)
  10.             return s + i;
  11.     }

  12.     return NULL;
  13. }


  14. int main(int argc, char *argv[])
  15. {
  16.     /*char ch;

  17.     printf("ch =");
  18.     scanf("%c", &ch);

  19.     printf("%s\n", my_strchr(argv[1], ch));
  20.     */
  21.     printf("%s\n", my_strchr(argv[1], atoi(argv[2])));
  22.     return 0;
  23. }

点击(此处)折叠或打开

  1. //rand.c

  2. #include <stdio.h>
  3. #include<stdlib.h>
  4. #include <time.h>

  5. #define NUM 10

  6. typedef struct
  7. {
  8.     int data;
  9.     char *str;
  10. }data_t;


  11. char *get_string(char *str, int num)
  12. {
  13.     int i;

  14.     for(i = 0; i < num - 1; i++)
  15.     {
  16.         str[i] = rand()%26 + 'a' - (rand()%2) * 32;
  17.     }

  18.     str[num -1] = 0;

  19.     return str;
  20. }

  21. void init_struct(data_t *arr_struct, int num)
  22. {
  23.    int i, j;

  24.    srand(time(NULL));
  25.    //双层for循环,要在最里层得到一个随机数时,必须将srand放在双层for循环的外面
  26.    //若放在第一层for循环的里面,将得到第一层循环数的个数个相同的的随机数
  27.    for(i = 0; i < num; i++)
  28.    {
  29.         arr_struct[i].data = rand()%100 + 1;

  30.         arr_struct[i].str = malloc(NUM);

  31.         arr_struct[i].str = get_string(arr_struct[i].str, NUM);
  32.         /*
  33.         for(j = 0; j < NUM -1; j ++)
  34.         {
  35.             arr_struct[i].str[j] = rand()%26 + 'a';
  36.         }
  37.         arr_struct[i].str[NUM-1] = 0; */
  38.    }
  39. }

  40. void show_struct(data_t *arr_struct, int num)
  41. {
  42.     int i;
  43.     for(i = 0; i < num; i++)
  44.     {
  45.         printf("--%d--%s--\n", arr_struct[i].data, arr_struct[i].str);
  46.     }
  47. }

  48. int main(int argc, const char *argv[])
  49. {
  50.     data_t arr_struct[NUM];


  51.     init_struct(arr_struct, NUM);
  52.     show_struct(arr_struct, NUM);
  53.     return 0;
  54. }

点击(此处)折叠或打开

  1. //range.c

  2. //冒泡排序
  3. //插入排序
  4. //快速排序
  5. //以结构体的int类型成员排序
  6. //以结构体的char类型成员排序
  7. //以结构体的char *类型成员排序

  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include <time.h>

  12. #define NUM 5

  13. typedef struct
  14. {
  15.     int data;
  16.     char ch;
  17.     char str[10];
  18. }data_t;


  19. void get_char(char *ch)
  20. {
  21.     *ch = rand() % 26 + 'a';
  22. }

  23. void get_string(char *str, int size)
  24. {
  25.     int i;

  26.     for(i = 0; i < size - 1; i++)
  27.     {
  28.        str[i] = rand() % 26 + 'a' ;
  29.     }
  30.     str[size- 1] = 0;

  31. }

  32. void init_struct(data_t *arr_struct, int num)
  33. {
  34.     int i;

  35.     srand(time(NULL));
  36.     for(i = 0; i < num; i++)
  37.     {
  38.        arr_struct[i].data = rand() % 100 + 1;

  39.        get_char(&arr_struct[i].ch);

  40.        get_string(arr_struct[i].str, num);
  41.     }
  42.     
  43. }

  44. void bubble_sort(int *array, int num)
  45. {
  46.     int i, j, tmp;

  47.     for(i = 0; i < num -1; i++)
  48.     {
  49.         for(j = i + 1; j < num; j++)
  50.         {
  51.             if(array[i] > array[j])
  52.             {
  53.                tmp = array[i];
  54.                array[i] = array[j];
  55.                array[j] = tmp;
  56.             }
  57.         }
  58.     }
  59. }

  60. void insert_sort(int *array, int num)
  61. {
  62.     int i, j, save;

  63.     for(i = 1; i < num; i++)
  64.     {
  65.        save = array[i];
  66.        for(j = i - 1; j >= 0 && array[j] > save; j--)
  67.        {
  68.             array[j+1] = array[j];
  69.        }

  70.        array[j+1] = save;
  71.     }
  72. }


  73. int compare_int(const void *first, const void *second)
  74. {
  75.     return *((int *)first) - *((int *)second);
  76. }

  77. int compare_struct_int(const void *f, const void *s)
  78. {
  79.     return ((data_t *)f)->data - ((data_t *)s)->data;
  80. }

  81. int compare_struct_ch(const void *f, const void *s)
  82. {
  83.     return ((data_t *)f)->ch - ((data_t *)s)->ch;
  84. }

  85. int compare_struct_str(const void *f, const void *s)
  86. {
  87.     return strcmp(((data_t *)f)->str, ((data_t *)s)->str);
  88. }

  89. void show(int *array, int num)
  90. {
  91.     int i;

  92.     printf("array:");
  93.     for(i = 0; i < NUM; i++)
  94.     {
  95.         printf("%4d", array[i]);
  96.     }
  97.     putchar('\n');
  98. }

  99. void show_struct(data_t *arr_struct, int num)
  100. {
  101.    int i;

  102.    printf("struct: \n") ;
  103.    for(i = 0; i < num; i++)
  104.    {
  105.         printf("--%02d--%c--%s--\n", arr_struct[i].data, arr_struct[i].ch, arr_struct[i].str);
  106.    }
  107. }

  108. int main(int argc, const char *argv[])
  109. {
  110.     int i;
  111.     int array[NUM];

  112.     srand(time(NULL));
  113.     for(i = 0; i < NUM; i++)
  114.     {
  115.         array[i] = rand() % 100 + 1;
  116.     }
  117.     show(array, NUM);

  118.     bubble_sort(array, NUM);
  119.     printf("bubble sort: ");
  120.     show(array, NUM);

  121.     insert_sort(array, NUM);
  122.     printf("insert sort: ");
  123.     show(array, NUM);

  124.     qsort(array, NUM, sizeof(int), compare_int);
  125.     printf("qsort sort: ");
  126.     show(array, NUM);

  127.     data_t arr_struct[NUM];

  128.     init_struct(arr_struct, NUM);
  129.     show_struct(arr_struct, NUM);

  130.     qsort(arr_struct, NUM, sizeof(data_t), compare_struct_int);
  131.     printf("struct sort by int:\n");
  132.     show_struct(arr_struct, NUM);


  133.     qsort(arr_struct, NUM, sizeof(data_t), compare_struct_ch);
  134.     printf("struct sort by char:\n");
  135.     show_struct(arr_struct, NUM);

  136.     qsort(arr_struct, NUM, sizeof(data_t), compare_struct_str);
  137.     printf("struct sort by string:\n");
  138.     show_struct(arr_struct, NUM);
  139.     return 0;
  140. }

点击(此处)折叠或打开

  1. //range_1.c

  2. //2分法排序

  3. #include <stdio.h>
  4. #define NUM 5

  5. void init(int *arr, int num)
  6. {
  7.     int i;

  8.     for(i = 0; i < num; i++)
  9.     {
  10.         arr[i] = i + 1;
  11.     }
  12. }


  13. int search(int *arr, int num, int search_num)
  14. {
  15.     int start = 0, end = num - 1, middle;

  16.     while(start <= end)
  17.     {
  18.         middle = (start + end) /2;

  19.         if(arr[middle] < search_num)
  20.         {
  21.            start = middle + 1;
  22.         }
  23.         else
  24.             if(arr[middle] > search_num)
  25.                 end = middle - 1;
  26.             else
  27.                 return middle;
  28.     }

  29.     printf("search_num not exit\n");
  30.     return -1;
  31. }

  32. int main(int argc, const char *argv[])
  33. {
  34.     int arr[NUM], n;

  35.     init(arr, NUM);
  36.     
  37.     n = search(arr, NUM, 3);

  38.     if(n != -1)
  39.         printf("arr[%d] = %d\n", n, arr[n]);



  40.     return 0;
  41. }

阅读(1917) | 评论(0) | 转发(0) |
0

上一篇:队列 _0311

下一篇:mmap()系统调用

给主人留下些什么吧!~~