/*
    本文是选择问题:

选择一组N个数当中的第k小的数(第k大的数类似)
     集中方法的实现代码

*/
 
 
 
#include "sorting.h"
#include "fatal.h"
 
#define SORTING_BUBBLE  1
#define SORTING_INSERTION   2
#define SORTING_SELECTION   3
#define SORTING_SHELL       4
#define SORTING_QUICK       5
#define SORTING_HEAP        6
#define SORTING_MERGE       7
 
 
/*
    解法1: 我们可以对这个乱序数组按照从小到大先行排序,然后

取出前k大,总的时间复杂度为O(n*logn + k)。

*/
 
int select_by_sorting(int A[], int N, int k, int SortingMethod)
{
     || k > N )
    {
        fprintf (stderr, "error, k ??????\n");
        exit (EXIT_FAILURE);
    }
    switch(SortingMethod)
    {
    case SORTING_BUBBLE :
        bubble_sort (A, N,IntComp);
         ];
    case SORTING_INSERTION :
        insertion_sort (A, N,IntComp);
         ];
    case SORTING_SELECTION :
        selection_sort (A, N,IntComp);
         ];
    case SORTING_SHELL :
        shell_sort (A, N,IntComp);
         ];
    case SORTING_QUICK :
        quick_sort (A, N,IntComp);
         ];
    case SORTING_HEAP :
        heap_sort (A, N,IntComp);
         ];
    case SORTING_MERGE :
        merge_sort (A, N,IntComp);
         ];
    default:
        Error ("not a known sorting method!");
    }
    ;
}
/*
    解法2: 先把前k个元素读进数组并排序(递增顺序),接着,将剩下

的元素逐个读入。当新元素大于数组中的第k个元素是则忽略,否则将
     其放入正确的位置,旧的第k个元素将被挤掉!

*/
int select2(int A[], int N, int k)
{
    // 可改进为前面k个数原地排序。
    int *Ak = malloc(sizeof(int )*k);
    Ak ];
    int i,;
    ; i < k; i++)
    {
          ; j--)
         {
             if([i]< Ak[j])
                Ak ] = Ak[ j];
             else
                 break;
         }
        Ak ] = A[ i];
    }
    for(= k ; i < N; ++)
    {
         ]<= A [i]) continue;
        
         ; --j)
         {
             if([i] < Ak[])
                Ak ] = Ak[ j];
             else
                 break;
         }
        Ak ] = A[ i];
    }
    ];
    free(Ak );   
    return ret ;
}
 
/*
    解法3:利用选择排序或交互排序,K次选择后

即可得到第k大的数。总的时间复杂度为O(n*k)

*/
 
int select3(int A[], int N, int k)
{
    int minIndex ;
    int tmp;
    int i,;
    ; i <k; ++i)
    {
        minIndex = i;
         ; j <N; ++j)
             if([minIndex] > A [j])
                minIndex = j;
        tmp = A[ minIndex];
        A [minIndex] = A [i];
        A [i] = tmp;
    }
    ];
}
 
/*
    解法4:利用快速排序的思想,从数组S中随机找出一个元素X,

把数组分为两部分Sa和Sb。Sa中的元素大于等于X,Sb中元素
     小于X。这时有两种情况:
          1. Sa中元素的个数小于k,则Sb中的第k-|Sa|个元素即为第k大数;
          2. Sa中元素的个数大于等于k,则返回Sa中的第k大数。时间复杂度近似为O(n)

 
*/
int partition(int A[], int p, int q)
{
    // select a pivot
    // for simplicity select p as pivot
    int i,;
    i = p ;
    int tmp;
    ; j <= q ; j++)    
    {
         if([j] < A[])
         {
             ++i;
             if(== j)
                 continue;
            tmp = A[ i];
            A [i] = A[];
            A [j] = tmp;
         }
    }
    tmp = A [i];
    A[] = A[p];
    A[] = tmp;
    return i ;
}
int findk( int A[], int p, int q, int k)
{
    int r = partition (A, p,q);
     == r)
         return A[ r];
    )
    {
         , k);
    }else
          ,q, k);
}
int select4(int A[], int N, int k)
{
    , k);
}
 
 
#define ITEMNUM 5000
#define METHODNUM 10
int main()
{
    int A[METHODNUM ][ITEMNUM];
    ;
    int temp;
     ; i < ITEMNUM; ++i)
    {
        temp = rand();
         ; j < METHODNUM; j++)
            A [j][ i] = temp ;
    }
    
    int r1,r2 ,r3, r4,r5,r6 ,r7, r8;
    r1 ],ITEMNUM ,k, SORTING_BUBBLE);
    r2 ],ITEMNUM ,k, SORTING_INSERTION);
    r3 ],ITEMNUM ,k, SORTING_SELECTION);
    r4 );
    r5 );
    r6 );
    r7);
    r8 ],ITEMNUM ,k);
    ],ITEMNUM ,k);
    printf("%d\n%d\n%d\n%d\n%d\n%d\n%d\n%d\n" ,r1, r2,r3,r4 ,r5, r6,r7,r8 );
    printf("%d\n" ,r9);
    ],ITEMNUM ,k);
    printf("%d\n" ,r10);
    ;
}

选择问题(selection problem)的更多相关文章

  1. 选择屏幕(Selection Screen)

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  2. 排序算法 - 选择排序(selection sort)

    选择排序(Selection sort)跟插入排序一样,也是O(n^2)的复杂度,这个排序方式也可以用我们的扑克牌来解释. 概念 桌面上有一堆牌,也是杂乱无章的,现在我们想将牌由小到大排序,如果使用选 ...

  3. 简单选择排序 Selection Sort 和树形选择排序 Tree Selection Sort

    选择排序 Selection Sort 选择排序的基本思想是:每一趟在剩余未排序的若干记录中选取关键字最小的(也可以是最大的,本文中均考虑排升序)记录作为有序序列中下一个记录. 如第i趟选择排序就是在 ...

  4. ISLR系列:(4.1)模型选择 Subset Selection

    Linear Model Selection and Regularization 此博文是 An Introduction to Statistical Learning with Applicat ...

  5. 排序算法--选择排序(Selection Sort)_C#程序实现

    排序算法--选择排序(Selection Sort)_C#程序实现 排序(Sort)是计算机程序设计中的一种重要操作,也是日常生活中经常遇到的问题.例如,字典中的单词是以字母的顺序排列,否则,使用起来 ...

  6. 选择排序 Selection Sort

    选择排序 Selection Sort 1)在数组中找最小的数与第一个位置上的数交换: 2)找第二小的数与第二个位置上的数交换: 3)以此类推 template<typename T> / ...

  7. the steps that may be taken to solve a feature selection problem:特征选择的步骤

    參考:JMLR的paper<an introduction to variable and feature selection> we summarize the steps that m ...

  8. 跳跃空间(链表)排序 选择排序(selection sort),插入排序(insertion sort)

    跳跃空间(链表)排序 选择排序(selection sort),插入排序(insertion sort) 选择排序(selection sort) 算法原理:有一筐苹果,先挑出最大的一个放在最后,然后 ...

  9. 【ABAP系列】SAP ABAP选择屏幕(SELECTION SCREEN)事件解析

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP选择屏幕(SEL ...

随机推荐

  1. Spring Boot连接Mysql数据库问题解决

    在spring Boot项目中使用mysql数据库进行数据库的增删查改,出现以下错误: Error starting ApplicationContext. To display the auto-c ...

  2. mac 升级10.12 php debug 环境 跑不起的解决 解决方案

    1:  mac 升级后发现 php从原来的5.5  升级为 5.6 了...   所以以前 php.ini 里面的配置全部都没有了. mac 给我们做了备份2:  没办法只能升级php对应的插件到5. ...

  3. 04 Effective Go 高效的go语言 重点内容

    Effective Go  高效的go语言 Introduction 介绍 Examples 例子 Formatting 格式 Commentary 评论 Names 名字 Package names ...

  4. tftp的安装

    下载并且安装软件xinetd tftp tftpd sudo apt-get install xinetd tftp tftpd 在/etc/xinetd.d/下建立一个配置文件tftp sudo v ...

  5. python面向对象(三)之继承

    继承 介绍 继承是从已有的类中派生出新的类,新的类能吸收已有类的数据属性和行为,并能扩展新的能力.继承即常说的is-a关系.子类继承父类的特征和行为,使得子类具有父类的各种属性和方法.或子类从父类继承 ...

  6. 插入标识列identity_insert

    插入标识列identity_insert 在进行数据插入时,如果插入列名包括标识列,常常会遇到以下3种提示: 一.“当 IDENTITY_INSERT 设置为 OFF 时,不能向表 'xxxxxxxx ...

  7. SQL Server和Access数据读写

    1.查询Access中数据的方法: select * from OpenRowSet('microsoft.jet.oledb.4.0',';database=c:/db2.mdb','select ...

  8. js交互

    Js和native交互的方法与问题 实现JS和Native交互有两种方式: 第一种:shouldOverrideUrlLoading(WebView view, String url) 通过给WebV ...

  9. **IOS自动完成(搜索自动提示)功能实现

    UISearchBar搜索AutoComplete下拉列表搜索提示 http://www.codeios.com/thread-10685-1-1.html 介绍:     在搜索框上加入下拉列表.在 ...

  10. ASP.NET Identity 修改表名和主键类型

    public class UserLogin : IdentityUserLogin<Guid> { } public class UserRole : IdentityUserRole& ...