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. json工具包比较 fastjson jackson gson

    对json进行json-object进行相互转化时,笔者接触到三种工具jar,现对其进行比较. fastjson:速度最快,阿里巴巴开源. jackson:springMvc 默认使用. gson:谷 ...

  2. java时间类简单总结

    java时间类(Data类) 1.Data类(没有考虑到国际化,好多方法已过时java.util.Data包中) 父类(是类不是接口含有直接子类3个):  日期格式为:年月日时分秒(不包含毫秒部分) ...

  3. QTP学习笔记之—VBS

    1.ToString() : Returns a string that represents the current test object. Example The following examp ...

  4. ssh 的搭建

    struts包的下载:http://struts.apache.org/download.cgi#struts252 string包的下载: http://repo.spring.io/release ...

  5. JSON.parse

    摘自:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse The J ...

  6. 逻辑运算符&&和&的区别 ||和|的区别

    A:最终结果一样. B:&& 和 || 有短路作用,左边是false ,右边不执行.

  7. 跨域的get和post的区别

    GET和POST是HTTP请求的两种基本方法,要说它们的区别,接触过WEB开发的人都能说出一二.最直观的区别就是GET把参数包含在URL中,POST通过request body传递参数.当你在面试中被 ...

  8. Python面试题

    1.Python装饰器 详情 2.设置多个Python项目使用不同版本的Python和第三方库 使用PyEnv 详情 3.PEP8 详情 4.参数传递 按引用传递 5.列表解析,字典解析 详情 6.列 ...

  9. 第六百零六天 how can I 坚持(应该是六百零六天吧)

    找了个考研的借口,也是挺逗的,终于结束了,而且考的很渣. 最近发生了很多事,很快就要离开泛华了,放弃安逸,开始改变吧,其实感觉自己内心挺怂的,很怕改变,哎,这不像是有梦想,能成事的人应该有的. 还是想 ...

  10. asp.net 查询好的数据后 排序显示在桌面上

    select top 10 * ,row()_number over ( order by 字段 desc) as rownum  from  表 row()_number;简单的说row_numbe ...