本文準(zhǔn)備講解1個簡單的算法編程問題, 這個算法編程問題來自LintCode平臺爷速。不了解.LintCode平臺的讀者可以閱讀筆者文章(在線編程平臺推薦-LeetCode)央星。問題的英文版本描述如下:
Median
Given an 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.
Example
Given [4, 5, 1, 2, 3], return 3.
Given [7, 9, 4, 5], return 5.
中位數(shù)
給定一個未排序的整數(shù)數(shù)組,找到其中位數(shù)惫东。
如果數(shù)組的元素個數(shù)是偶數(shù)莉给,則返回排序后數(shù)組的第N/2個數(shù)。
樣例
給出數(shù)組[4, 5, 1, 2, 3]廉沮, 返回 3
給出數(shù)組[7, 9, 4, 5]禁谦,返回 5
該問題要求先對整數(shù)數(shù)組做排序處理,然后找到整數(shù)序列的中位數(shù)废封。所以核心問題只有1個:如何對整數(shù)數(shù)組排序處理≈莶矗現(xiàn)在公布1種簡單的算法方案。
簡單的算法