public static int[] max_min(int[] a){
//res[0] records the minimum value while res[1] records the maximal one.
int res[] = new int[];
int n = a.length;
if(n == ) {
return res;
}
if(n == ) {
res[] = res[] = a[];
} int min, max;
if(n% == ) {
res[] = res[] = a[]; for(int i=; i<=n-; i+=) {
if(a[i] < a[i+]) {
min = a[i];
max = a[i+];
}
else {
min = a[i+];
max = a[i];
}
res[] = Math.min(min, res[]);
res[] = Math.max(max, res[]);
}
}
else {
res[] = Math.min(a[], a[]);
res[] = Math.max(a[], a[]);
for(int i=; i<=n-; i+=) {
if(a[i] < a[i+]) {
min = a[i];
max = a[i+];
}
else {
min = a[i+];
max = a[i];
}
res[] = Math.min(min, res[]);
res[] = Math.max(max, res[]);
}
} return res;
}

algorithm@ O(3/2 n) time to findmaximum and minimum in a array的更多相关文章

  1. Design and Analysis of Algorithms_Fundamentals of the Analysis of Algorithm Efficiency

    I collect and make up this pseudocode from the book: <<Introduction to the Design and Analysis ...

  2. 1.4 算法 - algorithm

    1)概述 2)示例 //algorithm find演示 #include <vector> #include <algorithm> #include <iostrea ...

  3. [Algorithm] Heap data structure and heap sort algorithm

    Source, git Heap is a data structure that can fundamentally change the performance of fairly common ...

  4. [Algorithms] Insertion sort algorithm using TypeScript

    Insertion sort is a very intuitive algorithm as humans use this pattern naturally when sorting cards ...

  5. array题目合集

    414. Third Maximum Number 给一个非空的整数数组,找到这个数组中第三大的值,如果不存在,那么返回最大的值.要求时间复杂度为o(n) 例如: Example 1: Input: ...

  6. Design and Analysis of Algorithms_Decrease-and-Conquer

    I collect and make up this pseudocode from the book: <<Introduction to the Design and Analysis ...

  7. Design and Analysis of Algorithms_Divide-and-Conquer

    I collect and make up this pseudocode from the book: <<Introduction to the Design and Analysis ...

  8. Design and Analysis of Algorithms_Introduction

    I collect and make up this pseudocode from the book: <<Introduction to the Design and Analysis ...

  9. Cracking the Code Interview 4.3 Array to Binary Tree

    Given a sorted (increasing order) array, write an algorithm to create a binary tree with minimal hei ...

随机推荐

  1. VC中不同类型DLL及区别

    1. DLL的概念可以向程序提供一些函数.变量或类. 静态链接库与动态链接库的区别:(1)静态链接库与动态链接库都是共享代码的方式.静态链接库把最后的指令都包含在最终生成的EXE文件中了:动态链接库不 ...

  2. Tomcat多次部署

    http://blog.csdn.net/knityster/article/details/6300804

  3. POJ3207+tarjan+2-sat

    /* 2-sat 题意:给定一个圆,圆上一些点.两点一线.现给出一些线,这些线可以在圆内连起来,也可以在圆外. 问有没有可能所有的线画完 且 不出现相交. 思路:把线画在圆内或圆外 看成一个组合.其它 ...

  4. Windows10搭建PHP7开发环境

    原文:Windows10搭建PHP7开发环境 3年前写了一篇<Windows下搭建PHP开发环境>之后就再也没有碰过PHP了,最近新发布了PHP7然后回去看了一下之前写的文章,发现很多配置 ...

  5. 【一】 sched.h

    第一个数据结构体是 task_struct ,这个数据结构被内核用来表示进程,包含其所有信息. 定义于文件 include/linux/sched.h 中,先看看其完整定义 struct task_s ...

  6. WinCE NAND flash - FAL

    http://blog.csdn.net/renpine/article/details/4572347 http://msdn.microsoft.com/en-US/library/ee48203 ...

  7. VS2005工程由Pocket PC 2003 SDK转为WINCE6.0 SDK的问题

    把VS2005工程有采用的Pocket PC 2003 SDK改为WINCE6.0 SDK,具体操作见链接 http://blog.csdn.net/loongembedded/article/det ...

  8. IE中Ajax数据缓存的问题

    在IE中,Ajax回来的数据会有缓存的现象发生,解决方式: 1,改变URL,每次请求加一个随机数参数Math.random()或者new Date().getTime(),这样IE会认为URL不一样, ...

  9. sencha touch tabsidebar 源码扩展

    先上图看效果 没错,这是一个sencha touch 项目,而这里的右边推出效果(下文叫做tabsiderbar),使用插件tabsiderbar来扩展的. 插件js下载地址:http://www.m ...

  10. ,net运行框架

    .NET FrameWork框架 是一套应用程序开发框架,主要目的提供一个开发模型. 主要的两个组件: 公共语言运行时(Common Language Runtime)(CLR): 提供内存管理.线程 ...