2008年5月20日

排序算法

選擇排序
將要排序的對象分作兩部份,一個是已排序的,一個是未排序的,從後端未排序部份選擇一個最小值,並放入前端已排序部份的最後一個,例如:

排序前:70 80 31 37 10 1 48 60 33 80

[1] 80 31 37 10 70 48 60 33 80 選出最小值1
[1 10] 31 37 80 70 48 60 33 80 選出最小值10
[1 10 31] 37 80 70 48 60 33 80 選出最小值31
[1 10 31 33] 80 70 48 60 37 80 ......
[1 10 31 33 37] 70 48 60 80 80 ......
[1 10 31 33 37 48] 70 60 80 80 ......
[1 10 31 33 37 48 60] 70 80 80 ......
[1 10 31 33 37 48 60 70] 80 80 ......
[1 10 31 33 37 48 60 70 80] 80 ......




插入排序
像是玩樸克一樣,我們將牌分作兩堆,每次從後面一堆的牌抽出最前端的牌,然後插入前面一堆牌的適當位置,例如:

排序前:92 77 67 8 6 84 55 85 43 67


[77 92] 67 8 6 84 55 85 43 67 將77插入92前
[67 77 92] 8 6 84 55 85 43 67 將67插入77前
[8 67 77 92] 6 84 55 85 43 67 將8插入67前
[6 8 67 77 92] 84 55 85 43 67 將6插入8前
[6 8 67 77 84 92] 55 85 43 67 將84插入92前
[6 8 55 67 77 84 92] 85 43 67 將55插入67前
[6 8 55 67 77 84 85 92] 43 67 ......
[6 8 43 55 67 77 84 85 92] 67 ......
[6 8 43 55 67 67 77 84 85 92] ......


氣泡排序法
顧名思義,就是排序時,最大的元素會如同氣泡一樣移至右端,其利用比較相鄰元素的方法,將大的元素交換至右端,所以大的元素會不斷的往右移動,直到適當的位置為止。


基本的氣泡排序法可以利用旗標的方式稍微減少一些比較的時間,當尋訪完陣列後都沒有發生任何的交換動作,表示排序已經完成,而無需再進行之後的迴圈比較與交換動作,例如:

排序前:95 27 90 49 80 58 6 9 18 50


27 90 49 80 58 6 9 18 50 [95] 95浮出
27 49 80 58 6 9 18 50 [90 95] 90浮出
27 49 58 6 9 18 50 [80 90 95] 80浮出
27 49 6 9 18 50 [58 80 90 95] ......
27 6 9 18 49 [50 58 80 90 95] ......
6 9 18 27 [49 50 58 80 90 95] ......
6 9 18 [27 49 50 58 80 90 95] 由於接下來不會再發生交換動作,排序提早結束


展开 Read More...

2008年5月15日

Apache2 VirtualHost 403 error

Apache2 VirtualHost 403 error
From Noah.org


error 403 with virtual hosts
Working with virtual hosts under Apache2 is pretty easy, but I had some trouble getting things started due to some unclear docs and lack of examples. My biggest problem was the "HTTP 403 / client denied by server configuration error".

There are two causes to 403 errors with virtual hosts.

First, every single parent path to the virtual document root must be Readable, Writable, and Executable by the web server httpd user. The access_log will show a 403 code, but the following message is returned to the browser with no "403" string printed:

Forbidden
You don't have permission to access /index.html on this server.


I got nailed a couple of times because one of the parent directories in the virtual document root was not executable by 'www' (the user my web server runs as). The error log file messages offer no hints when this happens. It makes it seem like a configuration problem, so you can waste a lot of time look for the problem in httpd.conf.

The second cause is actually a configuration problem -- the problem is forgetting to allow access in the httpd.conf. In this case the access_log will show a 403 error and Aapche2 will also sometimes send a "403" in the error string to the browser:

HTTP 403 / client denied by server configuration error


It may also send the Forbidden message with no "403" string. I don't know what this difference means.

Forbidden
You don't have permission to access /index.html on this server.

A tail of the error_log gives a message like this for each access attempt:

[Tue Jul 25 17:58:17 2006] [error] [client 192.168.1.1] client denied by server
configuration: /var/www/vhosts/palermo/

The problem is that the extra/httpd-vhosts.conf is missing the directive to allow access to the directory.

Allow access by adding a <directory> section inside the <vhost> section.

<directory /vhost_document_root>
allow from all
<directory>

The following should give a better idea of how this should work:

<VirtualHost *>
ServerName palermo.example.com
ServerAlias palermo.example.com
DocumentRoot /var/www/vhosts/palermo
<directory /var/www/vhosts/palermo>
allow from all
</directory>
</VirtualHost>

It is strange that neither the sample httpd-vhosts.conf file that comes with Apache2 nor the Apache2 documentation on VirtualHost gives a example that could work.

Retrieved from "http://www.noah.org/wiki/Apache2_VirtualHost_403_error"

展开 Read More...