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子句后指定了一个表的多个列条件时,优化器通常会将多个列的选择性 ...
随机推荐
- Spark On YARN使用时上传jar包过多导致磁盘空间不够。。。
今天测试过程中发现YARN Node变成Unhealthy了,后来定位到硬盘空间不够..... 通过查找大于100M的文件时发现有N多个spark-assembly-1.4.0-SNAPSHOT-ha ...
- 关于intellij IDEA 上junit的用法
话说,最近正在看视频学java.里面有个叫做junit的东西很有用.但是实话说我摆弄了半天都没弄明白. 今天呢通过一些资料,终于弄清楚了junit的大致用法,这里写出来,用以分享和备忘. 首先,环境和 ...
- MVC中使用内建的HTML辅助方法产生表单元素提交表单与button按钮事件的陷阱
网站模板页有个登陆的退出按钮,当点击时跳转到登陆页面. <button onclick="logout()" >退出</button> $("#l ...
- phpwind ecshop 用户整合
phpwind ecshop 用户整合,其实很简单.但在网上搜到的尽是乱七八遭的方法,搞得很复杂. 原来公司做的phpwind 与 ecshop 结合的项目,别的同事已经把用户整合好了,当时我还不知道 ...
- SqlServer Where后面Case When语句的写法
select * from tb where (case when col='***' then '***' else '***' end)='***'
- Docker学习过程中遇到的问题及解决方法
1.重新安装Docker后,运行不起来 [root@zyt-test-14-53 ~]# docker infoCannot connect to the Docker daemon. Is the ...
- for..in遍历,枚举器
#pragma mark ------------for循环遍历集合中的元素------ //创建一个数组,包含5个字符串对象,倒序取出数组中的所有元素,并存储到另一可变数组中 NSArray *ar ...
- php 判断文件/目录是否存的方法
涉及函数 is_file(), is_dir() , file_exists() is_file() 判断文件是否存在 is_dir() 判断目录是否存在 file_exists() 既可用于判断文件 ...
- adb常用命令
adb命令的主要用途 1. 运行android设备的shell(命令行). 2.管理模拟器或android设备的映射端口. 3. 安装和卸载应用程序. 4.计算机和android设备之间的上传 ...
- vmware workstation 上创建的centos 7.2 ,新添加一块网卡。无法找到配置文件。
在vmware workstation 11上,新建一个centos 7.2系统. 初装带有一个块网卡:能够在/etc/sysconfig/network-scripts/目录下找到相应的网卡配置文件 ...