PTA Sort Three Distinct Keys
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的更多相关文章
- NSMutable sort排序
Compare method Either you implement a compare-method for your object: - (NSComparisonResult)compare: ...
- .NET 排序 Array.Sort<T> 实现分析
System.Array.Sort<T> 是.NET内置的排序方法, 灵活且高效, 大家都学过一些排序算法,比如冒泡排序,插入排序,堆排序等,不过你知道这个方法背后使用了什么排序算法吗? ...
- [转载]两个半小时学会Perl
Learn Perl in about 2 hours 30 minutes By Sam Hughes Perl is a dynamic, dynamically-typed, high-leve ...
- 使用DBMS_STATS来收集统计信息【转】
overview Oracle's cost-based optimizer (COB) uses statistics to calculate the selectivity (the fract ...
- Hash function
Hash function From Wikipedia, the free encyclopedia A hash function that maps names to integers fr ...
- sort-based shuffle的核心:org.apache.spark.util.collection.ExternalSorter
依据Spark 1.4版 在哪里会用到它 ExternalSorter是Spark的sort形式的shuffle实现的关键.SortShuffleWriter使用它,把RDD分区中的数据写入文件. o ...
- 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 ...
- 算法(第四版)C# 习题题解——2.3
写在前面 整个项目都托管在了 Github 上:https://github.com/ikesnowy/Algorithms-4th-Edition-in-Csharp 查找更为方便的版本见:http ...
- 11G新特性 -- Multicolumn Statistics (Column groups)
默认oracle会收集表中各个列的统计信息,但是会忽略列之间的关联关系.在大多情况下,优化器假设在复杂查询中的列之间是独立的.当where子句后指定了一个表的多个列条件时,优化器通常会将多个列的选择性 ...
随机推荐
- 小结一下:javascript 金额计算
今天在项目中开发一个计算金额的功能,开始我是这样做的: eg: var amount += parseFloat( 0.01+0.02); 结果:0.0300000000000000001 为什么呢? ...
- Android工作学习第5天之Activity的传值问题
注:本文大部分为网上转载,本人只是根据工作的需要略做整合! 本章将借用一个实例,讲解如何注册并激活一个新的Activity,以及多个Activity之间如何传值. 下面是主Activity的代码: [ ...
- oracle查询中文数据出现乱码
首先,在oracle中,输入select userenv('language') from dual,查询出oracle使用的编码方式,我的是SIMPLIFIED CHINESE_CHINA.ZHS1 ...
- JAVA Socket 编程学习笔记(一)
1. Socket 通信简介及模型 Java Socket 可实现客户端--服务器间的双向实时通信.java.net包中定义的两个类socket和ServerSocket,分别用来实现双向连接的cli ...
- SQLyog-直接导出JSON格式的数据
前言:以前做过的一个项目,有这样的一个需求使用搜索引擎来查询对应的区域信息,不过区域信息要先导出来,并且数据格式是JSON格式的,在程序中能实现这个需求,不过下面的这种方法更加的简单,通过 ...
- nginx 使用
1.下载nginx包http://files.cnblogs.com/files/jyjin/nginx.zip 2.解压后找到nginx.conf文件进行配置 3.配置server选项: serve ...
- python celery + redis
redis http://debugo.com/python-redis celery http://docs.jinkan.org/docs/celery/getting-started/intro ...
- python转exe的小工具
其实只是在cxfreeze的基础上加了个壳,做成窗口软件了 使用了pyqt做的界面,软件发布在了开源中国上,可以直接去下面的地址查看 http://git.oschina.net/robocky/py ...
- xxx.app已损坏,打不开.你应该将它移到废纸篓 macOS 10.12 Sierra
出现这个问题的解决方法: 修改系统配置:系统偏好设置... -> 安全性与隐私.修改为任何来源 如果没有这个选项的话 (macOS Sierra 10.12) ,打开终端,执行 sudo spc ...
- rabbitMQ学习(一)
一般模式 服务端: import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabb ...