【算法实现】查找第i大的数

闲来无事,实现了一下随机选择算法,从一堆数中找出第i大的数,主要的几个函数如下:

<=A[r]r>=A[r]

  1. /* 查找第i大的数 */
  2. int rand_select(int *array, int beg_index, int end_index, int select)
  3. {
  4.         if (beg_index == end_index)
  5.                 return array[beg_index];
  6.         int pos = rand_partition(array, beg_index, end_index);
  7.         int rank = pos - beg_index + 1;
  8.         if (rank == select)
  9.                 return array[pos];
  10.         else if (rank < select)
  11.                 return rand_select(array, pos + 1, end_index, select - rank);
  12.         else
  13.                 return rand_select(array, beg_index, pos - 1, select);
  14. }

  15. /* 随机分区 */
  16. int rand_partition(int *array, int beg_index, int end_index)
  17. {
  18.         srand((int)time(NULL));
  19.         int pivot = (rand() % (end_index - beg_index + 1)) + beg_index;
  20.         if (pivot != beg_index) {
  21.                 array_switch(array, pivot, beg_index);
  22.         }
  23.         int i = beg_index;
  24.         int j = i + 1;
  25.         for (; j <= end_index; j++) {
  26.                 if (array[j] < array[beg_index]) {
  27.                         array_switch(array, j, ++i);
  28.                 }
  29.         }
  30.         array_switch(array, i, beg_index);
  31.         return i;
  32. }

  33. /* 交换数 */
  34. void array_switch(int *array, int index1, int index2)
  35. {
  36.         int tmp = array[index1];
  37.         array[index1]= array[index2];
  38.         array[index2] = tmp;
  39. }

[test]
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>

  4. int rand_select(int *array, int beg_index, int end_index, int select);
  5. int rand_partition(int *array, int beg_index, int end_index);
  6. void array_switch(int *array, int index1, int index2);

  7. int main(int argc, char *argv[])
  8. {
  9.         if (argc < 2) {
  10.                 printf("usage: %s <array num> <kth large num>/n", argv[0]);
  11.                 return -1;
  12.         }
  13.         int num = atoi(argv[1]);
  14.         int select = atoi(argv[2]);
  15.         if (select > num) {
  16.                 printf("not enough num, array num[%d], kth large num[%d]/n",
  17.                                 num, select);
  18.                 return -1;
  19.         }
  20.         int *array = (int *)malloc(sizeof(int) * num);
  21.         if (array == NULL) {
  22.                 printf("malloc failed/n");
  23.                 return -1;
  24.         }
  25.         array[0] = 0;
  26.         int i = 0;
  27.         for (; i < num; i++) {
  28.                 scanf("%d", &array[i]);
  29.         }
  30.         printf("array[");
  31.         for (i = 0; i < num; i++) {
  32.                 printf("%d, ", array[i]);
  33.         }
  34.         printf("]/n");
  35.         int pic = rand_select(array, 0, num - 1, select);
  36.         printf("the %dth large num is [%d]/n", select, pic);
  37.         return 0;
  38. }

[result]
./rand_select 6 3
2
1
5
6
9
10
array[2, 1, 5, 6, 9, 10, ]
the 3th large num is [5]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值