<?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>那些日子里</title>
	<atom:link href="http://www.thosedays.us/feed" rel="self" type="application/rss+xml" />
	<link>http://www.thosedays.us</link>
	<description>记录生活的点滴</description>
	<lastBuildDate>Sun, 01 Apr 2012 14:54:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>.bash_profile vs .bashrc</title>
		<link>http://www.thosedays.us/bash_profile-vs-bashrc/291.html</link>
		<comments>http://www.thosedays.us/bash_profile-vs-bashrc/291.html#comments</comments>
		<pubDate>Sun, 01 Apr 2012 13:40:36 +0000</pubDate>
		<dc:creator>huotuotuofly</dc:creator>
				<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://www.thosedays.us/?p=291</guid>
		<description><![CDATA[by Josh Staiger[1]

&#160;

使用Linux，Unix和Mac OS X的时候，当需要给shell配置PATH或者其他环境变量的时候我总是忘记应该修改那个bash配置文件。是编辑主目录中的.bash_profile还是.bashrc[2]呢？可以将配置信息写在其中的任意一个中，如果文件不存在你也可以直接创建一个。但是为什么有两个不同的文件呢？区别是什么？根据bash man帮助信息,.bash_profile用于login shells，而.bashrc是使用交互式的non-login shell时才被执行。什么是login或者non-login shell呢？

<span class="readmore"><a href="http://www.thosedays.us/bash_profile-vs-bashrc/291.html" title=".bash_profile vs .bashrc">阅读全文——共1031字</a></span>]]></description>
			<content:encoded><![CDATA[<p>by Josh Staiger[1]</p>
<p>&nbsp;</p>
<p><span style="color: #000000;">使用</span>Linux，Unix和Mac OS X的时候，当需要给shell配置PATH或者其他环境变量的时候我总是忘记应该修改那个bash配置文件。是编辑主目录中的.bash_profile还是.bashrc[2]呢？可以将配置信息写在其中的任意一个中，如果文件不存在你也可以直接创建一个。但是为什么有两个不同的文件呢？区别是什么？根据bash man帮助信息,.bash_profile用于login shells，而.bashrc是使用交互式的non-login shell时才被执行。什么是login或者non-login shell呢？</p>
<p><span id="more-291"></span></p>
<p>当你在终端上使用用户名密码登录，不论是在本机或者使用ssh连接远程机器。在初始命令提示符出现之前.bash_profile会被执行以配置shell环境。但是如果你已经登陆了机器，之后从Gnome或是KDE中打开一个伪终端（xtem），提示符出现之前.bashrc会被执行。当你在终端通过输入/bin/bash创建一个新的bash实例时.bashrc也会被执行。那为什么两个不同的文件呢？</p>
<p>&nbsp;</p>
<p>假设，你想在每次登录的时候打印一些冗长的诊断信息（平均负载，内存使用，当前用户，等信息）。你只想在登陆的时候看见，所以就将命令放在.bash_profile里面。如果放在.bashrc里，每次代开终端都会看到这些信息。</p>
<p>&nbsp;</p>
<p>Mac OS X 例外</p>
<p>Mac OS X的终端应用是个特例。每个新的终端窗口会默认调用一个登录shell，这个shell会执行.bash_profile而不是.bashrc.其他的一些终端模拟器可能也是这么做的，但这并不推荐。</p>
<p>&nbsp;</p>
<p>建议</p>
<p>多数情况你不想维护两个login和non-login shell的配置文件。当你设置PATH的时候，你想的是应用于这两种情况。可以只在.bashrc中设置PATH，然后在.bash_profile中调用.bashrc来达到目的。</p>
<p>&nbsp;</p>
<p>在.bash_profile中添加代码：</p>
<pre>

if [ -f ~/.bashrc ]; then

source ~/.bashrc

fi
</pre>
<p>这样当你从终端登录机器的时候.bashrc也会被调用。</p>
<p>&nbsp;</p>
<p>俺的话：</p>
<p>[1] 翻译自<a title="http://www.joshstaiger.org/archives/2005/07/bash_profile_vs.html" href="http://www.joshstaiger.org/archives/2005/07/bash_profile_vs.html">http://www.joshstaiger.org/archives/2005/07/bash_profile_vs.html</a></p>
<p>[2] .bashrc不存在也有可能是因为当前使用的shell不是bash，使用命令echo $0查看当前shell类型。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thosedays.us/bash_profile-vs-bashrc/291.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Use Groovy</title>
		<link>http://www.thosedays.us/how-to-use-groovy/303.html</link>
		<comments>http://www.thosedays.us/how-to-use-groovy/303.html#comments</comments>
		<pubDate>Fri, 30 Mar 2012 07:31:39 +0000</pubDate>
		<dc:creator>huotuotuofly</dc:creator>
				<category><![CDATA[未分类]]></category>

		<guid isPermaLink="false">http://www.thosedays.us/?p=303</guid>
		<description><![CDATA[Groovy

1、 What is groovy



<span class="readmore"><a href="http://www.thosedays.us/how-to-use-groovy/303.html" title="How to Use Groovy">阅读全文——共1801字</a></span>]]></description>
			<content:encoded><![CDATA[<p>Groovy</p>
<p>1、 What is groovy</p>
<ul>
<li>“is an agile and <strong>dynamic language</strong> for the <strong>Java Virtual Machine</strong></li>
<li>builds upon the strengths of Java but has <strong>additional power features</strong> inspired by languages like Python, Ruby and Smalltalk</li>
</ul>
<p>…”</p>
<p><span id="more-303"></span></p>
<p>The grammar is very similar with Java.</p>
<p>See more in  <a href="http://groovy.codehaus.org/">http://groovy.codehaus.org/</a></p>
<p>2、 Develop Environment</p>
<p>2.1 JDK 1.5 above</p>
<p>2.2 eclipse+groovy plugin (support Groovy 1.5.7)</p>
<p>3、 Use groovy in Eclipse</p>
<p>Install the plug-in in Eclipse with url <a href="http://dist.codehaus.org/groovy/distributions/update/">http://dist.codehaus.org/groovy/distributions/update/</a></p>
<p><img class="alignnone" src="http://www.thosedays.us/wp-content/uploads/2012/03/groovy1.bmp" alt="" width="407" height="287" /></p>
<p>And select the site to install it.</p>
<p>If there are some dependency errors, please install the patch with url  <a href="http://beust.com/eclipse/">http://beust.com/eclipse/</a></p>
<p>error info. ，</p>
<p>Cannot complete the request. See the details.<br />
Unsatisfied dependency: [org.codehaus.groovy.eclipse.testng.feature.feature.group 1.5.7.20081120_2330] requiredCapability: org.eclipse.equinox.p2.iu/org.testng.eclipse/0.0.0<br />
Unsatisfied dependency: [org.codehaus.groovy.eclipse.testng 1.5.7.20081120_2330] requiredCapability: osgi.bundle/org.testng.eclipse/0.0.0<br />
Unsatisfied dependency: [org.codehaus.groovy.eclipse.testng.feature.feature.group 1.5.7.20081120_2330] requiredCapability: org.eclipse.equinox.p2.iu/org.testng.eclipse/0.0.0<br />
Unsatisfied dependency: [org.codehaus.groovy.eclipse.testng.feature.feature.group 1.5.7.20081120_2330] requiredCapability:</p>
<p>&nbsp;</p>
<p><img class="alignnone" src="http://www.thosedays.us/wp-content/uploads/2012/03/groovy2.bmp" alt="" width="419" height="228" /></p>
<p>Restart the Eclipse.</p>
<p>4、 Create groovy project</p>
<p>4.1 Create a Groovy project</p>
<p>New-&gt;Project-&gt;Java Project to create Java project</p>
<p>4.2 Add Groovy feature</p>
<p>Right click the project, Groovy-&gt;Add Groovy Nature.  Add Groovy Libraries to the project.</p>
<p><img class="alignnone" src="http://www.thosedays.us/wp-content/uploads/2012/03/groovy31.bmp" alt="" width="438" height="129" /></p>
<p>4.3 Create Groovy class</p>
<p>Right click the project, New -&gt;Other -&gt;Groovy-&gt;Groovy Class</p>
<p>&nbsp;</p>
<p><img class="alignnone" src="http://www.thosedays.us/wp-content/uploads/2012/03/groovy3.bmp" alt="" width="458" height="376" /></p>
<p>Auto-generate the code as follows:</p>
<pre>
/**
* @author Administrator
*
*/
public class HelloWorld{

/**
* @param args
*/
public static void main(def args){
// TODO Auto-generated method stub
}

}
</pre>
<p>4.4   Write Code</p>
<p>Write code in the main method like ”println(“hello world ”);”</p>
<p>4.5   Compile and run the groovy class</p>
<p>Right click the source file and click “Compile Groovy File”.</p>
<p>After that click Run As-&gt; Groovy</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thosedays.us/how-to-use-groovy/303.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Linux下Firefox is already running, but not responding解决方法。</title>
		<link>http://www.thosedays.us/linux%e4%b8%8bfirefox-is-already-running-but-not-responding%e8%a7%a3%e5%86%b3%e6%96%b9%e6%b3%95%e3%80%82/281.html</link>
		<comments>http://www.thosedays.us/linux%e4%b8%8bfirefox-is-already-running-but-not-responding%e8%a7%a3%e5%86%b3%e6%96%b9%e6%b3%95%e3%80%82/281.html#comments</comments>
		<pubDate>Wed, 07 Mar 2012 15:24:12 +0000</pubDate>
		<dc:creator>huotuotuofly</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[学习]]></category>

		<guid isPermaLink="false">http://www.thosedays.us/?p=281</guid>
		<description><![CDATA[该问题出现主要是由于Linux多用户登录，或者用户异地登录出现死锁导致的。

Windows下参考 这里

Linux下当重启电脑不能解决时可以使用如下命令，删除主目录下的几个文件

<span class="readmore"><a href="http://www.thosedays.us/linux%e4%b8%8bfirefox-is-already-running-but-not-responding%e8%a7%a3%e5%86%b3%e6%96%b9%e6%b3%95%e3%80%82/281.html" title="Linux下Firefox is already running, but not responding解决方法。">阅读全文——共688字</a></span>]]></description>
			<content:encoded><![CDATA[<p>该问题出现主要是由于Linux多用户登录，或者用户异地登录出现死锁导致的。<br />
Windows下参考 <a href="http://support.mozilla.org/en-US/kb/Firefox%20is%20already%20running%20but%20is%20not%20responding" title="这里" target="_blank">这里</a></p>
<p>Linux下当重启电脑不能解决时可以使用如下命令，删除主目录下的几个文件</p>
<pre>

rm -f `find $HOME/.Mozilla -name lock`
rm -f `find $HOME/.Mozilla -name sessionstore.js`
rm -f `find $HOME/.Mozilla -name .parentlock`
</pre>
<p>进一步的，可以重命名/usr/lib/firefox/firefox-bin 文件为/usr/lib/firefox/firefox-bin.org，修改/usr/lib/firefox/firefox-bin为：</p>
<pre>
#!/bin/bash
# /usr/lib/firefox/firefox-bin script
if ! [ `id -u` = 0 ]
then
chmod -R 777 $HOME/.mozilla/
find $HOME/.mozilla -name lock &gt; /dev/null 2&gt;&amp;1
if [ $? = 0 ]
then
rm -f `find $HOME/.mozilla -name lock` 2&gt; /dev/null
rm -f `find $HOME/.mozilla -name sessionstore.js` 2&gt; /dev/null
rm -f `find $HOME/.mozilla -name .parentlock` 2&gt; /dev/null
fi
fi

/usr/lib/firefox/firefox-bin.org $*
</pre>
<p>使得此问题再次出现时，能在后台自动解决。</p>
<p><a title="REFERENCE" href="http://www.linuxquestions.org/questions/mandriva-30/firefox-is-already-running-but-not-responding-bug-635051/page5.html" target="_blank">Reference</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.thosedays.us/linux%e4%b8%8bfirefox-is-already-running-but-not-responding%e8%a7%a3%e5%86%b3%e6%96%b9%e6%b3%95%e3%80%82/281.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>test</title>
		<link>http://www.thosedays.us/test/278.html</link>
		<comments>http://www.thosedays.us/test/278.html#comments</comments>
		<pubDate>Wed, 15 Jun 2011 18:11:18 +0000</pubDate>
		<dc:creator>huotuotuofly</dc:creator>
				<category><![CDATA[未分类]]></category>

		<guid isPermaLink="false">http://www.thosedays.us/?p=278</guid>
		<description><![CDATA[

]]></description>
			<content:encoded><![CDATA[<p><img alt="" src="http://www.beautifulphoto.net/upload/201106152038068386.jpg" class="alignnone" width="640" height="480" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.thosedays.us/test/278.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>数据库存储过程的使用和delimiter用法</title>
		<link>http://www.thosedays.us/sqlprocedureanddelimiter/268.html</link>
		<comments>http://www.thosedays.us/sqlprocedureanddelimiter/268.html#comments</comments>
		<pubDate>Sat, 21 May 2011 17:57:12 +0000</pubDate>
		<dc:creator>huotuotuofly</dc:creator>
				<category><![CDATA[学习]]></category>

		<guid isPermaLink="false">http://www.thosedays.us/%e6%95%b0%e6%8d%ae%e5%ba%93%e5%ad%98%e5%82%a8%e8%bf%87%e7%a8%8b%e7%9a%84%e4%bd%bf%e7%94%a8%e5%92%8cdelimiter%e7%94%a8%e6%b3%95/268.html</guid>
		<description><![CDATA[刚刚遇到sql存储过程的使用，老忘东西那就速度记下来。



1、创建一个简单的数据库添加过程。

<span class="readmore"><a href="http://www.thosedays.us/sqlprocedureanddelimiter/268.html" title="数据库存储过程的使用和delimiter用法">阅读全文——共462字</a></span>]]></description>
			<content:encoded><![CDATA[<p>刚刚遇到sql存储过程的使用，老忘东西那就速度记下来。</p>
<p><span id="more-268"></span></p>
<p>1、创建一个简单的数据库添加过程。</p>
<pre>

MySQL&gt; delimiter //

-&gt;create procedure mytest.insertIN(IN param1 int)

-&gt;begin

-&gt;insert into test values(param1);

-&gt;end

-&gt;//

MySQL&gt; delimiter ;
</pre>
<p>说明：a、mytest为一个数据库，内有一个简单的表test,表只包含一个字段id(int)。</p>
<p>b、procedure为关键字，IN（或OUT）表示参数param1是传入（或传出）参数。</p>
<p>c、参数传给CallableStatement对象是通过setXXX方法完成的。</p>
<p>d、delimiter用来定义命令的结束符。delimiter // 是将结束符设为// ，delimiter ;是将结束符重置为sql默认的结束符。设置结束符是因为过程体中用到默认结束符。</p>
<p>&nbsp;</p>
<p>2、存储过程的使用</p>
<p>上述语句就在mytest数据库中创建了名为insertIN的存储过程。</p>
<pre>
&lt;%
String proc=&quot;{call insertIN(?)}&quot;;//调用存储过程
try{
Class.forName(&quot;com.mysql.jdbc.Driver&quot;);
String url=&quot;jdbc:MySQL://localhost:3306/mytest?user=root&amp;password=123456$charsetEncoding=utf-8&quot;;
Connection con=DriverManager.getConnection(url);
CallableStatement stmt=con.prepareCall(proc);

stmt.setInt(1,9);//设置传入参数的值为9
stmt.execute();
stmt.close();
out.println(&quot;&lt;br&gt;MySQL数据库存储过程insertIN(IN param1 INt)被JSP成功调用！&quot;);
}catch(SQLException e){
out.println(e.toString());
}
%&gt;
</pre>
<p>3、OK</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thosedays.us/sqlprocedureanddelimiter/268.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>深入字符编码（一）</title>
		<link>http://www.thosedays.us/charset/265.html</link>
		<comments>http://www.thosedays.us/charset/265.html#comments</comments>
		<pubDate>Thu, 19 May 2011 16:56:14 +0000</pubDate>
		<dc:creator>huotuotuofly</dc:creator>
				<category><![CDATA[学习]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[JSP]]></category>
		<category><![CDATA[字符集]]></category>

		<guid isPermaLink="false">http://www.thosedays.us/%e6%b7%b1%e5%85%a5%e5%ad%97%e7%ac%a6%e7%bc%96%e7%a0%81%ef%bc%88%e4%b8%80%ef%bc%89/265.html</guid>
		<description><![CDATA[写了一个很简单的jsp页面，从数据库中取出联系人的个人信息（编号、姓名、电话），完成显示修改删除等操作。逻辑很快就完成了，结果页面显示一直是乱码，并且过程中还出现了这样那样的问题。本文就此说说俺解决问题时学到的知识和获得的经验。



1、关于编码

<span class="readmore"><a href="http://www.thosedays.us/charset/265.html" title="深入字符编码（一）">阅读全文——共1308字</a></span>]]></description>
			<content:encoded><![CDATA[<p>写了一个很简单的jsp页面，从数据库中取出联系人的个人信息（编号、姓名、电话），完成显示修改删除等操作。逻辑很快就完成了，结果页面显示一直是乱码，并且过程中还出现了这样那样的问题。本文就此说说俺解决问题时学到的知识和获得的经验。</p>
<p><span id="more-265"></span>
<p>1、关于编码</p>
<p><strong>不同的字符集，规定了不同字符的编码方式。</strong></p>
<p>关于字符集和编码的区别，我认为不必要较真，要了解详细参见<a href="http://www.iteye.com/topic/97803">http://www.iteye.com/topic/97803</a>&#160;</p>
<p>常用的字符集：</p>
<p><strong>ASCII</strong>：仅占一个字节8位。高位为校验位，能表示127（2^7）个字符。<a href="http://en.wikipedia.org/wiki/ASCII">http://en.wikipedia.org/wiki/ASCII</a></p>
<p>&#160;</p>
<p><strong>gb2312</strong>: 对英文字符、英文字母、汉字做了编码，占用两个字节。普遍应用于国内的网站论坛，相比于utf-8好处是单个页面体积小。</p>
<p>&#160;</p>
<p><strong>unicode</strong>： 万国码，支持各国语言。一个字符固定2个字节（unicode有两个版本另一个版本一个字符4个字节，常用的是2字节编码）。<strong>Java中JVM内部是以Unicode存储字符串的</strong>，所以char a= ’中’ ;在java中是正确的。此外还要注意JVM的Unicode编码和eclipse中的页面编码是不一样的，前者是不能修改的，它内部实现就是Unicode编码的，或者说字符在JVM中就是Unicode形式存在的。而后者是指一个文件的编码方式，影响页面的显示。这是很重要的。</p>
<p>unicode缺点是与ascii兼容不好，出现了向上兼容性不好的问题，而其他编码包括utf-8与ascii是兼容的。</p>
<p><strong>utf-8</strong>: 是基于unicode的可变长字符编码。ascii中的字符在utf-8字符集中完全保留占一个字节，中文占3个字节。</p>
<p>&#160;</p>
<p><strong>ISO-8859-1</strong>：Latin-1 单字节编码，mysql默认的编码模式，建议将默认改为utf-8。</p>
<p>&#160;</p>
<p>2、编程语言与编码</p>
<blockquote><p>&#160;</p>
<p>C、C++、Python2内部字符串都是使用当前系统默认编码     <br />Python3、Java内部字符串用Unicode保存       <br />Ruby有一个内部变量$KCODE用来表示可识别的多字节字符串的编码，变量值为&quot;EUC&quot;&quot;SJIS&quot; &quot;UTF8&quot; &quot;NONE&quot;之一。       <br />$KCODE的值为&quot;EUC&quot;时，将假定字符串或正则表达式的编码为EUC-JP。       <br />同样地，若为&quot;SJIS&quot;时则认定为Shift JIS。若为&quot;UTF8&quot;时则认定为UTF-8。       <br />若为&quot;NONE&quot;时，将不会识别多字节字符串。       <br />在向该变量赋值时，只有第1个字节起作用，且不区分大小写字母。       <br />&quot;e&quot; &quot;E&quot; 代表 &quot;EUC&quot;，&quot;s&quot; &quot;S&quot; 代表 &quot;SJIS&quot;，&quot;u&quot; &quot;U&quot; 代表 &quot;UTF8&quot;，而&quot;n&quot; &quot;N&quot; 则代表 &quot;NONE&quot;。       <br />默认值为&quot;NONE&quot;。       <br />即默认情况下Ruby把字符串当成单字节序列来处理。</p>
</blockquote>
<p>3、选用哪种编码</p>
<p>看下我们经常用的一些网站都用那中编码。</p>
<ol>
<li>淘宝 &#8211; GB2312 </li>
<li>支付宝 &#8211; GB2312 </li>
<li>口碑 &#8211; GBK </li>
<li>中国雅虎 &#8211; GB2312 </li>
<li>百度 &#8211; GB2312 </li>
<li>新浪 &#8211; GB2312 </li>
<li>搜狐 &#8211; GB2312 </li>
<li>豆瓣 &#8211; UTF-8 </li>
<li>Yupoo &#8211; UTF-8 </li>
<li>谷歌 &#8211; UTF-8 </li>
<li>&#8230;</li>
</ol>
<p>虽然gb2312 编码页面下载量要小一点，但utf-8才是世界的标准，其强大的兼容性是应该为我们考虑的。</p>
<p>&#160;</p>
<p>下篇文章将用一个实例说明怎样解决页面的乱码问题。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thosedays.us/charset/265.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>线性时间排序</title>
		<link>http://www.thosedays.us/linearordering/246.html</link>
		<comments>http://www.thosedays.us/linearordering/246.html#comments</comments>
		<pubDate>Sun, 10 Apr 2011 15:27:00 +0000</pubDate>
		<dc:creator>huotuotuofly</dc:creator>
				<category><![CDATA[算法]]></category>

		<guid isPermaLink="false">http://www.thosedays.us/?p=246</guid>
		<description><![CDATA[前面说的算法都是基于输入元素的比较，即所谓的比较排序。而任意一个比较排序算法在最坏情况下，都需要做Ω(nlgn) 次的比较。因此堆排序和合并排序都是渐进最优的比较排序算法。具体证明用到决策树模型。参见这里。

本文讨论的则是三种以线性时间运行的非比较排序算法：计数排序、基数排序和桶排序。



<span class="readmore"><a href="http://www.thosedays.us/linearordering/246.html" title="线性时间排序">阅读全文——共2393字</a></span>]]></description>
			<content:encoded><![CDATA[<p>前面说的算法都是基于输入元素的比较，即所谓的比较排序。而任意一个比较排序算法在最坏情况下，都需要做Ω(nlgn) 次的比较。因此堆排序和合并排序都是渐进最优的比较排序算法。具体证明用到决策树模型。参见<a href="http://planetmath.org/encyclopedia/LowerBoundForSorting.html">这里</a>。</p>
<p>本文讨论的则是三种以线性时间运行的非比较排序算法：计数排序、基数排序和桶排序。</p>
<p><span id="more-246"></span>
<p>一、计数排序</p>
<p>假定n个输入元素中的每一个都是介于0到k之间的整数，此处k为某个整数（可以选n个元素中的最大值）。当k=O(n)时，计数排序的运行时间为Θ(n)。</p>
<p>基本思想是对每一个输入元素x确定出小于x的元素个数。这样就可以把x直接放在相应的位置上了。</p>
<p>计数排序的重要性在于它的稳定性(相同值的相对位置在排序前后没变)，常作为基数排序算法的一个子过程。</p>
<p>应用：显示器的快速呈像。因为颜色的种类显然是固定的（k），算法中关注各个值的个数，然后放到相应位置。</p>
<p>代码实现：</p>
<hr />
<p>/*************************    <br />**counting_sort.c     <br />**huotuotuofly     <br />*************************/     <br />#include &lt;stdio.h&gt;</p>
<p>int maxIs(int [],int);    <br />void countingSort(int [],int [], int,int);     <br />int main(){     <br />&#160;&#160;&#160; int arr[]={2,5,3,0,2,3,0,3};     <br />&#160;&#160;&#160; int length=sizeof(arr)/sizeof(*arr);     <br />&#160;&#160;&#160; /*变长数组是刚加进c99的特性，虽说gcc默认标准是c89,但gcc     <br />&#160;&#160;&#160; 对此做了一些扩展，支持c99的一些特性，变长数组就是其中之一     <br />&#160;&#160;&#160; 。此外gcc .. -std=c89 编译变长数组也是成功的，这说明此处变     <br />&#160;&#160;&#160; 长数组是gcc的扩展实现，早在c99出现前gcc就支持了。而在VC中     <br />&#160;&#160;&#160; 编译是不能通过的。所以变长数组还是尽量少用为好，可用动态数组     <br />&#160;&#160;&#160; 代替。*/     <br />&#160;&#160;&#160; int result[length];     <br />&#160;&#160;&#160; int max=maxIs(arr,length);     <br />&#160;&#160;&#160; <br />&#160;&#160;&#160; countingSort(arr,result,max,length);     <br />&#160;&#160;&#160; printf(&quot;排序结果：&quot;);     <br />&#160;&#160;&#160; int i;     <br />&#160;&#160;&#160; for(i=0;i&lt;length;i++){     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; printf(&quot;%d &quot;,result[i]);     <br />&#160;&#160;&#160; }     <br />&#160;&#160;&#160; printf(&quot;\n&quot;);     <br />}     <br />/**************************************************************     <br />找到数组中的最大值。保证每个数组元素都是介于0～max中的值     <br />**************************************************************/     <br />int maxIs(int tem[],int length){     <br />&#160;&#160;&#160; int i;     <br />&#160;&#160;&#160; int largest=0;     <br />&#160;&#160;&#160; for(i=0;i&lt;length;i++){     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; if(tem[i]&gt;tem[largest])     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; largest=i;     <br />&#160;&#160;&#160; }     <br />&#160;&#160;&#160; return tem[largest];     <br />}     <br />/*************************************************************     <br />sour原数组     <br />result存放结果数组     <br />max数组中的最大值     <br />lenght数组长度     <br />tem数组下标是序列中各个可能的值。     <br />**************************************************************/</p>
<p>void countingSort(int sour[],int result[],int max,int length){    <br />&#160;&#160;&#160; int i;     <br />&#160;&#160;&#160; int tem[max+1];     <br />&#160;&#160;&#160; //初始化     <br />&#160;&#160;&#160; for(i=0;i&lt;=max;i++){     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; tem[i]=0;     <br />&#160;&#160;&#160; }     <br />&#160;&#160;&#160; int j;     <br />&#160;&#160;&#160; //tem[i]中存的是等于i的元素个数     <br />&#160;&#160;&#160; for(j=0;j&lt;length;j++){     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; tem[sour[j]]=tem[sour[j]]+1;</p>
<p>&#160;&#160;&#160; }    <br />&#160;&#160;&#160; //tem[i]的值是小于或等于i的元素个数     <br />&#160;&#160;&#160; for(i=1;i&lt;=max;i++){     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; tem[i]=tem[i]+tem[i-1];</p>
<p>&#160;&#160;&#160; }    <br />&#160;&#160;&#160; /*从序列中从后向前依次取出数字sour[j]，并且从tem中查看不大于     <br />&#160;&#160;&#160; 此元素的元素个数tem[sour[j]],然后将此元素sour[j]放入result的     <br />&#160;&#160;&#160; tem[sour[j]]位置处，并将记录数组tem减一。循环。*/     <br />&#160;&#160;&#160; for(j=length-1;j&gt;=0;j&#8211;){     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; result[tem[sour[j]]-1]=sour[j];     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; tem[sour[j]]—;     <br />&#160;&#160;&#160; }     <br />} </p>
<hr />
<p>&#160;</p>
<p>二、基数排序</p>
<p>基数（radix sort）排序最早是用在老式穿卡机的算法。</p>
<p>思路：从低位到高位，对序列中元素的每一位进行排序。（我觉得从高位到低位也可以，只是在排次高位时的前提是高位相同。这样排序时间会更短。但是比较高位是否相同会增加算法的复杂度。）从低位向高位依次排序，要保证每次的排序是稳定的不然会破坏前面排好的序列。</p>
<p>伪代码：</p>
<p>假设长度为n的数组A，每个元素都有d位数字，其中第1位是最低位，第d位是最高位。</p>
<p>RADIX_SORT(A，d)</p>
<p>1&#160;&#160;&#160;&#160;&#160;&#160;&#160; for i=1 to d</p>
<p>2&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; do 对数组A中数字的第i位做稳定排序</p>
<p>&#160;</p>
<hr />
<p>三、桶排序</p>
<p>桶排序（bucket sort）当输入符合均匀分布时，即可以以线性期望时间运行。相较于计数排序假设输入是一个范围内的整数，桶排序假设输入是随机产生均与分布的，能较均匀的散列到区间[0,1)上。</p>
<p>看起来就像哈希表。</p>
<p>伪代码：</p>
<p>void BucketSon(R)   <br />&#160;&#160;&#160; { //对R[0..n-1]做桶排序，其中0≤R[i].key&lt;1(0≤i&lt;n)    <br />&#160;&#160;&#160;&#160;&#160; for(i=0，i&lt;n;i++) //分配过程．    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; 将R[i]插入到桶B[∟n(R[i].key)」]中； //可插入表头上    <br />&#160;&#160;&#160;&#160;&#160; for(i=0;i&lt;n;i++) //排序过程    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; 当B[i]非空时用插人排序将B[i]中的记录排序；    <br />&#160;&#160;&#160;&#160;&#160; for(i=0，i&lt;n；i++) //收集过程    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; 若B[i]非空，则将B[i]中的记录依次输出到R中；    <br />&#160;&#160;&#160;&#160; }</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thosedays.us/linearordering/246.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>排序算法整理（三）</title>
		<link>http://www.thosedays.us/sortingalgorithm3/236.html</link>
		<comments>http://www.thosedays.us/sortingalgorithm3/236.html#comments</comments>
		<pubDate>Sat, 09 Apr 2011 14:44:15 +0000</pubDate>
		<dc:creator>huotuotuofly</dc:creator>
				<category><![CDATA[算法]]></category>

		<guid isPermaLink="false">http://www.thosedays.us/?p=236</guid>
		<description><![CDATA[堆排序和快速排序。



一、堆排序

<span class="readmore"><a href="http://www.thosedays.us/sortingalgorithm3/236.html" title="排序算法整理（三）">阅读全文——共999字</a></span>]]></description>
			<content:encoded><![CDATA[<p>堆排序和快速排序。</p>
<p><span id="more-236"></span><br />
一、堆排序
</p>
<p>利用堆数据结构（一颗完全二叉树，用数组存储，排序中用的是最大堆，最小堆用于实现优先队列）的特性。即：节点的值总是不小于其子节点的值。</p>
<p>时间复杂度：O(nlgn)  原地排序</p>
<p>代码实现：</p>
<pre>
/******************************************
**heapsort.c
**huotuotuofly
*******************************************/
#include &lt;stdio.h&gt;

void build_max_heap(int [],int);
void max_heap_adjust(int [],int,int);
int main(){
	int arr[]={16,14,10,8,7,9,3,2,4,1};
	int length=sizeof(arr)/sizeof(int);

	build_max_heap(arr,length);
	int heap_size=length;
	int i;
	printf(&quot;最大堆：&quot;);
	for(i=0;i&lt;length;i++){
                printf(&quot;%d &quot;,arr[i]);
        }

	printf(&quot;\n&quot;);
	for(i=length-1;i&gt;=1;i--){
		int ex=arr[i];
		arr[i]=arr[0];
		arr[0]=ex;
		heap_size--;
		max_heap_adjust(arr,0,heap_size);
	}
	printf(&quot;排序结果：&quot;);
	for(i=0;i&lt;length;i++){
		printf(&quot;%d &quot;,arr[i]);
	}
	printf(&quot;\n&quot;);
}

/**************************************************
堆调整。
将以下标i为根的树调整为最大堆，假定i的两个二叉子树已
经是最大堆。
size为整个最大堆的大小，而非整个数组的长度。
***************************************************/
void max_heap_adjust(int tem[],int i,int size){
	int leftNode=2*i+1;
	int rightNode=2*i+2;
	int larger;
	if(leftNode&lt;=size-1&amp;&amp;tem[leftNode]&gt;tem[i])
		larger=leftNode;
	else larger=i;
	if(rightNode&lt;=size-1&amp;&amp;tem[rightNode]&gt;tem[larger])
		larger=rightNode;
	if(larger!=i){
		int ex=tem[i];
		tem[i]=tem[larger];
		tem[larger]=ex;
		max_heap_adjust(tem,larger,size);
	}
}
/**************************************************
建堆。自下往上建堆。调用堆调整方法。
***************************************************/
void build_max_heap(int tem[],int length){
	int i;
	for(i=(length/2)-1;i&gt;=0;i--){
		max_heap_adjust(tem,i,length);
	}
}
</pre>
<p>二、快速排序</p>
<p>分治思想，取主元，然后分别将小于大于主元的元素放在左右两侧；左右两侧递归使用此算法。实际中就是一般应用此算法进行排序。</p>
<p>时间复杂度：O(nlgn) 原地排序</p>
<pre>
/***************************************
**quicksort.c
**huotuotuofly
***************************************/
#include &lt;stdio.h&gt;

void quicksort(int [],int ,int);
int partition(int [],int ,int);
int main(){
	int arr[]={2,7,1,2,3,9,5,6,4,8};
	int length=sizeof(arr)/sizeof(int);
	quicksort(arr,0,length-1);

	int i;
	printf(&quot;排序结果：&quot;);
	for(i=0;i&lt;length;i++){
		printf(&quot;%d &quot;,arr[i]);
	}
	printf(&quot;\n&quot;);
}
/****************************************
取主元，并将小于它的放在左边，大于它的放
在右边。
再对左右两边递归进行快排
q为主元在序列中找到适当位置后的下标。
*****************************************/
void quicksort(int tem[],int p,int r){
	if(p&lt;r){
		int q=partition(tem,p,r);
		quicksort(tem,p,q-1);
		quicksort(tem,q+1,r);
	}
}

/******************************************
此处主元取得是单个序列中的最后一个值（也可
以取第一个）。
函数将主元放在适当位置，并返回其下标。
*******************************************/
int partition(int tem[],int p,int r){
	int x=tem[r];
	int i=p-1;
	int j;
	for(j=p;j&lt;r;j++){
		if(tem[j]&lt;=x){
			i++;
			int ex=tem[j];
			tem[j]=tem[i];
			tem[i]=ex;
		}
	}
	int ex=tem[i+1];
	tem[i+1]=tem[r];
	tem[r]=ex;
	return i+1;
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.thosedays.us/sortingalgorithm3/236.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>排序算法整理（二）</title>
		<link>http://www.thosedays.us/sortingalgorithm2/219.html</link>
		<comments>http://www.thosedays.us/sortingalgorithm2/219.html#comments</comments>
		<pubDate>Sat, 09 Apr 2011 06:05:46 +0000</pubDate>
		<dc:creator>huotuotuofly</dc:creator>
				<category><![CDATA[算法]]></category>

		<guid isPermaLink="false">http://www.thosedays.us/?p=219</guid>
		<description><![CDATA[关于冒泡排序和选择排序

一、冒泡排序



<span class="readmore"><a href="http://www.thosedays.us/sortingalgorithm2/219.html" title="排序算法整理（二）">阅读全文——共295字</a></span>]]></description>
			<content:encoded><![CDATA[<p>关于冒泡排序和选择排序</p>
<p><span id="more-219"></span>一、冒泡排序</p>
<p>
重复的交换相邻的两个反序元素。
</p>
<p>时间复杂度：O(n^2)</p>
<pre>
/*****************************
**bubble_sort.c
**huotuotuofly
******************************/
#include &lt;stdio.h&gt;

void bubbleSort(int [],int);
int main(){
	int arr[]={2,1,5,7,3,4,6};
	int length=sizeof(arr)/sizeof(int);
	bubbleSort(arr,length);
	printf(&quot;排序结果：&quot;);
	int i;
	for(i=0;i&lt;length;i++){
		printf(&quot;%d &quot;,arr[i]);
	}
	printf(&quot;\n&quot;);
}
/*******************************
从左往右，两两比较，大的往后放
********************************/
void bubbleSort(int tem[],int length){
	int i;
	int j;
	for(i=0;i&lt;length;i++){
		for(j=0;j&lt;length-i-1;j++){
                        if(tem[j]&gt;tem[j+1]){
				int ex=tem[j+1];
				tem[j+1]=tem[j];
				tem[j]=ex;
			}
		}
	}
}
</pre>
<p>二、选择排序</p>
<p>每次选出最小的值，放到前面已排好的序列的最后</p>
<p>时间复杂度：O(n^2)</p>
<pre>
/************************
**select_sort.c
**huotuotuofly
*************************/
#include &lt;stdio.h&gt;

int main(){
	int arr[]={5,1,3,7,2,4,9,6,8};
	int length=sizeof(arr)/sizeof(int);
	int i;
	int j;
	int min;
	/*min记下最小元素的下标*/
	for(i=0;i&lt;length;i++){
		min=i;
		for(j=i+1;j&lt;length;j++){
			if(arr[min]&gt;arr[j])
				min=j;
		}
		int ex=arr[i];
		arr[i]=arr[min];
		arr[min]=ex;
	}
	/*输出结果*/
	printf(&quot;排序结果：&quot;);
	for(i=0;i&lt;length;i++){
		printf(&quot;%d &quot;,arr[i]);
	}
	printf(&quot;\n&quot;);
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.thosedays.us/sortingalgorithm2/219.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>排序算法整理（一）</title>
		<link>http://www.thosedays.us/sortingalgorithm/196.html</link>
		<comments>http://www.thosedays.us/sortingalgorithm/196.html#comments</comments>
		<pubDate>Fri, 08 Apr 2011 18:54:45 +0000</pubDate>
		<dc:creator>huotuotuofly</dc:creator>
				<category><![CDATA[算法]]></category>

		<guid isPermaLink="false">http://www.thosedays.us/?p=196</guid>
		<description><![CDATA[最近面试遇到好多关于排序算法的题目。算法没系统的学过，只在数据结构中有一点点涉及。就趁这机会复习下排序算法。

一、插入排序

依次取出元素，插入到前面已排好的序列中。

<span class="readmore"><a href="http://www.thosedays.us/sortingalgorithm/196.html" title="排序算法整理（一）">阅读全文——共413字</a></span>]]></description>
			<content:encoded><![CDATA[<p>最近面试遇到好多关于排序算法的题目。算法没系统的学过，只在数据结构中有一点点涉及。就趁这机会复习下排序算法。</p>
<p><span id="more-196"></span>一、插入排序</p>
<p>依次取出元素，插入到前面已排好的序列中。</p>
<p>玩牌的时候，一张一张的摸牌，将刚摸到的牌插入到手中已排好的牌中。实际上就是应用了插入排序的算法。</p>
<p><a href="http://zh.wikipedia.org/wiki/%E6%97%B6%E9%97%B4%E5%A4%8D%E6%9D%82%E5%BA%A6">时间复杂度</a>：O(n^2)</p>
<p>具体实现：</p>
<pre>
/*********************************
**insert_sort.c
**huotuotuofly
*********************************/
#include &lt;stdio.h&gt;

int main(){
	int arr[]={5,2,4,6,1,3};
	int length=sizeof(arr)/sizeof(*arr);
	int i;
        for(i=1;i&lt;length;i++){/*gcc 的默认标准是c89, 不支持判断语句中声明变量,可用-sd=c99进行修改*/
                int key=arr[i];
                int j=i-1;
                while(j&gt;=0&amp;&amp;arr[j]&gt;key){
			arr[j+1]=arr[j];
			j--;
	     }
		arr[j+1]=key;
	}

	printf(&quot;排序结果：&quot;);
	for(i=0;i&lt;length;i++){
		printf(&quot;%d&quot;,arr[i]);
	}
	printf(&quot;\n&quot;);
}
</pre>
<p>二、合并（归并）排序</p>
<p>两两归并，递归进行。</p>
<p>时间复杂度：O(nlogn)</p>
<p>代码如下：</p>
<pre>
/*********************************
**merge_sort.c
**huotuotuofly
*********************************/
#include &lt;stdio.h&gt;
void merge_sort(int [],int, int);
int main(){
	int arr[]={5,2,4,7,1,3,2,6};
	int length=sizeof(arr)/sizeof(int);

	merge_sort(arr,0,length-1);
	int i;
	printf(&quot;排序结果：&quot;);
	for(i=0;i&lt;length;i++){
		printf(&quot;%d &quot;,arr[i]);
	}
	printf(&quot;\n&quot;);
}

/*********************************
合并两个已排好序的数组。
将排好序的左右两部分放入两个数组中，依次取出第
一个比较，小的放入原队列，如此进行。
**********************************/
void merge(int tem[],int p,int q,int r){
	int i=q-p+1;
	int j=r-q;
	int left[i];
	int right[j];
	int a;
	int b;
	for(a=0;a&lt;i;a++){
	        left[a]=tem[p+a];
	}
	for(b=0;b&lt;j;b++){
		right[b]=tem[q+1+b];
	}
	a=0;
	b=0;
	int cur;
	for(cur=p;cur&lt;=r;cur++){
                if(b&gt;=j||(left[a]&lt;=right[b]&amp;&amp;a&lt;i)){
                        tem[cur]=left[a];
                        a++;
                 }
                 else if(a&gt;=i||(left[a]&gt;right[b]&amp;&amp;b&lt;j)){
			tem[cur]=right[b];
			b++;
		}

	}
}
/*********************************
分治思想，递归进行。
左部分排序，右部分排序，合并两个有序部分。
*********************************/

void merge_sort(int tem[],int p,int r){
	if(p&lt;r){
		int q=(p+r)/2;
		merge_sort(tem,p,q);
		merge_sort(tem,q+1,r);
		merge(tem,p,q,r);
	}

}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.thosedays.us/sortingalgorithm/196.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

