Chinaunix首页 | 论坛 | 博客
  • 博客访问: 68352
  • 博文数量: 115
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 10
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-08 19:09
文章分类
文章存档

2015年(115)

我的朋友

分类: C/C++

2015-08-06 16:49:33

找出数组中第k大/小的数.
这个题是常见题,但是一直都没有动手写过。今天实现了一下。顺道写了下快排。说是很简单的代码,写起来也不容易。
最好的算法是基于快排的,复杂度大概为n。道理不解释。代码如下:

点击(此处)折叠或打开

  1. /*
  2.  * =====================================================================================
  3.  *
  4.  * Filename: quicksort.c
  5.  *
  6.  * Description:
  7.  *
  8.  * Version: 1.0
  9.  * Created: 10/10/2012 01:09:51 PM
  10.  * Revision: none
  11.  * Compiler: gcc
  12.  *
  13.  * Author: YOUR NAME (),
  14.  * Organization:
  15.  *
  16.  * =====================================================================================
  17.  */
  18. #include <stdlib.h>
  19. #include <stdio.h>


  20. #include <unistd.h>

  21. #define swap(a,b) a^=b;b^=a;a^=b;

  22. int* sort1round(int * nums, int size){
  23.     printf ( "start sort!! %d\n",size );
  24.     if(size<=1) return nums;
  25.     int *tail = nums+size-1;
  26.     int *head = nums;
  27.     printf ( "key = %d\n", *head );
  28.     while(1){
  29.         while(*tail > *head && tail!=head)tail--;
  30.         if(tail == head) break;
  31.         if(*tail<=*head){
  32.             //swap tail and key
  33.             printf ( "1. swap %d %d\n", *tail, *head );
  34.             swap(*tail, *head);
  35.             head++;
  36.             //now tail is the key
  37.         }
  38.         while(*tail > *head) head++;
  39.         if(tail == head) break;
  40.         if(*tail<*head && tail!=head){
  41.             printf ( "2. swap %d %d\n", *tail, *head );
  42.             swap(*tail, *head);
  43.             tail--;
  44.         }
  45.     }
  46.     printf ( "end sort with key = %d\n", *head );
  47.     return head;
  48. }

  49. void quicksort(int *nums, int size){
  50.     if(size<=1)return;
  51.     int *key = sort1round(nums,size);
  52.     quicksort(nums,key-nums);
  53.     quicksort(key+1,nums+size-1-key);
  54. }
  55. int getk(int *nums,int size, int k){
  56.     if(size<=1) return *nums;
  57.     int *key = sort1round(nums,size);
  58.     if(key-nums == k-1){
  59.         return *key;
  60.     }

  61.     if(key-nums > k-1){
  62.         return getk(nums,key-nums,k);
  63.     }
  64.     if(key-nums < k-1){
  65.         return getk(key+1, nums+size-key-1, k - (key-nums+1));
  66.     }
  67.     return -1000;
  68. }


  69. /*
  70.  * === FUNCTION ======================================================================
  71.  * Name: main
  72.  * Description:
  73.  * =====================================================================================
  74.  */
  75.     int
  76. main ( int argc, char *argv[] )
  77. {
  78.     int input[] = {2,3,6,-2,4,5};
  79. //    quicksort(input, sizeof(input)/4);
  80.     int i;
  81.     for(i=0;i<sizeof(input)/4;i++){
  82.         printf ( "%d\n", input[i] );
  83.     }
  84.     printf ( "get k = 4 %d\n", getk(input, sizeof(input)/4,2 ));
  85.     return EXIT_SUCCESS;
  86. }                /* ---------- end of function main ---------- */

阅读(154) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~