ch1_5_1统计最大最小元素的平均比较次数
public class ch1_5_1统计最大最小元素的平均比较次数 {
public static void main(String[] args) {
// TODO Auto-generated method stub
double ans=0;
for(int i=0;i<10;i++)
ans+=count();
System.out.println(ans/10);
}
private static int count() {
int a[]=new int[10];
for(int i=0;i<a.length;i++) {
a[i]=(int)(Math.random()*20+1);
}
int max=a[0];
int min=a[0];
int count=0;
for(int i=1;i<a.length;i++) {
count++;
if(a[i]>max) {max=a[i];}
else {
count++;
if(a[i]<=min)
{
min=a[i];
}
}
}
return count;
}
}
ch1_5_1统计最大最小元素的平均比较次数的更多相关文章
- 选择问题(选出第i个最小元素)
通过分治法解决的分析(还有其他方法解决选择问题如使用 堆) 1 同快速排序一样,对输入的数组进行递归分解 不同的是:快速排序会递归处理分解的两边,而选择问题只处理需要的一边 2 选择问题的期望时间代价 ...
- 把总数amount拆分以标准差最小的标准,平均拆分成count个整数
public function tt_add(){ $res = $,); echo array_sum($res); echo '----' . count($res); dump($res); e ...
- 42.旋转数组的最小元素[Get min value of rotated array]
[题目] 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转.输入一个排好序的数组的一个旋转,输出旋转数组的最小元素.例如数组{3, 4, 5, 1, 2}为{1, 2, 3, 4, 5 ...
- 定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数。
// test14.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...
- 自定义栈类型,具有找到站内最小元素的min函数 ,且min(),pop(),push()函数的时间复杂度为O(1)
基本思想: // 借助一个辅助栈,入栈时,若新元素比辅助栈栈顶元素小,则直接放入辅助站 // 反之,辅助站中放入次小元素(即辅助栈栈顶元素)====保证最小元素出栈时,次小元素被保存 static c ...
- C++ *max_element函数找最大元素 *min_element函数找最小元素 STL算法(转)
http://blog.sina.com.cn/s/blog_6f3a860501019z1f.html #include<iostream> #include<algorithm& ...
- 基于visual Studio2013解决面试题之0707最小元素
题目
- 实现栈最小元素的min函数
#include<iostream> #include<stack> using namespace std; class min_stack { public: void p ...
- 《剑指Offer》第20题(Java实现):定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))。
一.题目描述 定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1)). 二.思路解析 首先定义一个Integer类型的栈,记为stack,此栈用来完成数据 ...
随机推荐
- element-ui dialog loading
element-ui dialog loading 指令方式 服务方式 v-loading 当使用指令方式时,全屏遮罩需要添加fullscreen修饰符(遮罩会插入至 body 上),此时若需要锁定屏 ...
- GitHub Actions in Action
GitHub Actions in Action https://lab.github.com/githubtraining/github-actions:-hello-world https://g ...
- Code Book All In One
Code Book All In One Jupyter Notebook Jupyter Lab https://jupyter.org/ Storybook https://storybook.j ...
- 微信小程序-云开发-实战项目
微信小程序-云开发-实战项目 微信小程序 微信小程序平台服务条款 https://developers.weixin.qq.com/miniprogram/product/service.html h ...
- mongodb & vue & node.js
mongodb mongodb & vue & node.js https://docs.mongodb.com/manual/tutorial/install-mongodb-on- ...
- JavaScript Inheritance All in One
JavaScript Inheritance All in One constructor inheritance prototype chain inheritance "use stri ...
- taro & Error: spawn taro ENOENT
taro & Error: spawn taro ENOENT https://stackoverflow.com/questions/27688804/how-do-i-debug-erro ...
- 云服务器Centos7部署Tomcat服务器
目录 部署Tomcat服务器 1.安装JDK1.8 2.安装与启动tomcat 配置安全组(8080端口) 参考文章 部署Tomcat服务器 1.安装JDK1.8 JDK下载地址:https://ww ...
- 学习String源码的部分方法
先看构造器: private final char value[]; //char类型的数组 以下均会用到 private int hash; //缓存字符串的哈希值 //以下均会用到 public ...
- SpringBoot配置本地文件映射路径
1.前言 在springboot的项目中,如果需要通过项目方式访问本地磁盘的文件,不仅可以使用nginx代理的方式,还可以使用springboot配置的方式进行访问. 实例原因说明:由于上传的图片是要 ...