一个数组中的元素被分为low, medium, high三类,实现一个排序算法,将该数组按照不同的类别进行排序。规则为low < medium < high.

Example:

Input:

The input array is ['g', 'n', 'e', 'c', 't', 'z', 'o', 'f'], and the elment sorted into 3 categories as below:

low: a-h
med: i-p
high: q-z

Output:

['c', 'e', 'g', 'f', 'n', 'o', 'z', 't']

function provided listed below, and its algorithm complexityis O(n).

bool islow(char c);
bool ismed(char c);
bool ishigh(char c);

Resolution:

You can sort any array with famous sorting algorithm, like bubblesort, insertsort, quicksort and etc. However, any sorting algorithm,  based on comparation of element, can’t do better than O(nlogn) without extra memory.

The element in the array has partitioned into 3 categories, which means this array only have three different element. Therefore, You do not need to care about the ranking of element in same categories.  So, Sorting the array could use 2 partition algorithm in quicksort. First, select an element in medium category as pivot, after partition, the left side of element in low category, and the right side is mixer of medium & high. Then, select a high one, partition the latter part of array. You will get the result.  and the complexity of algorithm is O(n).

Code:

阅读全文…

INFORMATION_SCHEMA支持获取一个table中任意列的属性相关信息

http://dev.mysql.com/doc/refman/5.0/en/columns-table.html

SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT

FROM INFORMATION_SCHEMA.COLUMNS

WHERE table_name = ‘tbl_name’

[AND table_schema = ‘db_name’]

[AND column_name = ‘wild’]

可以通过上述语句获取指定列的DATA_TYPE与CHARACTER_MAXIMUM_LENGTH来确定一个列是否为varchar,以及支持的最大size

“一个tbdesc表中有一列append,类型为varchar”

mysql> SELECT DATA_TYPE,CHARACTER_MAXIMUM_LENGTH from INFORMATION_SCHEMA.COLUMNS  where table_name=’tbdesc’ and column_name = ‘append’;
+———–+————————–+
| DATA_TYPE | CHARACTER_MAXIMUM_LENGTH |
+———–+————————–+
| varchar   |                      512 |
+———–+————————–+

以下语句可以重新设置该列的maxsize

ALTER TABLE tbdesc MODIFY append varchar(2048)

,