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. 正确使用STL-MAP中Erase函数

    一切尽在代码中. #include <iostream> #include <map> #include <string> using namespace std ...

  2. 修改MYSQL数据库表的字符集

    MySQL 乱码的根源是的 MySQL 字符集设置不当的问题,本文汇总了有关查看 MySQL 字符集的命令.包括查看 MySQL 数据库服务器字符集.查看 MySQL 数据库字符集,以及数据表和字段的 ...

  3. hbase集群在启动的时候找不到JAVA_HOME的问题

    hbase集群在启动的时候找不到JAVA_HOME的问题,启动集群的时候报错信息如下: root@master:/usr/local/hbase-/bin# ./start-hbase.sh star ...

  4. javaweb学习总结(四十六)——Filter(过滤器)常见应用

    一.统一全站字符编码 通过配置参数charset指明使用何种字符编码,以处理Html Form请求参数的中文问题 1 package me.gacl.web.filter; 2 3 import ja ...

  5. Android UI-开源框架ImageLoader的完美例子

    Android开源框架ImageLoader的完美例子 2013年8月19日开源框架之Universal_Image_Loader学习 很多人都在讨论如何让图片能在异步加载更加流畅,可以显示大量图片, ...

  6. ANDROID_MARS学习笔记_S01_006ImageView

    一.ImageView介绍 设置scalType Must be one of the following constant values. Constant Value Description ma ...

  7. Android:控件布局(相对布局)RelativeLayout

    RelativeLayout是相对布局控件:以控件之间相对位置或相对父容器位置进行排列. 相对布局常用属性: 子类控件相对子类控件:值是另外一个控件的id android:layout_above-- ...

  8. Retrofit初识

    Retrofit Retrofit是一套RESTful架构的Android(Java)客户端实现,基于注解,提供JSON to POJO(Plain Ordinary Java Object,简单Ja ...

  9. 常用PHP框架功能对比表

    自接触PHP开发以来,已使用了不少框架,虽然对每个框架都没有专研至深,但对每一款所使用的PHP框架功能都有一些了解的.至此,本人将大家常用的一些PHP框架功能整理成表,希望对大家在选择PHP框架时,可 ...

  10. FastScroll(3)分组的listview 打开fastscroll的分组提示功能

    1,让ListView显示分组(用两个layout) 2,让ListView实现sectionIndexer接口 3,代码如下: import java.util.ArrayList; import ...