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子句后指定了一个表的多个列条件时,优化器通常会将多个列的选择性 ...
随机推荐
- 常用的.Net 知识点
1.Replace C#:(using System.Text.RegularExpressions;) string txt = Regex.Replace(txtLog.Text.ToString ...
- centos 解压jdk安装包方式安装jdk 出现 java/lang/NoClassDefFoundError: java/lang/Object 错误
安装完JDK ,设定环境变量后出现这个错误: [root@localhost lib]# javacError occurred during initialization of VMjava/lan ...
- AX7: CREATE AN AUTOMATED TEST PACKAGE\MODEL
AX7: CREATE AN AUTOMATED TEST PACKAGE\MODEL It’s really important for a stable solution the use of a ...
- 线程Thread
一.线程: 程序:保存在物理介质(光盘,软盘,硬盘)当中的代码片段 进程:一旦程序运行起来,就变成了操作系统当中的一个进程 线程:程序当中一条独立执行的线索 二.线程的五大状态 新生 就绪 运行 ...
- JOST数据 日期转换
开发中有时候需要从服务器端返回json格式的数据,在后台代码中如果有DateTime类型的数据使用系统自带的工具类序列化后将得到一个很长的数字表示日期数据,如下所示: 复制代码代码如下: //设置服务 ...
- 湖人VS爵士!!科比4月14日最后一战,本赛季最高得分!狂得60分!!完美大逆转!!!
莫愁前路无知己,天下谁人不识君.科比,愿你如迈克尔·乔丹,仍然活跃在篮球界.退役不是结束,而是另一段人生的开始. 北京时间2016年4月14日,湖人101-96击败爵士,科比-布莱恩特告别战,20年职 ...
- webApi上传下载文件
上传文件通过webApi html端调用时包含(form提交包含 enctype="multipart/form-data",才可以启作用获取到文件) public class U ...
- C# DataGridView显示行号的三种方法
方法一: 网上最常见的做法是用DataGridView的RowPostPaint事件在RowHeaderCell中绘制行号: private void dgGrid_RowPostPaint( obj ...
- Div添加阴影效果
-moz-box-shadow: 2px 2px 10px #909090;/*firefox*/ -webkit-box-shadow: 2px 2px 10px #909090;/*safari或 ...
- SQL SERVER中求上月、本月和下月的第一天和最后一天 DATEADD DATEDIFF
SQL SERVER中求上月.本月和下月的第一天和最后一天 1.上月的第一天 SELECT CONVERT(CHAR(10),DATEADD(month,-1,DATEADD(dd,-DAY(GE ...