<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Cholwich's blog &#187; C</title>
	<atom:link href="http://cholwich.org/wordpress/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://cholwich.org/wordpress</link>
	<description>Just another blog of mine</description>
	<lastBuildDate>Fri, 09 Jul 2010 09:11:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>ลองเล่น Go</title>
		<link>http://cholwich.org/wordpress/2009/11/22/%e0%b8%a5%e0%b8%ad%e0%b8%87%e0%b9%80%e0%b8%a5%e0%b9%88%e0%b8%99-go/</link>
		<comments>http://cholwich.org/wordpress/2009/11/22/%e0%b8%a5%e0%b8%ad%e0%b8%87%e0%b9%80%e0%b8%a5%e0%b9%88%e0%b8%99-go/#comments</comments>
		<pubDate>Sun, 22 Nov 2009 14:07:27 +0000</pubDate>
		<dc:creator>cholwich</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[go]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://cholwich.org/wordpress/?p=349</guid>
		<description><![CDATA[ไม่ได้เขียนอะไรใหม่ๆ ที่นี่มาเกือบครึ่งปี เพราะงานยุ่ง แล้วก็ยังเสพติด twitter และ facebook งอมแงมอีกต่างหาก วันนี้มีเวลาว่าง (จริงๆ ก็ไม่ว่างหรอก แต่ขี้เกียจทำงานที่ควรจะทำ) เลยเอา Go Programming Language ที่พัฒนาโดย Google มาลองเล่นดู เนื่องจากผมใช้ Mac OS X ซึ่งเป็นแพลตฟอร์มที่สนับสนุนอยู่แล้ว ก็เลยติดตั้งไม่ยาก แค่มี XCode อยู่ แล้วลง mercurial เพิ่ม ก็สามารถโหลดซอร์สโค้ดคอมไพเลอร์ของ Go มาคอมไพล์เองได้เลย ใช้เวลาคอมไพล์ตัวคอมไพเลอร์สั้นมาก แป๊บเดียวเสร็จ ประทับใจพอสมควร จากนั้นก็เลยลองเขียนโปรแกรมแบบมั่วๆ (เพราะยังงงกับ syntax อยู่) เพื่อแก้ปัญหา N-Queens แบบง่ายๆ (ใช้ backtracking search ด้วย recursive ดื้อๆ เอานี่แหละ) โดยใช้ Go: package main [...]]]></description>
			<content:encoded><![CDATA[<p>ไม่ได้เขียนอะไรใหม่ๆ ที่นี่มาเกือบครึ่งปี เพราะงานยุ่ง แล้วก็ยังเสพติด <a href="http://twitter.com">twitter</a> และ <a href="http://facebook.com">facebook</a> งอมแงมอีกต่างหาก วันนี้มีเวลาว่าง (จริงๆ ก็ไม่ว่างหรอก แต่ขี้เกียจทำงานที่ควรจะทำ) เลยเอา <a href="http://golang.org">Go Programming Language</a> ที่พัฒนาโดย <a href="http://google.com">Google</a> มาลองเล่นดู เนื่องจากผมใช้ Mac OS X ซึ่งเป็นแพลตฟอร์มที่สนับสนุนอยู่แล้ว ก็เลยติดตั้งไม่ยาก แค่มี XCode อยู่ แล้วลง mercurial เพิ่ม ก็สามารถโหลดซอร์สโค้ดคอมไพเลอร์ของ Go มาคอมไพล์เองได้เลย ใช้เวลาคอมไพล์ตัวคอมไพเลอร์สั้นมาก แป๊บเดียวเสร็จ ประทับใจพอสมควร</p>
<p>จากนั้นก็เลยลองเขียนโปรแกรมแบบมั่วๆ (เพราะยังงงกับ syntax อยู่) เพื่อแก้ปัญหา N-Queens แบบง่ายๆ (ใช้ backtracking search ด้วย recursive ดื้อๆ เอานี่แหละ) โดยใช้ Go:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">package main
&nbsp;
import <span style="color: #ff0000;">&quot;fmt&quot;</span>
&nbsp;
var N <span style="color: #993333;">int</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">10</span>
&nbsp;
func nqueens<span style="color: #009900;">&#40;</span>board <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #993333;">int</span><span style="color: #339933;">,</span> filled <span style="color: #993333;">int</span><span style="color: #009900;">&#41;</span> <span style="color: #993333;">int</span> <span style="color: #009900;">&#123;</span>
	var attack bool <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> filled <span style="color: #339933;">==</span> N<span style="color: #339933;">-</span><span style="color: #0000dd;">1</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">for</span> i<span style="color: #339933;">:=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>N<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span> <span style="color: #009900;">&#123;</span>
			fmt.<span style="color: #202020;">Printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%d<span style="color: #000099; font-weight: bold;">\t</span>&quot;</span><span style="color: #339933;">,</span> board<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#125;</span>
		fmt.<span style="color: #202020;">Printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #0000dd;">1</span>
	<span style="color: #009900;">&#125;</span>
	filled <span style="color: #339933;">+=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">for</span> i<span style="color: #339933;">:=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>N<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span> <span style="color: #009900;">&#123;</span>
		board<span style="color: #009900;">&#91;</span>filled<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> i<span style="color: #339933;">;</span>
		attack <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">for</span> j<span style="color: #339933;">:=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> j<span style="color: #339933;">&lt;</span>filled<span style="color: #339933;">;</span> j<span style="color: #339933;">++</span> <span style="color: #009900;">&#123;</span>
			dy <span style="color: #339933;">:=</span> board<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #339933;">-</span>board<span style="color: #009900;">&#91;</span>filled<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			dx <span style="color: #339933;">:=</span> j <span style="color: #339933;">-</span> filled<span style="color: #339933;">;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>board<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> board<span style="color: #009900;">&#91;</span>filled<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> 
			<span style="color: #009900;">&#40;</span>dy<span style="color: #339933;">/</span>dx <span style="color: #339933;">==</span> <span style="color: #0000dd;">1</span> <span style="color: #339933;">||</span> dy<span style="color: #339933;">/</span>dx <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span>dy<span style="color: #339933;">%</span>dx <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				attack <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">break</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>		
		<span style="color: #b1b100;">if</span> <span style="color: #339933;">!</span>attack <span style="color: #009900;">&#123;</span>
			nqueens<span style="color: #009900;">&#40;</span>board<span style="color: #339933;">,</span> filled<span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#125;</span>	
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
func main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	var board <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #993333;">int</span> <span style="color: #339933;">=</span> make<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #993333;">int</span><span style="color: #339933;">,</span> N<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	nqueens<span style="color: #009900;">&#40;</span>board<span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>ด้วยความอยากรู้ ก็เลยลองเอาโปรแกรมนี้เอามาแปลงเป็นภาษา C แบบตรงๆ บรรทัดต่อบรรทัด</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include&lt;stdio.h&gt;</span>
<span style="color: #339933;">#include&lt;stdlib.h&gt;</span>
&nbsp;
<span style="color: #993333;">const</span> <span style="color: #993333;">int</span> N<span style="color: #339933;">=</span><span style="color: #0000dd;">10</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">int</span> nqueens<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> <span style="color: #339933;">*</span>board<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> filled<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #993333;">int</span> i<span style="color: #339933;">,</span>j<span style="color: #339933;">,</span>attack<span style="color: #339933;">,</span>dy<span style="color: #339933;">,</span>dx<span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>filled <span style="color: #339933;">==</span> N<span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>N<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
			<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%d<span style="color: #000099; font-weight: bold;">\t</span>&quot;</span><span style="color: #339933;">,</span> board<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	filled<span style="color: #339933;">++;</span>
	<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>N<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		board<span style="color: #009900;">&#91;</span>filled<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> i<span style="color: #339933;">;</span>
		attack <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>j<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> j<span style="color: #339933;">&lt;</span>filled<span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			dy <span style="color: #339933;">=</span> board<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #339933;">-</span>board<span style="color: #009900;">&#91;</span>filled<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			dx <span style="color: #339933;">=</span> j <span style="color: #339933;">-</span> filled<span style="color: #339933;">;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>board<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> board<span style="color: #009900;">&#91;</span>filled<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span>
			<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>dy<span style="color: #339933;">/</span>dx <span style="color: #339933;">==</span> <span style="color: #0000dd;">1</span> <span style="color: #339933;">||</span> dy<span style="color: #339933;">/</span>dx <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span>dy<span style="color: #339933;">%</span>dx <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				attack <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>attack<span style="color: #009900;">&#41;</span>
			nqueens<span style="color: #009900;">&#40;</span>board<span style="color: #339933;">,</span> filled<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #993333;">int</span> <span style="color: #339933;">*</span>board <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>malloc<span style="color: #009900;">&#40;</span><span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span>N<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	nqueens<span style="color: #009900;">&#40;</span>board<span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>เสร็จแล้วก็ลองเขียน Python อีกโปรแกรมหนึ่ง</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> nqueens<span style="color: black;">&#40;</span>board, filled<span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">global</span> N
	<span style="color: #ff7700;font-weight:bold;">if</span> filled == N-<span style="color: #ff4500;">1</span>:
		<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> board:
			<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;%d<span style="color: #000099; font-weight: bold;">\t</span>&quot;</span> <span style="color: #66cc66;">%</span> board<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span>,
		<span style="color: #ff7700;font-weight:bold;">print</span>
		<span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">True</span>
	filled+=<span style="color: #ff4500;">1</span>
	<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, N<span style="color: black;">&#41;</span>:
		board<span style="color: black;">&#91;</span>filled<span style="color: black;">&#93;</span> = i
		attack = <span style="color: #008000;">False</span>
		<span style="color: #ff7700;font-weight:bold;">for</span> j <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, filled<span style="color: black;">&#41;</span>:
			dy = board<span style="color: black;">&#91;</span>j<span style="color: black;">&#93;</span>-board<span style="color: black;">&#91;</span>filled<span style="color: black;">&#93;</span>
			dx = j - filled
			<span style="color: #ff7700;font-weight:bold;">if</span> board<span style="color: black;">&#91;</span>j<span style="color: black;">&#93;</span> == board<span style="color: black;">&#91;</span>filled<span style="color: black;">&#93;</span> <span style="color: #ff7700;font-weight:bold;">or</span> <span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>dy/dx == <span style="color: #ff4500;">1</span> <span style="color: #ff7700;font-weight:bold;">or</span> dy/dx == -<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">and</span> dy<span style="color: #66cc66;">%</span>dx==<span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>:
				attack = <span style="color: #008000;">True</span>
				<span style="color: #ff7700;font-weight:bold;">break</span>
		<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> attack:
			nqueens<span style="color: black;">&#40;</span>board, filled<span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #ff4500;">0</span>
N=<span style="color: #ff4500;">10</span>
board = <span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">*</span>N
nqueens<span style="color: black;">&#40;</span>board, -<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span></pre></div></div>

<p>ทดลองรัน (ด้วย N=10) แล้วจับเวลาง่ายๆ ด้วย time ก็จะได้ผลเป็น</p>
<table>
<tr>
<th></th>
<th>Go</th>
<th>C</th>
<th>Python</th>
</tr>
<tr>
<th>real</th>
<td>0m0.165s</td>
<td>0m0.041s</td>
<td>0m1.048s</td>
</tr>
<tr>
<th>user</th>
<td>0m0.116s</td>
<td>0m0.029s</td>
<td>0m1.028s</td>
</tr>
<tr>
<th>sys</th>
<td>0m0.040s</td>
<td>0m0.002s</td>
<td>0m0.015s</td>
</tr>
</table>
<p>ประสิทธิภาพแบบคร่าวๆ ก็ถือว่าใช้ได้ทีเดียว ไม่ได้แย่กว่า C นัก แต่ไม่รู้ว่าผมชินกับภาษา C มากเกินไปหรือเปล่า ทำให้ผมรู้สึกแปลกๆ งงๆ กับ syntax ของ Go มันดูไม่ค่อยสวยงามยังไงไม่รู้ บอกไม่ถูก เหมือนแค่เอา C มาตัดบางส่วนออก เพราะกลัวจะพิมพ์เยอะเกินไป มันเลยดูขัดๆ อีกอย่าง Go ก็ไม่ได้เขียนสั้นๆ ง่ายๆ ได้เหมือน Python ผมว่าโครงสร้าง syntax ของ C กับ Python สวยงามกว่าเยอะ แต่ผมก็ยังไม่ลอง concurrent programming ที่เป็นจุดขายของ Go เหมือนกัน เอาไว้หนีงานมาลองใหม่คราวหน้าละกัน</p>
]]></content:encoded>
			<wfw:commentRss>http://cholwich.org/wordpress/2009/11/22/%e0%b8%a5%e0%b8%ad%e0%b8%87%e0%b9%80%e0%b8%a5%e0%b9%88%e0%b8%99-go/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>qsort@stdlib</title>
		<link>http://cholwich.org/wordpress/2007/06/05/qsortstdlib/</link>
		<comments>http://cholwich.org/wordpress/2007/06/05/qsortstdlib/#comments</comments>
		<pubDate>Tue, 05 Jun 2007 07:50:03 +0000</pubDate>
		<dc:creator>cholwich</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[qsort]]></category>

		<guid isPermaLink="false">http://cholwich.org/wordpress/2007/06/05/qsortstdlib/</guid>
		<description><![CDATA[ในไลบรารีมาตรฐานของภาษาซี มีฟังก์ชันสำหรับจัดการข้อมูลที่สำคัญอยู่ 2 ฟังก์ชัน คือ bsearch สำหรับการค้นหาข้อมูลแบบ binary search และ qsort สำหรับเรียงลำดับข้อมูลโดยใช้อัลกอริทึม quick sort โดยปกตินักศึกษาส่วนใหญ่มักจะไม่ใช้ เพราะมันดูเหมือนจะยุ่งยากซับซ้อน โดยเฉพาะเรื่องเกี่ยวกับ pointer วันนี้ต้องเขียนโปรแกรมเพื่อเรียงลำดับสายอักขระ เลยเอามาเขียนอธิบายวิธีการใช้ไว้หน่อย เผื่อจะเป็นประโยชน์ต่อตัวเอง และคนที่ผ่านมาเห็น void qsort&#40;void *base, size_t nmemb, size_t size, int&#40;*compar&#41;&#40;const void *, const void *&#41;&#41; จาก function prototype จะเห็นว่าฟังก์ชันนี้ต้องการ argument 4 ตัว คือ base เป็น pointer ที่ชี้ไปยังอะเรย์ที่ใช้เก็บข้อมูลทั้งหมด nmemb ใช้ระบุจำนวนสมาชิกของอะเรย์ที่ต้องการเรียงลำดับ size กำหนดขนาดของข้อมูลแต่ละชุด ซึ่งจำเป็นต้องใช้เวลาย้ายข้อมูลสลับตำแหน่งกัน compar เป็น pointer [...]]]></description>
			<content:encoded><![CDATA[<p>ในไลบรารีมาตรฐานของภาษาซี มีฟังก์ชันสำหรับจัดการข้อมูลที่สำคัญอยู่ 2 ฟังก์ชัน คือ bsearch สำหรับการค้นหาข้อมูลแบบ binary search และ qsort สำหรับเรียงลำดับข้อมูลโดยใช้อัลกอริทึม quick sort โดยปกตินักศึกษาส่วนใหญ่มักจะไม่ใช้ เพราะมันดูเหมือนจะยุ่งยากซับซ้อน โดยเฉพาะเรื่องเกี่ยวกับ pointer วันนี้ต้องเขียนโปรแกรมเพื่อเรียงลำดับสายอักขระ เลยเอามาเขียนอธิบายวิธีการใช้ไว้หน่อย เผื่อจะเป็นประโยชน์ต่อตัวเอง และคนที่ผ่านมาเห็น</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span> qsort<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span> <span style="color: #339933;">*</span>base<span style="color: #339933;">,</span> size_t nmemb<span style="color: #339933;">,</span> size_t size<span style="color: #339933;">,</span> <span style="color: #993333;">int</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>compar<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">const</span> <span style="color: #993333;">void</span> <span style="color: #339933;">*,</span> <span style="color: #993333;">const</span> <span style="color: #993333;">void</span> <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>จาก function prototype จะเห็นว่าฟังก์ชันนี้ต้องการ argument 4 ตัว คือ</p>
<ol>
<li>base เป็น pointer ที่ชี้ไปยังอะเรย์ที่ใช้เก็บข้อมูลทั้งหมด</li>
<li>nmemb ใช้ระบุจำนวนสมาชิกของอะเรย์ที่ต้องการเรียงลำดับ</li>
<li>size กำหนดขนาดของข้อมูลแต่ละชุด ซึ่งจำเป็นต้องใช้เวลาย้ายข้อมูลสลับตำแหน่งกัน</li>
<li>compar เป็น pointer ที่ชี้ไปยังฟังก์ชัน เพื่อเปรียบเทียบสมาชิกแต่ละตัวในอะเรย์ ฟังก์ชันนี้ควรจะให้ค่าน้อยกว่าศูนย์ เท่ากับศูนย์ และมากกว่าศูนย์ เช่นเดียวกับฟังก์ชัน strcmp</li>
</ol>
<p><strong>ตัวอย่าง</strong> ผมต้องการเรียงลำดับสตริง ซึ่งในภาษาซีจะใช้ char * ดังนั้นผมจึงสร้างตัวแปรแบบ char ** เพื่อเป็นอะเรย์สำหรับเก็บ pointer ที่ชี้ไปยังสตริงแต่ละตัว ส่วน nmemb ก็คือจำนวนสมาชิกของอะเรย์ กับ size ก็คือ sizeof(char) หาได้ไม่ยาก ปัญหาหลักอยู่ที่ฟังก์ชันสำหรับเปรียบเทียบข้อมูล ซึ่งจะเห็นว่าเรามีฟังก์ชัน strcmp สำหรับเปรียบเทียบสตริงอยู่แล้ว เพียงแต่ prototype อาจจะไม่เหมือนกัน เลยต้องทำฟังก์ชันขึ้นมาห่อไว้หน่อย</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">int</span> compare<span style="color: #009900;">&#40;</span><span style="color: #993333;">const</span> <span style="color: #993333;">void</span> <span style="color: #339933;">*</span>a<span style="color: #339933;">,</span> <span style="color: #993333;">const</span> <span style="color: #993333;">void</span> <span style="color: #339933;">*</span>b<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">const</span> <span style="color: #993333;">char</span> <span style="color: #339933;">**</span>pa <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">const</span> <span style="color: #993333;">char</span> <span style="color: #339933;">**</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">const</span> <span style="color: #993333;">char</span> <span style="color: #339933;">**</span>pb <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">const</span> <span style="color: #993333;">char</span> <span style="color: #339933;">**</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>b<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> strcmp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>pa<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>pb<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>อ้อ เวลาฟังก์ชัน qsort เรียก compar มาทำงาน จะเรียกโดยส่ง pointer ที่ชี้ไปยังสมาชิก 2 ตัวในอะเรย์ไปให้ ฉะนั้นจึงต้องมา cast ให้เป็น char ** ก่อน แล้วถึงจะไปเรียกใช้ strcmp ได้ ส่วนการเรียกฟังก์ชัน qsort ก็แค่</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">qsort<span style="color: #009900;">&#40;</span>a<span style="color: #339933;">,</span> n<span style="color: #339933;">,</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span> <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> compare<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>สุดท้ายที่อยากฝากไว้ก็คือ &#8220;don&#8217;t reinvent the wheel&#8221; เราควรจะใช้สิ่งที่มีอยู่แล้วให้เป็นประโยชน์ให้มากที่สุด โดยเฉพาะอย่างยิ่งฟังก์ชันมาตรฐานต่างๆ เพราะฟังก์ชันเหล่านี้ได้รับการพัฒนามายาวนาน ผ่านร้อนผ่านหนาวมาเยอะ น่าจะทำงานได้ดีกว่าการเขียนฟังก์ชันขึ้นมาใช้งานเอง แต่ทั้งนี้ไม่ได้บอกว่าไม่ควรทำเองนะ เพียงแต่พยายามปรับใช้สิ่งต่างๆ ที่มีอยู่ให้ดี เพื่อจะได้มีเวลาไปทำให้สิ่งใหม่ๆ</p>
<p>อ้างอิง: man qsort</p>
]]></content:encoded>
			<wfw:commentRss>http://cholwich.org/wordpress/2007/06/05/qsortstdlib/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
