algorithm@ O(3/2 n) time to findmaximum and minimum in a array
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的更多相关文章
- 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 ...
- 1.4 算法 - algorithm
1)概述 2)示例 //algorithm find演示 #include <vector> #include <algorithm> #include <iostrea ...
- [Algorithm] Heap data structure and heap sort algorithm
Source, git Heap is a data structure that can fundamentally change the performance of fairly common ...
- [Algorithms] Insertion sort algorithm using TypeScript
Insertion sort is a very intuitive algorithm as humans use this pattern naturally when sorting cards ...
- array题目合集
414. Third Maximum Number 给一个非空的整数数组,找到这个数组中第三大的值,如果不存在,那么返回最大的值.要求时间复杂度为o(n) 例如: Example 1: Input: ...
- Design and Analysis of Algorithms_Decrease-and-Conquer
I collect and make up this pseudocode from the book: <<Introduction to the Design and Analysis ...
- Design and Analysis of Algorithms_Divide-and-Conquer
I collect and make up this pseudocode from the book: <<Introduction to the Design and Analysis ...
- Design and Analysis of Algorithms_Introduction
I collect and make up this pseudocode from the book: <<Introduction to the Design and Analysis ...
- 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 ...
随机推荐
- python的web压力测试工具-pylot安装使用
http://blog.csdn.net/chenggong2dm/article/details/10106517 pylot是python编写的一款web压力测试工具.使用比较简单.而且测试结果相 ...
- 程序自动生成Dump文件
前言:通过drwtsn32.NTSD.CDB等调试工具生成Dump文件, drwtsn32存在的缺点虽然NTSD.CDB可以完全解决,但并不是所有的操作系统中都安装了NTSD.CDB等调试工具.了解了 ...
- cf 148D 概率DP
题意:原来袋子里有w只白鼠和b只黑鼠龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老师谁就赢.王妃每次抓一只老鼠,龙每次抓完一只老鼠之后会有一只老鼠跑出来.每次抓老鼠和跑出来的老鼠都是随机的.如果两个人都没 ...
- EasyUI combotree值的设置 setValue
如果combotree的json数据如下: [ { "id":"2", "text":"wwwww", "st ...
- ubuntu下如何设置主机名
方法如下: 在终端输入 hostname 查看主机名主机名存放在/etc/hostname中 ,sudo gedit /etc/hostname 修改后保存/etc/hosts 还有一份 , sudo ...
- ids & hdmi 原理
http://www.taiwanwebinar.com/zh_TW/STATIC/SITE/dwc_hdmi_tx.pdf http://blog.csdn.net/g_salamander/art ...
- Servlet课程0425(七) 到数据库中去验证用户,同时防止SQL注入漏洞
Login.java //登录界面 package com.tsinghua; import javax.servlet.http.*; import java.io.*; public class ...
- CheckBoxList 获取与设置选中的值
/// <summary> ///CheckBoxListHelper 的摘要说明 ///CheckBoxList获取与设置选中的值 /// </summary> public ...
- j2se jndi
http://blog.csdn.net/lldwolf/article/details/2299622 正如你可以看到,从JNDI中检索对象是相当方便,简单.通过使用JNDI来存储配置信息,无状态对 ...
- Linux用户空间与内核空间
源:http://blog.csdn.net/f22jay/article/details/7925531 Linux 操作系统和驱动程序运行在内核空间,应用程序运行在用户空间,两者不能简单地使用指针 ...