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统计最大最小元素的平均比较次数的更多相关文章

  1. 选择问题(选出第i个最小元素)

    通过分治法解决的分析(还有其他方法解决选择问题如使用 堆) 1 同快速排序一样,对输入的数组进行递归分解 不同的是:快速排序会递归处理分解的两边,而选择问题只处理需要的一边 2 选择问题的期望时间代价 ...

  2. 把总数amount拆分以标准差最小的标准,平均拆分成count个整数

    public function tt_add(){ $res = $,); echo array_sum($res); echo '----' . count($res); dump($res); e ...

  3. 42.旋转数组的最小元素[Get min value of rotated array]

    [题目] 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转.输入一个排好序的数组的一个旋转,输出旋转数组的最小元素.例如数组{3, 4, 5, 1, 2}为{1, 2, 3, 4, 5 ...

  4. 定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数。

    // test14.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...

  5. 自定义栈类型,具有找到站内最小元素的min函数 ,且min(),pop(),push()函数的时间复杂度为O(1)

    基本思想: // 借助一个辅助栈,入栈时,若新元素比辅助栈栈顶元素小,则直接放入辅助站 // 反之,辅助站中放入次小元素(即辅助栈栈顶元素)====保证最小元素出栈时,次小元素被保存 static c ...

  6. C++ *max_element函数找最大元素 *min_element函数找最小元素 STL算法(转)

    http://blog.sina.com.cn/s/blog_6f3a860501019z1f.html #include<iostream> #include<algorithm& ...

  7. 基于visual Studio2013解决面试题之0707最小元素

     题目

  8. 实现栈最小元素的min函数

    #include<iostream> #include<stack> using namespace std; class min_stack { public: void p ...

  9. 《剑指Offer》第20题(Java实现):定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))。

    一.题目描述 定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1)). 二.思路解析 首先定义一个Integer类型的栈,记为stack,此栈用来完成数据 ...

随机推荐

  1. git 取消未成功的 merge

    git 取消未成功的 merge # 合并时遇到冲突想取消操作,恢复index $ git merge --abort # 可以回退到某个提交 $ git reset --hard # 可以撤销某个提 ...

  2. TypeScript Interface vs Types All In One

    TypeScript Interface vs Types All In One TypeScript https://www.typescriptlang.org/docs/handbook/adv ...

  3. 5G & 音频,视频

    5G & 音频,视频 直播,webtrtc 音频,视频 基础知识 基本概念.播放流程.封装格式.编解码.传输协议 音视频播放流程 主要流程:采集 -> 前处理 -> 编码 -> ...

  4. DevOps in Action

    DevOps in Action DevOps is a set of software development practices that combines software developmen ...

  5. vue & components & props & methods & callback

    vue & components & props & methods & callback demo solution 1 & props & data ...

  6. Cookie 政策

    Cookie 政策 合规/隐私协议 https://www.synology.cn/zh-cn/company/legal/cookie_policy Cookie Cookie 政策 生效日期:20 ...

  7. nasm 函数返回一个数组 x86

    getArguments.asm: extern VirtualAlloc section .text global dllmain export getArguments dllmain: mov ...

  8. js in depth & prototype & __proto__

    js in depth & prototype & proto 实例的 proto 与 父类的 prototype,同时指向 父类的构造函数: https://hackernoon.c ...

  9. svg all in one

    svg all in one show svg in html methods https://vecta.io/blog/best-way-to-embed-svg https://css-tric ...

  10. 大送福利!市场香饽饽VAST到底什么来头,为何被高价估值

    近日,NGK星空计划新币VAST成为了香饽饽,还未正式上线前,市场讨论的热度就居高不下.如今NGK推出1万VAST免费送新人福利更是将这波热度推向了高潮. 具体福利规则:在美国加州时间2021年2月8 ...