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. Hibernate-Validation的使用

    首先是要加入下面两个包 hibernate-validator-4.1.0.Final.jar validation-api-1.0.0.GA.jar 如果在验证不通过的时候进行了添加.更新或删除操作 ...

  2. 谈 DevOps 自动化时,也应该考虑到 SOX 等法案

    [编者按]作者 Aaron Volkmann 是 CERT Division 高级研究员,在本文中,他对 DevOps 自动化违反 SOX 法案进行了阐述.同时,也简单的提出了如何通过 CI 来避免这 ...

  3. hdu 4454 Stealing a Cake 三分法

    很容易想到三分法求解,不过要分别在0-pi,pi-2pi进行三分. 另外也可以直接暴力枚举…… 代码如下: #include<iostream> #include<stdio.h&g ...

  4. ADT(android-bundler) HTML EDIT 编辑 xml HTML

    逗比的ADT,安装个html一直不成功,最后发现了如下方法 Helper_Install New Software_ http://download.eclipse.org/releases/indi ...

  5. 【转】windows c++获取文件信息——_stat函数的使用

    _stat函数的功能 _stat函数用来获取指定路径的文件或者文件夹的信息. 函数声明 int _stat( const char *path, struct _stat *buffer ); 参数: ...

  6. C#字符串的常用操作

    一.string关键字与StringBuilder类 C# 字符串是使用 string 关键字声明的一个字符数组.字符串是使用引号声明的,如下例所示: string s = "Hello, ...

  7. 96. Unique Binary Search Trees

    题目: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For e ...

  8. 2.1 linux中uboot移植

    -- --------------------------------------------------------------------------------------- (一)友善之臂介绍 ...

  9. 开源布局控件 WeifenLuo.WinFormsUI.Docking.dll使用

    WeifenLuo.WinFormsUI.Docking是一个很强大的界面布局控件,可以保存自定义的布局为XML文件,可以加载XML配置文件.! 先看一下效果 使用说明: 1.新建一个WinForm程 ...

  10. Linux下执行程序出现 Text file busy 提示时的处理方式

    使用 fuser xxx 命令查看xxx文件被哪个进程占用,然后关闭该进程,解决问题. # fuser xxxxxx:              2878# kill -9 2878 注:xxx是文件 ...