2388 Who's in the Middle(简单排序)

训练计划的第一个问题,首先从水问题开始:排序的数组,中间数则输出。

http://poj.org/problem?id=2388

冒泡排序:

#include <iostream>

using namespace std;

int main()
{
    int i, j, n,t;
    int a[10000];
    cin>>n;
    for(i=0; i<n; i++)
    {
        cin>>a[i];
    }

//冒泡排序
    for(i=0; i<n-1; i++)
        for(j=0; j<n-i-1; j++)
            if(a[j]<a[j+1])
            {
                t=a[j];
                a[j]=a[j+1];
                a[j+1]=t;
            }

    cout<<a[n/2]<<endl;
    return 0;
}

高速排序:

#include <iostream>

using namespace std;

//高速排序
void qsort(int a[ ], int l, int r)
{
    int x=a[l], i=l, j=r;
    if(l>=r) return;
    while(i<j)
    {
        while(i<j && a[j]>=x) j--;
        a[i]=a[j];
        while(i<j && a[i]<=x) i++;
        a[j]=a[i];
    }
    a[i]=x;
    qsort(a,l,i-1);
    qsort(a,i+1,r);
}
int main()
{
    int i, n;
    int a[10000];
    cin>>n;
    for(i=0; i<n; i++)
    {
        cin>>a[i];
    }
    qsort(a,0,n-1);
    cout<<a[n/2]<<endl;
    return 0;
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

2388 Who's in the Middle(简单排序)

时间: 2024-08-22 12:39:40

2388 Who&#39;s in the Middle(简单排序)的相关文章

Who&#39;s in the Middle(简单排序)

训练计划第一题,先从水题开始:对数组进行排序,然后输出中间数. http://poj.org/problem?id=2388 冒泡排序: #include <iostream> using namespace std; int main() { int i, j, n,t; int a[10000]; cin>>n; for(i=0; i<n; i++) { cin>>a[i]; } //冒泡排序 for(i=0; i<n-1; i++) for(j=0;

POJ 2388:Who&#39;s in the Middle

Who's in the Middle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 31015   Accepted: 17991 Description FJ is surveying his herd to find the most average cow. He wants to know how much milk this 'median' cow gives: half of the cows give

排序算法之简单排序算法

一.前言 其实排序对于前端工程师来说没有后端那么重要,不过理解排序的原理还是比较有用的.例如有些应用数据比较少量的就不需要用后台来排序,如果用php后端脚本等排序会增加后台的开销.放在前端岂不是更好,当然我们有一个前端的排序神器sort.当然也做了很多得优化,不过用sort排序还需要注意一点,sort默认不传参数时候的排序是按照字母顺序对数组中的元素进行排序,其实就是按照字符编码的顺序进行排序.        今天主要复习一下简单得排序,分别为冒泡排序.插入排序.选择排序,冒泡目前是我认为最简单

简单排序

冒泡排序 选择排序 插入排序 时间复杂度 O(n^2) 冒泡排序è基本不用  过于简单比较次数(见下) 与 交换次数(平均为比较次数的一半) 均正比于N^2 选择排序è快于冒泡 交换时间级别大于比较时间级别选择该排序 比较次数(见下) 正比于N^2 交换次数N 插入排序 VS表插入排序 效率比较 插入排序>(略快于)选择排序>冒泡排序 插入排序 表插入排序 原理 简单排序之一 利用有序链表插入效率的比较为O(N)的特点 比较次数MAX (N^2-N)/2 N 比较次数AVG (N^2-N)/4

JavaScript 实现页面元素(ul-li)的简单排序

JavaScript 实现页面元素(ul-li)的简单排序 html页面: <input type="button" value="show" onclick="show()" /> <ul id="ul1"> <li>4</li> <li>3</li> <li>2</li> <li>1</li> </

Java数据结构和算法之数组与简单排序

一.数组于简单排序 数组 数组(array)是相同类型变量的集合,可以使用共同的名字引用它.数组可被定义为任何类型,可以是一维或多维.数组中的一个特别要素是通过下标来访问它.数组提供了一种将有联系的信息分组的便利方法. 一维数组 一维数组(one‐dimensional array )实质上是相同类型变量列表.要创建一个数组,你必须首先定义数组变量所需的类型.通用的一维数组的声明格式是: type var‐name[ ]; 获得一个数组需要2步: 第一步,你必须定义变量所需的类型. 第二步,你必

UVA - 10905 - Children&#39;s Game (简单排序)

UVA - 10905 Children's Game Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description 4thIIUCInter-University Programming Contest, 2005 A Children's Game Input: standard input Output: standard output Problems

简单排序poj2388

#include <iostream>#include <stdio.h>#include <string.h>#include <stdlib.h> using namespace std;int cmp(const void *a,const void *b){ return *(int *)a-*(int *)b;}int main(){ int n; int a[10001]; while(scanf("%d",&n)!=

Java中的几种排序算法:冒泡排序,插入排序,二分法排序,简单排序,快速排序

冒泡排序: int[] hehe={4,7,2,5,6,9,0}; for(int i=0;i<hehe.length;i++){ for(int j=i+1;j<hehe.length;j++){ if(hehe[i]>hehe[j]){ int temp=hehe[i]; hehe[i]=hehe[j]; hehe[j]=temp; } } } 插入排序 int[] a={13,7,8,9,10,1,2,32}; int i,j,t,h; for (i=1;i<a.length