当k比较小的时候,可获得O(n)的时间复杂度.
#include
<
iostream
>
using namespace std;
![](/Images/OutliningIndicators/None.gif)
const
int
arrLen
=
5
;
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
void
countingSort(
int
*
a,
int
*
b,
int
k)
{
//
a数组元素在[0..k]
int
i, j;
int
*
c
=
new
int
[k
+
1
];
for
(i
=
0
; i
<=
k; i
++
) c[i]
=
0
;
for
(i
=
0
; i
<
arrLen; i
++
) c[a[i]]
++
;
//
包含小于或等于i的元素个数
for
(i
=
1
; i
<=
k; i
++
) c[i]
+=
c[i
-
1
];
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for
(j
=
arrLen
-
1
; j
>=
0
; j
--
)
{
b[c[a[j]]
-
1
]
=
a[j];
c[a[j]]
--
;
}
}
int
main()
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
int
a[arrLen]
=
{
4
,
4
,
3
,
1
,
1
}
;
int
b[arrLen];
countingSort(a, b,
5
);
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for
(
int
i
=
0
; i
<
5
; i
++
)
{
printf(
"
%d
"
, b[i]);
}
printf(
"
\n
"
);
system(
"
pause
"
);
}
posted @
2007-02-06 20:43 豪 阅读(679) |
评论 (2) |
编辑 收藏
创建http_request对象
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
function createAjaxObj()
{
var http_request = false;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (window.XMLHttpRequest)
{ // Mozilla, Safari,![](/Images/dot.gif)
http_request = new XMLHttpRequest();
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (http_request.overrideMimeType)
{
http_request.overrideMimeType('text/xml');
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} else if (window.ActiveXObject)
{ // IE
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
http_request = new ActiveXObject("Msxml2.XMLHTTP");
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (e)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
http_request = new ActiveXObject("Microsoft.XMLHTTP");
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (e)
{}
}
}
return http_request;
}http_request对象的使用
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
function ajax_add(obj)
{
var http_request = createAjaxObj();
var feedURL = obj.feedURL;
var feedName = obj.feedName;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
http_request.onreadystatechange = function()
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (http_request.readyState == 4)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (http_request.status == 200)
{
//接收服务器返回信息responseText或responseXML
var text_data = http_request.responseText;
var parentNode = document.getElementById("leftcolumn");
var newDiv = document.createElement("div");
newDiv.innerHTML = unescape(text_data);
parentNode.appendChild(newDiv);
waiting.innerHTML = "";
}
}
}
var submitURL = "add_rss.php";
http_request.open("POST", submitURL, true);
http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
http_request.send("feedURL="+feedURL.value+"&feedName="+escape(feedName.value));
var waiting = document.getElementById("waiting");
waiting.innerHTML = "正在载入
";
//http_request.send(null);
}
posted @
2007-02-05 19:54 豪 阅读(838) |
评论 (0) |
编辑 收藏
循环不变式
循环不变式主要用来帮助我们理解算法的正确性。
初始化:它在循环的第一轮迭代开始之前,应该是正确的。
保持:如果在循环的某一次迭代开始之前它是正确,那么,在下一次迭代开始之前,它也应该保持正确。
终止:当循环结束时,不变式给了我们一个有用的性质,它有助于表明算法是正确的。
posted @
2007-02-05 19:43 豪 阅读(391) |
评论 (0) |
编辑 收藏
没办法,近来靠php吃饭-_-
<?
/*************
| +-------------------------------------------------
| Id:
| +-------------------------------------------------
| Copyright (c)
| Author:Howie
| Email: qywyh_scut@163.com
| +-------------------------------------------------
| Create Date: 2007-2-3
| Modify Date:
| Note:
| demo:
| $objRSS = new loadRSS('http://news.163.com/special/00011K6L/rss_gn.xml', 10, 'cache/test.xml');
| echo $objRSS->getRSS();
|
| +-------------------------------------------------
***************/
class loadRSS {
//RSS url链接
var $_url = '';
![](/Images/OutliningIndicators/None.gif)
//RSS 缓存目录
var $_cacheDir = '';
![](/Images/OutliningIndicators/None.gif)
//缓存时间
var $_cacheTime = '';
![](/Images/OutliningIndicators/None.gif)
//是否从缓存读取
var $_loadFromCache = true;
![](/Images/OutliningIndicators/None.gif)
//保存rss字符串
var $_str = '';
![](/Images/OutliningIndicators/None.gif)
function loadRSS ($url, $cacheTime=0, $cacheDir=''){
$this->_url = $url;
if ($cacheDir == '' || $cacheTime == 0) {
$this->_loadFromCache = false;
} else {
$this->_cacheTime = $cacheTime;
$this->_cacheDir = $cacheDir;
}
}
![](/Images/OutliningIndicators/None.gif)
function getRSS () {
if (!$this->_loadFromCache) {
$fp = fopen($this->_url, 'r');
while ($tmp = fgets($fp, 4096)) {
$this->_str .= $tmp;
}
fclose($fp);
//echo $this->_str;
} else {
$fileModifyTime = @filemtime($this->_cacheDir);
$nowTime = time();
//修改缓存文件
if (!$fileModifyTime || $nowTime-$fileModifyTime >= $this->_cacheTime) {
$fp = fopen($this->_url, 'r');
$fp2 = fopen($this->_cacheDir, 'w');
while ($tmp = fgets($fp, 4096)) {
fwrite($fp2, $tmp);
$this->_str .= $tmp;
}
fclose($fp);
fclose($fp2);
return $this->_str;
}
$fp = fopen($this->_cacheDir, 'r');
while ($tmp = fgets($fp, 4096)) {
$this->_str .= $tmp;
}
fclose($fp);
}
return $this->_str;
}
![](/Images/OutliningIndicators/None.gif)
}
?>
posted @
2007-02-03 01:32 豪 阅读(347) |
评论 (0) |
编辑 收藏
以后不想在这里写心情了,这里就只记录一些技术性文章吧~
posted @
2007-01-16 01:30 豪 阅读(153) |
评论 (0) |
编辑 收藏