Suppose you have an array of N elements, containing three distinct keys, "true", "false", and "maybe". Given an O(N)O(N) algorithm to rearrange the list so that all "false" elements precede "maybe" elements, which in turn precede "true" elements. You may use only constant extra space.

Format of functions:

void MySort( ElementType A[], int N );

where ElementType A[] contains the N elements.

Sample program of judge:

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

typedef enum { true, false, maybe } Keys;
typedef Keys ElementType;

void Read( ElementType A[], int N ); /* details omitted */

void MySort( ElementType A[], int N );

void PrintA( ElementType A[], int N )
{
    int i, k;

    k = i = 0;
    for ( ; i<N && A[i]==false; i++ );
    if ( i > k )
        printf("false in A[%d]-A[%d]\n", k, i-1);
    k = i;
    for ( ; i<N && A[i]==maybe; i++ );
    if ( i > k )
        printf("maybe in A[%d]-A[%d]\n", k, i-1);
    k = i;
    for ( ; i<N && A[i]==true; i++ );
    if ( i > k )
        printf("true in A[%d]-A[%d]\n", k, i-1);
    if ( i < N )
        printf("Wrong Answer\n");
}

int main()
{
    int N;
    ElementType *A;

    scanf("%d", &N);
    A = (ElementType *)malloc(N * sizeof(ElementType));
    Read( A, N );
    MySort( A, N );
    PrintA( A, N );
    return 0;
}

/* Your function will be put here */

Sample Input:

6
2 2 0 1 0 0

Sample Output:

false in A[0]-A[0]
maybe in A[1]-A[2]
true in A[3]-A[5]
//
//  main.c
//  Sort Three Distinct Keys
//
//  Created by 余南龙 on 2016/12/9.
//  Copyright © 2016年 余南龙. All rights reserved.
//

void MySort( ElementType A[], int N ){
    int T[N], F[N], M[N];
    , j = , k = , index;

    ; index < N; index++){
        if(true == A[index]){
            i++;
        }
        else if(false == A[index]){
            j++;
        }
        else if(maybe == A[index]){
            k++;
        }
    }
    ; index < j; index++){
        A[index] = false;
    }
    for( ; index - j< k; index++){
        A[index] = maybe;
    }
    for( ; index - k - j < i; index++){
        A[index] = true;
    }
}

最新文章

  1. 【原创】机器学习之PageRank算法应用与C#实现(2)球队排名应用与C#代码
  2. SCOI2005栅栏
  3. Linux运维人员如何学习python编程
  4. buddy算法
  5. TYVJ P1008 传球游戏
  6. uva 12549 最大流
  7. Files to be needed by importing the android application with eclipse
  8. Web Service 的服务端的引用
  9. Linux 中查看网口流量的利器 -- sar
  10. java IO和NIO的场景选择
  11. 只启动一个zookeeper配置 server1只需要配置一个
  12. mystring c++ 自己的string 封装
  13. LVM挂载失败mount: you must specify the filesystem type
  14. oracle ebs form开发总结
  15. HYSBZ 2743 (树状数组) 采花
  16. [LintCode] Binary Tree Level Order Traversal(二叉树的层次遍历)
  17. 2019.01.08 bzoj4543: [POI2014]Hotel加强版(长链剖分+dp)
  18. linux安装activemq
  19. 如何使用seajs+jQuery构建中型项目
  20. css的border的solid

热门文章

  1. Druid 基础使用-操作篇(Imply )
  2. Ejabberd作为推送服务的优化手段
  3. QT不同版本编译
  4. IIS 发布添加网站错误:HTTP 错误 500.21 - Internal Server Error 解决方案
  5. reverse engineering in ax
  6. Java中的内部类(回调)
  7. Retina屏实现1px边框
  8. Ax 导出EXCEL给范围内的每个单元格加边框
  9. c#winform程序,修改MessageBox提示框中按钮的文本
  10. Mysql 关键字及保留字