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. (转)基于即时通信和LBS技术的位置感知服务(一):提出问题及解决方案

    一.前言.提出问题 公司最近举行2011年度创新设计大赛,快年底了正打算写写2010年以来Android开发的心得与经验,正好同事出了个点子:假如A和B两个人分别在不同的地点,能不能实现这样的功能,让 ...

  2. Topo图

    http://blog.csdn.net/youfangyuan/article/details/8367398 http://joshuaxiao.iteye.com/blog/2224120 ht ...

  3. Google chrome的字体设置

    http://blog.sina.com.cn/s/blog_a3b863da01016sv3.html 谷歌浏览器(Google chrome)速度很快,很好用.问题是字体显示有时候不对:用英文版的 ...

  4. Google-Gson使用

    转自:http://my.oschina.net/itblog/blog/204120 这几天,因为项目的需要,接触了Google的Gson库,发现这个东西很好用,遂记下简单的笔记,供以后参考.至于G ...

  5. http://blog.csdn.net/majian_1987/article/details/44939911

    http://blog.csdn.net/majian_1987/article/details/44939911

  6. highcharts 切换

    <!doctype html> <html lang="en"> <head> <script type="text/javas ...

  7. SQLite入门与分析(二)---设计与概念

    写在前面:谢谢各位的关注,没想到会有这么多人关注.高兴的同时,也感到压力,因为我接触SQLite也就几天,也没在实际开发中用过,只是最近项目的需求才来研究它,所以我很担心自己的文章是否会有错误,误导别 ...

  8. 212. Word Search II

    题目: Given a 2D board and a list of words from the dictionary, find all words in the board. Each word ...

  9. 关于捕获键盘信息的processDialogkey方法2--具体应用

    自定义控件里的keydown方法无法捕获所有的按键消息的处理方法1(自定义控件里的keydown方法无法获取的键值如上下左右键等) 处理办法具体如下: 1.首先在自定义控件UserControl1中重 ...

  10. Android开发之动画(转)

    activity跳转的过渡效果,很漂亮,很全 注意,切换方法overridePendingTransition只能在startActivity和finish方法之后调用. 第一个参数为第一个Activ ...