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;
    }
}

PTA Sort Three Distinct Keys的更多相关文章

  1. NSMutable sort排序

    Compare method Either you implement a compare-method for your object: - (NSComparisonResult)compare: ...

  2. .NET 排序 Array.Sort<T> 实现分析

    System.Array.Sort<T> 是.NET内置的排序方法, 灵活且高效, 大家都学过一些排序算法,比如冒泡排序,插入排序,堆排序等,不过你知道这个方法背后使用了什么排序算法吗? ...

  3. [转载]两个半小时学会Perl

    Learn Perl in about 2 hours 30 minutes By Sam Hughes Perl is a dynamic, dynamically-typed, high-leve ...

  4. 使用DBMS_STATS来收集统计信息【转】

    overview Oracle's cost-based optimizer (COB) uses statistics to calculate the selectivity (the fract ...

  5. Hash function

    Hash function From Wikipedia, the free encyclopedia   A hash function that maps names to integers fr ...

  6. sort-based shuffle的核心:org.apache.spark.util.collection.ExternalSorter

    依据Spark 1.4版 在哪里会用到它 ExternalSorter是Spark的sort形式的shuffle实现的关键.SortShuffleWriter使用它,把RDD分区中的数据写入文件. o ...

  7. How To Size Your Apache Flink® Cluster: A Back-of-the-Envelope Calculation

    January 11, 2018- Apache Flink Robert Metzger and Chris Ward A favorite session from Flink Forward B ...

  8. 算法(第四版)C# 习题题解——2.3

    写在前面 整个项目都托管在了 Github 上:https://github.com/ikesnowy/Algorithms-4th-Edition-in-Csharp 查找更为方便的版本见:http ...

  9. 11G新特性 -- Multicolumn Statistics (Column groups)

    默认oracle会收集表中各个列的统计信息,但是会忽略列之间的关联关系.在大多情况下,优化器假设在复杂查询中的列之间是独立的.当where子句后指定了一个表的多个列条件时,优化器通常会将多个列的选择性 ...

随机推荐

  1. Druid 基础使用-操作篇(Pivot、plyql)

    一.Pivot  --9095 端口  二.基本sql 使用 .plysql--   http://plywood.imply.io/plyql  (下面的端口应该是8082,我这个地方做了端口转换) ...

  2. JAVA基础知识:容器

    JDK所提供的容器都在java.util包里面,下面开始讨论的都是JDK1.4版本的,只讲述基本知识,不涉及泛型 容器API的类图结构如下图所示 Set:元素无顺序且不可重复      List:元素 ...

  3. 【VB6】VB6实现拖拽

    首先,设置OLEDropMode OLEDropMode = 1 然后插入代码 Private Sub 控件名_OLEStartDrag(Data As DataObject, AllowedEffe ...

  4. IE10中session失效取不到值的问题

    在eworkflow工作流,ebiao报表,eform自定义表单产品升级到IE10的时候,系统登录后,总是会取不到session中的值. for j2ee版本和for dotnet版本都一样取不到值. ...

  5. matlab函数_连通区域

    1. matlab函数bwareaopen──删除小面积对象格式:BW2 = bwareaopen(BW,P,conn)作用:删除二值图像BW中面积小于P的对象,默认情况下使用8邻域.算法:(1)De ...

  6. V8 data struct

    参考: [1]. 使用 Google V8 引擎开发可定制的应用程序: http://www.ibm.com/developerworks/cn/opensource/os-cn-v8engine/ ...

  7. Thinkphp源码分析系列(九)–视图view类

    视图类view主要用于页面内容的输出,模板调用等,用在控制器类中,可以使得控制器类把表现和数据结合起来.下面我们来看一下执行流程. 首先,在控制器类中保持着一个view类的对象实例,只要继承自控制器父 ...

  8. OC Runtime

    OC 是面向运行时的语言.Runtime就是系统在运行的时候的一些机制,其中最主要的是消息发送机制.OC语言与其他语言(如C语言)在函数(方法)的调用有很大的不同.C语言,函数的调用在编译的时候就已经 ...

  9. Ubuntu更改鼠标灵敏度

    需要命令:xinput 清自行用 man xinput 查询 xinput 帮助文档 1.插入鼠标,打开终端,输入命令:xinput 查询当前已挂在设备 2.拔出鼠标,打开终端,再输入命令:xinpu ...

  10. 1o_Samba

    ∮Linux下Samba的安装配置 §Linux 下文件共享软件 Samba 是一款实现跨主机,跨平台文件共享的软件.同类型的软件还有 ftp,nfs 不再赘述. §文件共享协议 Windows : ...