Given a unsorted array with integers, find the median of it.

A median is the middle number of the array after it is sorted.

If there are even numbers in the array, return the N/2-th number after sorted.

思路:

找个sort方法sort完后即可

最简单的就是bubble sort

但是bubble sort时间复杂度太大,所以改用quicksort

/* This function takes last element as pivot, places
the pivot element at its correct position in sorted
array, and places all smaller (smaller than pivot)
to left of pivot and all greater elements to right
of pivot */
int partition (int arr[], int low, int high)
{
int pivot = arr[high]; // pivot
int i = (low - ); // Index of smaller element for (int j = low; j <= high- ; j++)
{
// If current element is smaller than or
// equal to pivot
if (arr[j] <= pivot)
{
i++; // increment index of smaller element
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + ], &arr[high]);
return (i + );
} /* The main function that implements QuickSort
arr[] --> Array to be sorted,
low --> Starting index,
high --> Ending index */
void quickSort(int arr[], int low, int high)
{
if (low < high)
{
/* pi is partitioning index, arr[p] is now
at right place */
int pi = partition(arr, low, high); // Separately sort elements before
// partition and after partition
quickSort(arr, low, pi - );
quickSort(arr, pi + , high);
}
}

[LintCode笔记了解一下]80.Median的更多相关文章

  1. LintCode笔记 - 8. 旋转字符串

    这一题相对简单,但是代码质量可能不是很好,我分享一下我的做题笔记以及做题过程给各位欣赏,有什么不足望各位大佬指出来 原题目,各位小伙伴也可以试着做一下 . 旋转字符串 中文English 给定一个字符 ...

  2. LintCode笔记 - 145.大小写转换 - 极简之道 - 最短代码

    这道题目一眼就能看出是送分题,当然在这里也不谈高难度的实现逻辑,肯定有同学会想直接用自带函数实现不就可以了吗? 对的,就是这么简单,然而今天的重点是如何把代码简写到最短. 本文章将带你把代码长度从 一 ...

  3. LintCode笔记 - 82.落单的数

    这一题相对简单,但是代码质量可能不是很好,我分享一下我的做题笔记以及做题过程给各位欣赏,有什么不足望各位大佬指出来 原题目,各位小伙伴也可以试着做一下 . 落单的数 中文English 给出 * n ...

  4. [LintCode笔记了解一下]64.合并排序数组

    Given two sorted integer arrays A and B, merge B into A as one sorted array. 思路: 因为A的后面的部分都是空的留出来给我们 ...

  5. [LintCode笔记了解一下]44.Minimum Subarray

    这道题和max subarray很类似,我用local 和 global 的dp方式阔以解决这道 那么我们来看动态规划的四个要素分别是什么? State: localmin[i] 表示以当前第i个数最 ...

  6. [LintCode笔记了解一下]41.Maximum Subarray

    Given an array of integers, find a contiguous subarray which has the largest sum. 首先 當題目涉及到求最大最小值時,最 ...

  7. [LintCode笔记了解一下]39.恢复旋转排序数组

    思路: 1.需要O(n)的事件复杂度,所以多次循环不考虑 2.四步翻转法 -第一步,找到数组里最小的那个数字,因为是旋转排序数组,所以只要找到某个位置arr[i]>arr[i+1]的话,就找到了 ...

  8. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  9. 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)

    注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...

随机推荐

  1. Spring中application*的使用

    ApplicationAware 加载Spring配置文件时,如果Spring配置文件中所定义的Bean类实现了ApplicationContextAware 接口,那么在加载Spring配置文件时, ...

  2. 转 :关于springmvc使用拦截器

    原博客: http://elim.iteye.com/blog/1750680 SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的 ...

  3. maven中的pom配置文件标签的详细介绍

    <span style="padding:0px; margin:0px"><project xmlns="http://maven.apache.or ...

  4. 关于ie6中绝对定位或浮动的div中既有向左float也有向右float时候如何让外层div自适应宽度的解决方案--

    一个详细的说明请见: http://www.cnblogs.com/yiyang/p/3265006.html 我的问题大约为,如下代码: <!DOCTYPE html PUBLIC " ...

  5. requirejs——config

    config 函数用于requirejs的配置信息.主要包括:baseUrl.paths: 一.baseUrl: 待续

  6. 前端使用CryptoJs类库进行sha-256、MD5加密

    Google的加密库 CryptoJs(点此下载) 包含了很多常用的加解密方式,包括AES.DES.SHA-1.SHA-2.SHA256.MD5等. DES对称加密在之前的文章中也有介绍过,大传送门. ...

  7. linux shell find

     find /  -ctime +365 ! -name "*.bat" -exce rm -rf {} \;

  8. iPhone开发随想:rand()还是arc4random()

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://bj007.blog.51cto.com/1701577/544006 今天在iP ...

  9. Core 第三组 结对作业——四则运算 Part1. Core代码编写

    结对作业——四则运算 Part1. Core代码编写 PB15061303 刘梓轩PB16061489 艾寅中 GITHUB 地址 戳这里 目录 (因为内容较多,分为了三个部分,但作业系统中只能提交一 ...

  10. Visual studio 2015/2017 opencv3.4 kineck2.0 osg realsense配置

    ----------------------------------------------包含目录-------------------------------------------------- ...