<?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>MaxoTech Blog</title>
	<atom:link href="http://maxotek.net/blog/feed" rel="self" type="application/rss+xml" />
	<link>http://maxotek.net/blog</link>
	<description>Technology Blog</description>
	<lastBuildDate>Wed, 29 Jun 2011 07:44:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Search &amp; Remove Duplicate Files</title>
		<link>http://maxotek.net/blog/search-remove-duplicate-files-t311.html</link>
		<comments>http://maxotek.net/blog/search-remove-duplicate-files-t311.html#comments</comments>
		<pubDate>Sat, 02 Apr 2011 04:11:47 +0000</pubDate>
		<dc:creator>partho</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://maxotek.net/blog/?p=311</guid>
		<description><![CDATA[Unsurrogater is a product of ours that lets you search for duplicate files in Hard Disk Drives, CD/DVD Roms, USB Flash Drives, FTP Servers, Sharepoint Servers &#038; Compressed Files. Armed with a plethora of features, Unsurrogater sports a clean user interface that builds on a Job Category based navigation. The duplicate search process itself is [...]]]></description>
			<content:encoded><![CDATA[<p><a href="/products/unsurrogater">Unsurrogater</a> is a product of ours that lets you search for duplicate files in Hard Disk Drives, CD/DVD Roms, USB Flash Drives, FTP Servers, Sharepoint Servers &#038; Compressed Files.</p>
<p>Armed with a plethora of features, Unsurrogater sports a clean user interface that builds on a Job Category based navigation. The duplicate search process itself is very intuitive to start as the wizards guide you to setting up the job.</p>
<h1>Search Result</h1>
<p><img src="http://maxotek.net/images/screenshots/unsurrogater/2.0.12.0/05.%20search_results.jpg" /></p>
<p>Results are viewed hierarchically in a Tree Table display.</p>
<h1>Auto Marking</h1>
<p><img src="http://maxotek.net/images/screenshots/unsurrogater/2.0.12.0/06.%20auto_marking.jpg" /></p>
<p>Files can be marked manually or using the Auto Mark feature that takes hints from the user &#038; automatically selects files across the entire result.</p>
<h1>Actions</h1>
<p><img src="http://maxotek.net/images/screenshots/unsurrogater/2.0.12.0/07.%20actions.jpg" /></p>
<p>After marking the files, users can choose to perform operations such as copy, move, delete, archive, replace by hard links, etc.</p>
<h1>Reports</h1>
<p><img src="http://maxotek.net/images/screenshots/unsurrogater/2.0.12.0/09.%20reports.jpg" /></p>
<p>Results can be stored historically for viewing later or exported as an HTML/XML Report.</p>
<h1>Result Combination</h1>
<p><img src="http://maxotek.net/images/screenshots/unsurrogater/2.0.12.0/08.%20result_combination.jpg" /></p>
<p>A key feature of Unsurrogater is the ability to combine the search results of multiple jobs &#038; discover new duplicates. This saves a lot of time as you don&#8217;t have to run the job again for the files already scanned.</p>
]]></content:encoded>
			<wfw:commentRss>http://maxotek.net/blog/search-remove-duplicate-files-t311.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reflector to the Rescue</title>
		<link>http://maxotek.net/blog/reflector-to-the-rescue-t306.html</link>
		<comments>http://maxotek.net/blog/reflector-to-the-rescue-t306.html#comments</comments>
		<pubDate>Tue, 06 Jul 2010 02:18:29 +0000</pubDate>
		<dc:creator>partho</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Decompiling]]></category>
		<category><![CDATA[Version Control]]></category>
		<category><![CDATA[Reflector]]></category>
		<category><![CDATA[Subversion]]></category>

		<guid isPermaLink="false">http://maxotek.net/blog/reflector-to-the-rescue-t306.html</guid>
		<description><![CDATA[I was trying to get a list of all un-versioned C# code files in my working copy. Some examples on the internet pointed to Windows Power Shell which is able to pipe the output of one command to another (kind of what we have in linux). The command was:- (svn stat) -match '^\?.*\.cs$' The following [...]]]></description>
			<content:encoded><![CDATA[<p>I was trying to get a list of all un-versioned C# code files in my working copy. Some examples on the internet pointed to Windows Power Shell which is able to pipe the output of one command to another (kind of what we have in linux).</p>
<p>The command was:-</p>
<pre><code>(svn stat) -match '^\?.*\.cs$'</code></pre>
<p>The following command gave a much cleaner output (just the full path of the files)</p>
<pre><code>(svn stat &quot;--no-ignore&quot;) -match '^\?.*\.cs$' -replace '^.\s+',''</code></pre>
<p>The next command deleted those files from the PC.</p>
<pre><code>(svn stat &quot;--no-ignore&quot;) -match '^\?.*\.cs$' -replace '^.\s+','' | rm</code></pre>
<p>I did not pay attention to what the command does and executed it. Net result was that my precious source codes were gone. I looked for the files in the recycle bin. But, being a command line program, it does not seem to use the recycle bin. Then I tried NTFS undelete which is able to restore files that were deleted ‘permanently’ too. What happens is that when you delete a file (SHIFT + DELETE, skipping the recycle bin), windows merely marks the space the file occupied in the disk as free. This is done for performance reasons (a delete operation would take time that is comparable to writing to a file, otherwise).</p>
<p>However, to my dismay, NTFS undelete was unable to find the file. Perhaps it was because of Windows 7 and it&#8217;s ways of handling different versions of a file.</p>
<p>It seemed like I would have to re-create the files again. But, then it occurred to me that reflector could come in handy. I had compiled the project previously. So, the debug directory had the compiled assemblies. I used reflector to get the source files back. Although not the exact as the original, the decompiled code was good enough for me to re-create the custom user-control.</p>
]]></content:encoded>
			<wfw:commentRss>http://maxotek.net/blog/reflector-to-the-rescue-t306.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Subversion Bug &#8211; Case Insensitive Username</title>
		<link>http://maxotek.net/blog/subversion-bug-case-insensitive-username-t150.html</link>
		<comments>http://maxotek.net/blog/subversion-bug-case-insensitive-username-t150.html#comments</comments>
		<pubDate>Thu, 17 Jun 2010 18:32:38 +0000</pubDate>
		<dc:creator>partho</dc:creator>
				<category><![CDATA[Version Control]]></category>

		<guid isPermaLink="false">http://maxotek.net/blog/?p=150</guid>
		<description><![CDATA[I spent a complete day trying to find the cause for an access denied error while committing changes to the Maxotek Repository. At first it seemed like a program was using one of the files. However, even restarting the system had no effect. Then, I tried to leave out the culprit file from the commit [...]]]></description>
			<content:encoded><![CDATA[<p>I spent a complete day trying to find the cause for an access denied error while committing changes to the Maxotek Repository. At first it seemed like a program was using one of the files. However, even restarting the system had no effect. Then, I tried to leave out the culprit file from the commit and wasa greeted by the <strong>&#8220;Access Denied&#8221;</strong> message again, only this time without a mention of any file as the cause.</p>
<p>I tried deleting the cached passwords from <strong>%AppData%\Subversion\Auth\svn.simple</strong>. The next time I got the dialog asking for the account information. I typed the username &#038; password in but still got the error.</p>
<p>While at work, I tried doing the commit and it worked. So, it seemed like an IP address ban. But, then again I don&#8217;t have a static IP address. Also, the Update, Revert, Check Out commands worked without a hitch.</p>
<p>Then it occurred that it could be a ban by domain name. I use No-IP to map my dynamic IP address to a domain. So, I changed the domain but the results were the same.</p>
<p>I finally tracked it down to being a case-sensitivity issue in the username. A Check Out, Revert, Update was being allowed from a username <strong>User</strong> but Commit was not. The actual username being <strong>user</strong>.</p>
]]></content:encoded>
			<wfw:commentRss>http://maxotek.net/blog/subversion-bug-case-insensitive-username-t150.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What&#8217;s up with Maxotek?</title>
		<link>http://maxotek.net/blog/whats-up-with-maxotek-t296.html</link>
		<comments>http://maxotek.net/blog/whats-up-with-maxotek-t296.html#comments</comments>
		<pubDate>Mon, 15 Jun 2009 06:35:07 +0000</pubDate>
		<dc:creator>partho</dc:creator>
				<category><![CDATA[Site News]]></category>

		<guid isPermaLink="false">http://maxotek.net/blog/?p=296</guid>
		<description><![CDATA[Some of you may know that Maxotek is a one man company. It was always my dream to setup a software company and distribute softwares. During the last few years, I have reached closer to the dream, with Maxotek making a bit of name. Creating intuitive softwares for every day users, has been the main [...]]]></description>
			<content:encoded><![CDATA[<p>Some of you may know that Maxotek is a one man company. It was always my dream to setup a software company and distribute softwares. During the last few years, I have reached closer to the dream, with Maxotek making a bit of name. Creating intuitive softwares for every day users, has been the main goal.</p>
<p>It has been difficult to do all this, while still pursuing my degrees. Support from the users has been a great encouragement. Last month (25th of May, to be precise), I started working at <a href="http://www.globalids.com">Global IDs</a> as a Trainee Software Developer using Java technologies. Global IDs specializes in Data Integration on a large scale. Maxotek takes a step back in terms of my priority.</p>
<p>Chances of new softwares being released is very little. But, we will continue to provide technical support to our valued customers. One day, Maxotek will be back as my main goal. But, until then, there is a lot I must learn, especially the software process models and the business aspects of running a company.</p>
]]></content:encoded>
			<wfw:commentRss>http://maxotek.net/blog/whats-up-with-maxotek-t296.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Firefox hijacked by Smartbizsearch</title>
		<link>http://maxotek.net/blog/firefox-hijacked-by-smartbizsearch-t293.html</link>
		<comments>http://maxotek.net/blog/firefox-hijacked-by-smartbizsearch-t293.html#comments</comments>
		<pubDate>Tue, 28 Apr 2009 03:29:52 +0000</pubDate>
		<dc:creator>partho</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://maxotek.net/blog/?p=293</guid>
		<description><![CDATA[I was surprised to see my google search results being redirected to another website. Also, ads used to popup on clicks. Here&#8217;s what I did to fix the problem:- Start Firefox in Safe Mode (from Start menu) Reset all the addons Restart firefox]]></description>
			<content:encoded><![CDATA[<p>I was surprised to see my google search results being redirected to another website. Also, ads used to popup on clicks. Here&#8217;s what I did to fix the problem:-</p>
<ol>
<li>Start Firefox in Safe Mode (from Start menu)</li>
<li>Reset all the addons</li>
<li>Restart firefox</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://maxotek.net/blog/firefox-hijacked-by-smartbizsearch-t293.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Maxotek.com obtained</title>
		<link>http://maxotek.net/blog/maxotek-com-obtained-t285.html</link>
		<comments>http://maxotek.net/blog/maxotek-com-obtained-t285.html#comments</comments>
		<pubDate>Sat, 25 Apr 2009 02:01:07 +0000</pubDate>
		<dc:creator>partho</dc:creator>
				<category><![CDATA[Site News]]></category>

		<guid isPermaLink="false">http://maxotek.net/blog/?p=285</guid>
		<description><![CDATA[I had been waiting a long time for this domain to expire. The 75 days grace period + 5 days redemption period felt like an eternity. Along the way, I had my doubts about others back ordering the domain through go-daddy. I even tried ordering it myself, but thanks to their requirement of a verified [...]]]></description>
			<content:encoded><![CDATA[<p>I had been waiting a long time for this domain to expire. The 75 days grace period + 5 days redemption period felt like an eternity. Along the way, I had my doubts about others back ordering the domain through go-daddy. I even tried ordering it myself, but thanks to their requirement of a verified PayPal account, I saved some bucks.</p>
<p>A few days ago, I received an email from a company providing me an opportunity to obtain maxotek.com at a &#8220;premium price&#8221; before anyone else could, because I am the owner of maxotek.net. It didn&#8217;t seem right to me and so I checked the net for others&#8217; experiencing the same. From what I read, it was clear that it was a scam, so I played the waiting game and boy wasn&#8217;t I rewarded.</p>
<h1>What&#8217;s next?</h1>
<p>Maybe I&#8217;ll use both the domains for different activities. .COM for business and .NET for blogging and other internet services. Only time shall tell.</p>
]]></content:encoded>
			<wfw:commentRss>http://maxotek.net/blog/maxotek-com-obtained-t285.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Test Drive &#8211; Windows 7</title>
		<link>http://maxotek.net/blog/test-drive-windows-7-t200.html</link>
		<comments>http://maxotek.net/blog/test-drive-windows-7-t200.html#comments</comments>
		<pubDate>Tue, 20 Jan 2009 19:00:04 +0000</pubDate>
		<dc:creator>partho</dc:creator>
				<category><![CDATA[Reviews]]></category>

		<guid isPermaLink="false">http://maxotek.net/blog/?p=200</guid>
		<description><![CDATA[I got hold of the Beta version of Microsoft&#8217;s forthcoming Operating System &#8211; Windows 7. In terms of looks it is very similar to Windows Vista. The first change you will notice is the new Taskbar which merges the Quick Launch toolbar and gets rid of text labels for the items. When I first heard about [...]]]></description>
			<content:encoded><![CDATA[<p><img class="float" src="/images/blog/windows_7_logo.jpg" />I got hold of the Beta version of Microsoft&#8217;s forthcoming Operating System &#8211; Windows 7. In terms of looks it is very similar to Windows Vista. The first change you will notice is the new Taskbar which merges the Quick Launch toolbar and gets rid of text labels for the items. When I first heard about the new Taskbar, I didn&#8217;t think it would do well because icons might not be enough to recognize the applications. This is where the increase in the taskbar height comes in handy. The larger icons provide easy switching and save a lot of horizontal space. </p>
<p><img src="http://www.iconocast.com/B000000000000134_German/R4/News1_2.jpg" /></p>
<p>The taskbar is smart enough to detect an ongoing operation and shows a small progress bar behind the icon. Say you are downloading a file using Internet Explorer in the background and working on a Word document. You don&#8217;t even need to switch to the download dialog window to see how much of the file has been downloaded. Just move your eye to the taskbar icon, handy eh?</p>
<p><img src="http://funkjedi.com/wp-content/uploads/2009/01/windows7-toolbar.jpg" /></p>
<p>The taskbar also shows multiple thumbnails of grouped items; all of which can be previewed full screen without even having to switch to the windows (also no clicks or keypresses required).</p>
<p><img src="http://www.winsupersite.com/images/win7/win7_ff_taskbar_09.jpg" /></p>
<p>Another improvement is the desktop preview feature which can be activated by moving the mouse inside the vertical rectangle at the extreme right of the taskbar. Again, no clicks required. To get back to the current screen, just move your mouse away.</p>
<p><img src="http://cybernetnews.com/wp-content/uploads/2008/10/windows-7-desktop-after.jpg" /></p>
<p>Coming back to the taskbar icons, I noticed that unopen taskbar items which are pinned to the taskbar have a whitish glow below the icons. For open windows the background is a gradient based on the color which forms the majority of the icon (looks really cool, I must say).</p>
<p>Speaking of looks, the bootscreen of Windows 7 is what should have been done to Windows Vista.</p>
<p><img src="http://img.photobucket.com/albums/v374/vishaal_here/Windows7_Beta_Boot_Screen.png" /></p>
<p>I guess they rushed Vista&#8217;s release and left a lot of things uncompleted. Windows 7 will most certainly address these issues and be what Windows Vista should have been after such a long development period. The naming of Windows 7 seems more of a marketing nature, than technical. If you look at the versioning of Windows, you&#8217;ll find the major version has been updated only when there has been a substantial change in the Operating System core. Windows 9X systems (95, 98, ME) where all version 4, 2000 &amp; XP where v 5. So should have been Vista &amp; Vienna as version 6. But, as I read in an article somewhere, Vista is a tainted brand and so Microsoft decided to get the V out. Say bye to Vista/Vienna &amp; hello to Windows 7.</p>
]]></content:encoded>
			<wfw:commentRss>http://maxotek.net/blog/test-drive-windows-7-t200.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Data Structure Tutorial : Lesson 02 &#8211; Implementing Sorting Algorithms Part I</title>
		<link>http://maxotek.net/blog/data-structure-tutorial-lesson-2-implementing-sorting-algorithms-part-i-t185.html</link>
		<comments>http://maxotek.net/blog/data-structure-tutorial-lesson-2-implementing-sorting-algorithms-part-i-t185.html#comments</comments>
		<pubDate>Wed, 06 Aug 2008 14:33:08 +0000</pubDate>
		<dc:creator>partho</dc:creator>
				<category><![CDATA[Data Structures & Algorithms]]></category>

		<guid isPermaLink="false">http://maxotek.net/blog/?p=185</guid>
		<description><![CDATA[Tutorial Map Introduction Sorting Data Selecting a Sorting Algorithm Sorting Data by Using Bubble Sort Implementing Bubble Sort Algorithm Determining the Efficiency of Bubble Sort Algorithm Sorting Data by Using Selection Sort Implementing Selection Sort Algorithm Determining the Efficiency of Selection Sort Algorithm Sorting Data by Using Insertion Sort Implementing Insertion Sort Algorithm Determining the [...]]]></description>
			<content:encoded><![CDATA[<div class="tutnav"></div>
<h1>Tutorial Map</h1>
<ol>
<li> <a href="#cIntroduction">Introduction</a> </li>
<li> <a href="#cSortingData">Sorting Data</a> </li>
<li> <a href="#cSelectingASortingAlgorithm">Selecting a Sorting Algorithm</a> </li>
<li> <a href="#cSortingDataByUsingBubbleSort">Sorting Data by Using Bubble Sort</a> </li>
<li> <a href="#cImplementingBubbleSortAlgorithm">Implementing Bubble Sort Algorithm</a> </li>
<li> <a href="#cDeterminingTheEfficiencyOfBubbleSortAlgorithm">Determining the Efficiency of Bubble Sort Algorithm</a> </li>
<li> <a href="#cSortingDataByUsingSelectionSort">Sorting Data by Using Selection Sort</a> </li>
<li> <a href="#cImplementingSelectionSortAlgorithm">Implementing Selection Sort Algorithm</a> </li>
<li> <a href="#cDeterminingTheEfficiencyOfSelectionSortAlgorithm">Determining the Efficiency of Selection Sort Algorithm</a> </li>
<li> <a href="#cSortingDataByUsingInsertionSort">Sorting Data by Using Insertion Sort</a> </li>
<li> <a href="#cImplementingInsertionSortAlgorithm">Implementing Insertion Sort Algorithm</a> </li>
<li> <a href="#cDeterminingTheEfficiencyOfInsertionSortAlgorithm">Determining the Efficiency of Insertion Sort Algorithm</a> </li>
<li> <a href="#cSortingDataByUsingShellSortAlgorithm">Sorting Data by Using Shell Sort Algorithm</a> </li>
<li> <a href="#cEfficiencyOfShellSort">Efficiency of Shell Sort</a> </li>
</ol>
<h1 id="cIntroduction"> Introduction</h1>
<p> Retrieving data quickly is one of the major tasks of an efficient data management system. Sorting helps in retrieving data faster by storing data in a particular order. There are various sorting algorithms that can be used to arrange data in a specific order. </p>
<p> This tutorial discusses various sorting algorithms and their implementation. In addition, it compares the efficiency of various sorting algorithms. </p>
<h1 id="cSortingData"> Sorting Data</h1>
<p> Consider that you need to retrieve the telephone number of a person named Steve in a telephone directory, where the names in the telephone directory are stored in a random order. </p>
<p> To retrieve the desired record, you need to sequentially traverse the list of names one by one because the names are not sorted, which is a tedious and time consuming activity. When you have to retrieve a record from a huge volume of data, this activity becomes even more difficult. </p>
<p> A simple solution to save time, and search data efficiently is sorting. Sorting is the process of arranging data in some pre-defined order or sequence. The order can be either ascending or descending. </p>
<p> If the data is sorted, you can directly go to the section that stores the names starting with ‘S’, thereby reducing the number of records to be traversed. </p>
<p> There are different types of sorting algorithms that can help you sort data in a particular order. Even when two algorithms have the same efficiency, there can be situations when one works better than the other. </p>
<h1 id="cSelectingASortingAlgorithm"> Selecting a Sorting Algorithm</h1>
<p> There are various sorting algorithms that are used to sort data. Some of them are:- </p>
<ul>
<li> Bubble Sort </li>
<li> Selection Sort </li>
<li> Insertion Sort </li>
<li> Shell Sort </li>
<li> Merge Sort </li>
<li> Quick Sort </li>
<li> Heap Sort </li>
</ul>
<p> Because there are a number of sorting algorithms, it becomes important to understand which sorting algorithm to use in a particular situation. To select an appropriate algorithm, you need to consider the following criteria in the suggested order:- </p>
<ul>
<li> Execution time </li>
<li> Storage space </li>
<li> Programming effort </li>
</ul>
<p> Consider a situation where the data that needs to be sorted is small in quantity. To sort this data, all the sorting algorithms will use a reasonable amount of storage space. In addition, the sorting algorithms will be executed in a reasonable amount of time. </p>
<p> Therefore, in this situation, the criteria for selecting the sorting algorithm would be the programming effort involved. An algorithm that requires less programming effort would be preferred over an algorithm that requires more programming effort<em>. </em> </p>
<p> Consider another situation where the data that needs to be sorted is large in size. In such a situation, the time taken by different algorithms may differ drastically because of the difference in their orders of growth. For example, when there are a large number of elements, an algorithm with a logarithmic order of growth will execute much faster than an algorithm with a quadratic order of growth. </p>
<p> In addition, with increase in data, the space requirement of different algorithms may also differ drastically. Therefore, when the data is large, you need to select a sorting algorithm that makes most efficient use of time or memory, depending upon the requirement. </p>
<h1 id="cSortingDataByUsingBubbleSort"> Sorting Data by Using Bubble Sort</h1>
<p> Bubble sort is one of the simplest sorting algorithms. This algorithm has a quadratic order of growth and is therefore suitable for sorting small lists only. The algorithm works by repeatedly scanning through the list, comparing adjacent elements, and swapping them if they are in the wrong order. The algorithm gets its name from the way smaller elements “bubble’ to the top of the list after being swapped with the greater elements. </p>
<h1 id="cImplementingBubbleSortAlgorithm"> Implementing Bubble Sort Algorithm</h1>
<p> To understand the implementation of bubble sort algorithm, consider an unsorted list of numbers stored in an array. Suppose there are n elements in the array. </p>
<p> To implement bubble sort algorithm, you need to traverse the list multiple times. The process of traversing the entire list once is called a pass<em>. </em>It can be said that sorting is performed in multiple passes. </p>
<h5> Pass 1</h5>
<p> In Pass 1, you compare the first two elements and interchange their values, if the first number is greater than the second number. Then, you compare the second and the third elements, and interchange their values if they are not in the correct order. You repeat this process till (n &#8211; 1) <sup>th</sup> element is compared with the n<sup>th</sup> element. The total number of comparisons in Pass 1 is therefore, n &#8211; 1. </p>
<p> By the end of Pass 1, the largest element is placed at the n<sup>th</sup> position in the array. The<br />
	number of comparisons is one less than the total number of elements in the list. </p>
<h5> Pass 2 </h5>
<p> In Pass 2, you repeat the same process as in Pass 1, but stop the comparison after comparing the element at the (n &#8211; 2)<sup>th</sup> position with the element at the (n &#8211; 1)<sup>th</sup> position. This time the number of comparisons required will be one less than in Pass 1. The total number of comparisons in Pass 1 is therefore, n &#8211; 2. </p>
<p> By the end of Pass 2, the second largest number will be placed at the (n &#8211; 1)<sup>th</sup> position in<br />
	the array. </p>
<h5> Pass 3</h5>
<p> In Pass 3, you repeat the same process as in Pass 2, and this time there will be n &#8211; 3 comparisons. This means that the comparison will stop after comparing the element at the (n<br />
	- 3)<sup>th</sup> position with the element at the (n &#8211; 2)<sup>th</sup> position. </p>
<h5> Pass n &#8211; 1</h5>
<p> Continuing the same process in all subsequent passes, in the (n &#8211; 1)<sup>th</sup> pass you will have to perform only one comparison. After the completion of this pass, the list will be sorted in the ascending order. </p>
<p> <em><strong>Note:</strong></em> <em>To sort a list with n elements by using bubble sort, n </em>- <em>1 passes are required </em> </p>
<p> Consider an example. You have an unsorted list containing the ranks of students based on the results of an exam. You need to sort these ranks in the ascending order. The list of ranks is stored in an array, as shown in the following figure:- </p>
<p> <img src="/images/blog/ds_tut_lesson_2_bubble_1.png" /> </p>
<p> There are five elements in the list, therefore four passes would be required to sort the list. </p>
<p> In Pass 1, you need to perform the following steps:- </p>
<ol>
<li> Compare arr[0] with arr[1]. In the given list, arr[0] &gt; arr[1], therefore, you need to interchange the two values. The resultant list is shown in the following figure.
<p> <img src="/images/blog/ds_tut_lesson_2_bubble_2.png" /> </p>
</li>
<li> Compare arr[1] with arr[2]. Here, arr[1] &lt; arr[2], therefore, the values remain unchanged. </li>
<li> Compare arr[2] with arr[3]. Here, arr[2] &lt; arr[3], therefore, the values remain unchanged. </li>
<li> Compare arr[3] with arr[4]. Here arr[3] &gt; arr[4], therefore, you need to interchange the two values, as shown in the following figure.
<p> <img src="/images/blog/ds_tut_lesson_2_bubble_3.png" /> </p>
</li>
</ol>
<p> At the end of Pass 1, the largest element is placed at the last index position. The preceding process will be repeated in all the subsequent passes. However, the value at index 4 will not be compared in any of the subsequent passes because it has already been placed at its correct position. </p>
<p> In Pass 2, you need to perform the following steps:- </p>
<ol>
<li> Compare arr[0] with arr[1]. In the given list, arr[0] &lt; arr[1], therefore, the values remain unchanged. </li>
<li> Compare<br />
		arr[1] with arr[2]. Here, arr[1] &lt; arr[2], therefore, the values remain unchanged. </li>
<li> Compare arr[2] with arr[3]. Here, arr[2] &gt; arr[3], therefore, you need to interchange the two values, as shown in the following figure.
<p> <img src="/images/blog/ds_tut_lesson_2_bubble_4.png" /> </p>
</li>
</ol>
<p> At the end of this pass, the second largest element is placed at its correct position in the list. You will repeat the same process in the subsequent passes. However, the values at Index 3 and 4 will not be compared in the subsequent passes, because they are already placed at their correct position. </p>
<p> The result after Pass 3 would be as follows:- </p>
<p> <img src="/images/blog/ds_tut_lesson_2_bubble_1.png" /> </p>
<p> Similarly, the result after Pass 4 would be as follows. </p>
<p> <img src="/images/blog/ds_tut_lesson_2_bubble_1.png" /> </p>
<p> After Pass 4, the list is completely sorted. </p>
<p> The algorithm to sort the list by using the bubble sort algorithm is as follows:- </p>
<li> Set pass = 1 </li>
<li> Repeat step 3 varying j from 0 to n &#8211; 1 &#8211; pass </li>
<li> If the element at index j is greater than the element at index j + 1, swap the two elements </li>
<li> Increment pass by 1 </li>
<li> If pass &lt;= n &#8211; 1 goto step 2 </li>
<p> <em><strong>Note: </strong>If in the preceding example, instead of checking that the element at index j is greater than the element at index j </em>+ <em>1, you check that the element at index j is less than the element at index j</em>+ <em>1, then the list will be sorted in the descending order. </em> </p>
<h1 id="cDeterminingTheEfficiencyOfBubbleSortAlgorithm"> Determining the Efficiency of Bubble Sort Algorithm</h1>
<p> The efficiency for a sorting algorithm is measured in terms of the number of comparisons. The number of comparisons in bubble sort can be easily computed. In bubble sort, there are n &#8211; 1 comparisons in Pass 1, n &#8211; 2 comparisons in Pass 2, and so on. Therefore, the total number of comparisons will be: (n &#8211; 1) + (n &#8211; 2) + (n &#8211; 3) + &#8230; + 3 + 2 + El,which is an arithmetic progression. </p>
<p> The formula for determining the sum of an arithmetic progression is:- </p>
<p> Sum = n/2 [2a + (n - 1)d] </p>
<p> Where, </p>
<p> n is the total number of elements in the arithmetic progression. </p>
<p> a is the first element in the arithmetic progression. </p>
<p> d is the step value, that is, the difference in successive terms of an arithmetic progression. </p>
<p> For the preceding arithmetic progression, </p>
<p> n = n &#8211; 1 </p>
<p> a = 1 </p>
<p> d = 1 </p>
<p> Therefore, </p>
<p> Sum = (n -1)/2 [2 x 1 + (n - 1 - el) x el] <br />
	Sum = (n &#8211; 1)/2 [2 + (n - 2)] </p>
<p> Sum = (n -1)/2 (n) <br />
	Sum = n(n &#8211; 1)/2 </p>
<p> n(n &#8211; <em>1)/2 </em>is of O(n<sup>2</sup>) order. Therefore, the bubble sort algorithm is of the order O(n<sup>2</sup>). This means that the time taken to execute the algorithm increases quadratically with the increase in the size of the array. </p>
<p> Suppose it takes 100 ns to execute the algorithm on a list of 10 elements. Now, if the number of elements is doubled, that is, the number of elements is increased to 20, the execution time will increase to 400 ns, which is four times the time taken to execute the algorithm on 10 elements. </p>
<p> Given below is a function that takes an unsorted integer array and sorts it using the Bubble Sort Algorithm. </p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="csharp"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> BubbleSortArray<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> a<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;</span> a.<span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">// For n - 1 passes</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #008080; font-style: italic;">// In pass i, compare the first n - i elements with their next elements</span>
		<span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> j <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> j <span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;</span> a.<span style="color: #0000FF;">Length</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span> j<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>a<span style="color: #000000;">&#91;</span>j<span style="color: #000000;">&#93;</span> <span style="color: #008000;">&amp;</span>gt<span style="color: #008000;">;</span> a<span style="color: #000000;">&#91;</span>j <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #FF0000;">int</span> temp<span style="color: #008000;">;</span>
				temp <span style="color: #008000;">=</span> a<span style="color: #000000;">&#91;</span>j<span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
				a<span style="color: #000000;">&#91;</span>j<span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> a<span style="color: #000000;">&#91;</span>j <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
				a<span style="color: #000000;">&#91;</span>j <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> temp<span style="color: #008000;">;</span>
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
	<span style="color: #0600FF;">return</span> a<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<h1 id="cSortingDataByUsingSelectionSort"> Sorting Data by Using Selection Sort</h1>
<p> Like bubble sort, selection sort is another simple sorting algorithm. Selection sort also has a quadratic order of growth, and is therefore used for sorting small lists only. </p>
<p>
	Selection sort scans through the list iteratively, selecting one item in each scan, and moving the item to its correct position in the list. In contrast to bubble sort, where you need to perform n &#8211; r swaps in the r<sup>th</sup> pass, selection sort performs a maximum of one swap per pass. </p>
<h1 id="cImplementingSelectionSortAlgorithm"> Implementing Selection Sort Algorithm</h1>
<p> Similar to bubble sort, selection sort also involves multiple passes through the list of elements. In Pass 1, you locate the smallest element from the list, and swap it with the element at the first position in the list. At the end of Pass 1, the smallest value will be placed at its correct position in the list. </p>
<p> In Pass 2, you locate the next smallest value, and swap it with the element placed at the second position in the list. Similarly, you repeat the steps until all the values are placed at the correct positions in the list. If there are n elements in the list, the list will be sorted in n &#8211; 1 passes. </p>
<p> Suppose, you need to sort the following list of elements stored in an array by using the<br />
	selection sort algorithm. </p>
<p> <img src="/images/blog/ds_tut_lesson_2_selection_1.png" /> </p>
<p> To sort this list by using the selection sort algorithm, you need to perform the following <br />
	steps:- </p>
<ol>
<li> Locate the smallest element in the list. In the given list, arr[2] is the smallest element. </li>
<li> Swap the smallest element with the element at index 0 in the array. The resultant array is shown in the following figure.
<p> <img src="/images/blog/ds_tut_lesson_2_selection_2.png" /> </p>
</li>
<li> Locate the next smallest element in the list. Here, arr[4] is the next smallest element. </li>
<li> Swap the smallest element with the element at index 1 in the array, as shown in the following figure:-
<p> <img src="/images/blog/ds_tut_lesson_2_selection_3.png" /> </p>
</li>
<li> Locate the next smallest<br />
		element in the list. here, arr[2] is the next smallest element, as shown in the following figure:-</p>
<p> <img src="/images/blog/ds_tut_lesson_2_selection_4.png" /> </p>
<p> The smallest element needs to be swapped with the element at index 2. However, the smallest element is already at index 2. Therefore, you do not need to swap its position with another element. </p>
</li>
<li> Locate the next smallest element in the list. Here, arr[4] is the next smallest element. </li>
<li> Swap the smallest element with the element at index 3 in the array, as shown in the following figure:-
<p> <img src="/images/blog/ds_tut_lesson_2_selection_5.png" /> </p>
</li>
</ol>
<p> The list is now completely sorted. </p>
<p> The algorithm to sort the list by using the selection sort algorithm is as follows:- </p>
<ol>
<li> Repeat steps 2 and 3 varying j from 0 to n &#8211; 2 // Repeat for n &#8211; 1 passes </li>
<li> Find the index of the minimum value in arr[j] to arr[n - 1]
<ol type="a">
<li> Set min_index = j </li>
<li> Repeat step c varying i from j + 1 to n &#8211; 1 </li>
<li> If arr[i] &lt; arr[min_index]:
<ol type="i">
<li> min_index = i </li>
</ol>
</li>
</ol>
</li>
<li> Swap arr[j] with arr[min_index] </li>
</ol>
<h1 id="cDeterminingTheEfficiencyOfSelectionSortAlgorithm"> Determining the Efficiency of Selection Sort Algorithm</h1>
<p> To determine the efficiency of selection sort, you need to determine the number of comparisons in selection sort. There are n &#8211; 1 comparisons during Pass 1 to find the smallest element. There are n &#8211; 2 comparisons during Pass 2 to find the second smallest<br />
	element, and so on. </p>
<p> Therefore, the total number of comparisons will be: &#8211; </p>
<p> (n &#8211; 1) + (n &#8211; 2) + (n &#8211; 3) + &#8230; + 3 + 2 + 1 </p>
<p> This is the same as bubble sort. Therefore, the selection sort algorithm is of the order O(n<sup>2</sup>)<strong><em>. </em></strong>This means that the time taken to execute the algorithm increases quadratically with the increase in the size of the array. </p>
<p> Given below is a C# implementation of the above algorithm:- </p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="csharp"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> SelectionSortArray<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> a<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> j <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> j <span style="color: #008000;">&lt;</span> a.<span style="color: #0000FF;">Length</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span> j<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #FF0000;">int</span> MinIndex <span style="color: #008000;">=</span> j<span style="color: #008000;">;</span>
		<span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> j <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> a.<span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>a<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> <span style="color: #008000;">&lt;</span> a<span style="color: #000000;">&#91;</span>MinIndex<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				MinIndex <span style="color: #008000;">=</span> i<span style="color: #008000;">;</span>
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #FF0000;">int</span> Temp <span style="color: #008000;">=</span> a<span style="color: #000000;">&#91;</span>MinIndex<span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
		a<span style="color: #000000;">&#91;</span>MinIndex<span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> a<span style="color: #000000;">&#91;</span>j<span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
		a<span style="color: #000000;">&#91;</span>j<span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> Temp<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
	<span style="color: #0600FF;">return</span> a<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<h1 id="cSortingDataByUsingInsertionSort"> Sorting Data by Using Insertion Sort</h1>
<p> Similar to bubble and selection sort that have a quadratic order of growth, insertion sort also has a quadratic order of growth, and is therefore used for sorting small lists only. </p>
<p> However, insertion sort is much more efficient than bubble sort and selection sort, if the list that needs to be sorted is nearly sorted. This is because bubble sort and selection sort always perform the same number of comparisons, no matter what is the initial ordering of elements. </p>
<p> In contrast, insertion sort performs different number of comparisons depending on the initial ordering of elements. When the elements are already in the sorted order, insertion sort needs to make very few comparisons. </p>
<h1 id="cImplementingInsertionSortAlgorithm"> Implementing Insertion Sort Algorithm </h1>
<p> The insertion sort algorithm divides the list into two parts, sorted and unsorted. Initially, the sorted part contains only one element. In each pass, one element from the unsorted list is inserted at its correct position in the sorted list. As a result, the sorted list grows by one element and the unsorted list shrinks by one element in each pass. </p>
<p> Consider an unsorted list stored in an array that needs to be sorted by using the insertion<br />
	sort algorithm. </p>
<p> <img src="/images/blog/ds_tut_lesson_2_insertion_1.png" /> </p>
<p> To sort this list by using the insertion sort algorithm, you need to divide the list into two sublists, sorted and unsorted. Initially, the sorted list contains only the first element and the unsorted list contains the remaining four elements, as shown in the following figure:- </p>
<p> <img src="/images/blog/ds_tut_lesson_2_insertion_2.png" /> </p>
<p> <em><strong>Note:</strong> The list is not physically divided into two separate arrays. The division is only logical where the element at index 0’s considered to be in the sorted list and the elements at indexes I to 4 are considered to be in the unsorted list. </em> </p>
<p> Now, to further sort the unsorted list, you need to perform a number of passes: </p>
<ol>
<li> In Pass 1, take the first element (80) from the unsorted list, and store it at its correct position in the sorted list. Here, 80 is greater than 70, therefore, it remains at array index 1. However, the array index 1 is now considered to be a part of the sorted list. This is shown in the following figure:-
<p> <img src="/images/blog/ds_tut_lesson_2_insertion_3.png" /> </p>
<p> The sorted list has two elements now and the unsorted list has three elements. </p>
</li>
<li> In Pass 2, take the first element (30) from the unsorted list, and store it at its correct position in the sorted list. Here, 30 is less than 70 and 80, therefore, it needs to be stored at array index 0. To store 30 at array index 0, 80 needs to be shifted to array index 2, and 70 needs to be shifted to array index I. This is shown in the following figure.
<p> <img src="/images/blog/ds_tut_lesson_2_insertion_4.png" /> </p>
<p> The sorted list has three elements now and the unsorted list has two elements. </p>
</li>
<li> In Pass 3, take the first element (10) from the unsorted list, and store it at its correct<br />
		position in the sorted list.</p>
<p> Here, 10 is smaller than the three elements in the sorted list, therefore, it needs to be stored at index position 0, as shown in the following figure. </p>
<p> <img src="/images/blog/ds_tut_lesson_2_insertion_5.png" /> </p>
<p> The sorted list has four elements now and the unsorted list has one element. </p>
</li>
<li> In Pass 4, take the first element (20) from the unsorted list, and store it at its correct position in the sorted list. Here, 20 is greater than 10 and smaller than the other three elements in the sorted list. Therefore, it needs to be stored at index position 1, as shown in the following figure:-
<p> <img src="/images/blog/ds_tut_lesson_2_insertion_6.png" /> </p>
<p> The unsorted list is now empty and the sorted list contains all the elements. This<br />
			means that the list is now completely sorted. </p>
</li>
</ol>
<p> The algorithm to sort the list by using the insertion sort algorithm is as follows:- </p>
<ol>
<li> Repeat steps 2, 3, 4, and 5 varying i from 1 to n -1 </li>
<li> Set temp = arr[i] </li>
<li> Set j = i &#8211; i </li>
<li> Repeat until j becomes less than 0 or arr[j] becomes less than or equal to temp:
<ol type="a">
<li> Shift the value at index j to index j + 1 </li>
<li> Decrement j by i </li>
</ol>
</li>
<li> Store temp at index j + 1 </li>
</ol>
<h1 id="cDeterminingTheEfficiencyOfInsertionSortAlgorithm"> Determining the Efficiency of Insertion Sort Algorithm</h1>
<p> Consider an unsorted list of numbers with n elements. To sort this unsorted fist by using insertion sorting, you need to perform (n &#8211; 1I) passes. In insertion sort, if the list is already sorted, you will have to make only one comparison in each pass. </p>
<p> In n -1 passes, you will need to make n &#8211; 1 comparisons. This is the best case for insertion sort. Therefore, the best case efficiency of insertion sort is of the order O(n). </p>
<p> Now, consider a situation where initially, the list is stored in the reverse order. In this case, you need to make one comparison in the first pass, two comparisons in the second pass, three comparisons in Pass 3, and n &#8211; 1 comparisons in the (n &#8211; 1)<sup>th</sup> pass. </p>
<p> The total number of comparisons in this case is:- </p>
<p> Sum = 1 + 2 + &#8230; + (n &#8211; 1) </p>
<p> This is the same as bubble sort and selection sort. Therefore, the worst case efficiency of<br />
	insertion sort is of the order O(n<sup>2</sup>). </p>
<p> Given below is a C# implementation of the Insertion sort algorithm:- </p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="csharp"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> InsertionSortArray<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> a<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> a.<span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #FF0000;">int</span> Temp <span style="color: #008000;">=</span> a<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
		<span style="color: #FF0000;">int</span> j <span style="color: #008000;">=</span> i <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span>j <span style="color: #008000;">&gt;=</span> <span style="color: #FF0000;">0</span> <span style="color: #008000;">&amp;&amp;</span> a<span style="color: #000000;">&#91;</span>j<span style="color: #000000;">&#93;</span> <span style="color: #008000;">&gt;</span> Temp<span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			a<span style="color: #000000;">&#91;</span>j <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> a<span style="color: #000000;">&#91;</span>j<span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
			j<span style="color: #008000;">--;</span>
		<span style="color: #000000;">&#125;</span>
		a<span style="color: #000000;">&#91;</span>j <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> Temp<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
	<span style="color: #0600FF;">return</span> a<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<h1 id="cSortingDataByUsingShellSortAlgorithm"> Sorting Data by Using Shell Sort Algorithm</h1>
<p> Insertion sort is an efficient algorithm only if the list is already partially sorted and results in an inefficient solution in an average case. To overcome this limitation, a computer scientist, DL. Shell proposed an improvement over the insertion sort algorithm. The new algorithm was called shell sort after the name of its proposer. </p>
<p> The shell sort algorithm improves the insertion sort algorithm by grouping the elements separated by a distance of several positions to form multiple sublists. Once the list is divided into sublists, you apply insertion sort on each sublist to move the elements towards their correct positions. This helps an element to take a bigger step towards its correct position, thereby reducing the number of comparisons. </p>
<p> To understand the implementation of shell sort algorithm, consider an array a [0...n - 1]. To apply shell sort on this array, you need to select the distance by which the elements in<br />
	a group will be separated. </p>
<p> Suppose, initially you group the elements separated by a distance of four elements to create the following sublists:- </p>
<p> • a[0], a[4], a[8] &#8230; <br />
	• a[1], a[5], a[9] &#8230; <br />
	• a[2], a[6], a[10] &#8230;<br />
	• a[3], a[7], a[11] &#8230; </p>
<p> You can say that the sublists are created with an increment value of four. Each of the preceding sublists will be sorted by using insertion sort. In the next pass, the increment value will be reduced to three, and the elements will again be grouped to create the following sublists. </p>
<ul>
<li> a[0], a[3], a[6] &#8230; </li>
<li> a[1], a[4], a[7] &#8230; </li>
<li> a[2], a[5], a[8]<br />
		&#8230; </li>
</ul>
<p> Each of the preceding sublists will again be sorted by using insertion sort. In the next pass, the increment value will be reduced to two, and the elements will again be grouped to create the following sublists:- </p>
<ul>
<li> a[0], a[2], a[4] &#8230; </li>
<li> a[1], a[3], a[5]<br />
		&#8230; </li>
</ul>
<p> Each of the preceding lists Will again be sorted by using insertion sort. In the next pass, the increment value will be reduced to one. In this case, there will be only one list that will be almost sorted. Insertion sort will be applied on the list to sort it completely. </p>
<p> Consider an example of an unsorted list of 11 elements, as shown in the following figure. </p>
<p> <img src="/images/blog/ds_tut_lesson_2_shell_1.png" /> </p>
<p> To sort the given list by using the shell sort algorithm, you need to perform a number of passes. The number of passes would depend on the initial increment value. Suppose the initial increment value is three, then the list will be sorted in three passes of the complete<br />
	list. </p>
<p> <strong> Pass 1 </strong> </p>
<p> In Pass 1, you group the elements with a distance of three to form three sublists, as shown in the following figure:- </p>
<p> <img src="/images/blog/ds_tut_lesson_2_shell_2.png" /> </p>
<p> After the list is divided into sublists, you sort the sublists by using the insertion sort algorithm on each sublist individually. Once the sublists are sorted, you combine them to form one single list. </p>
<p> The following figure depicts the result of Pass 1:- </p>
<p> <img src="/images/blog/ds_tut_lesson_2_shell_3.png" /> </p>
<p> <strong>Pass 2 </strong> </p>
<p> In Pass 2, you group the elements separated by a distance of two to form two sublists, as shown in the following figure:- </p>
<p> <img src="/images/blog/ds_tut_lesson_2_shell_4.png" /> </p>
<p> After the list is divided into sublists, you sort the sublists by using the insertion sort algorithm. Once the sublists are sorted, you combine them to form a single list.<br />
	The following figure displays the result of Pass 2. </p>
<p> <img src="/images/blog/ds_tut_lesson_2_shell_5.png" /> </p>
<p> <strong>Pass 3 </strong> </p>
<p> In Pass 3, you group the elements separated by a distance of one, as shown in the<br />
	following figure:- </p>
<p> <img src="/images/blog/ds_tut_lesson_2_shell_6.png" /> </p>
<p> In this case, there is only a single list. The list is more or less sorted. You can sort the list completely by applying the insertion sort algorithm. The following figure displays the<br />
	result of Pass	3. </p>
<p> <img src="/images/blog/ds_tut_lesson_2_shell_7.png" /> </p>
<p> The list is now sorted. The algorithm to sort the list by using the shell sort algorithm is as<br />
	follows:- </p>
<ol>
<li> Repeat step 2 varying incr from 3 to 1 // Repeat for each pass. </li>
<li> Repeat steps 3 and 4, varying L from 0 to incr &#8211; 1 // Repeat for each sublist in a pass </li>
<li> Apply insertion sort on the L<sup>th</sup> sublist:
<ol type="a">
<li> Set i = L + incr // i is set to the index of the second element in the L<sup>th</sup> sublist </li>
<li> Repeat until i becomes greater than or equal to n:
<ol type="i">
<li> Set temp = arr[i] </li>
<li> Set j = i &#8211; incr </li>
<li> Repeat steps iv and v until j becomes less than 0 or arr[j] becomes less than or equal to temp </li>
<li> Shift the value at index j to index j + incr </li>
<li> Decrement j by the value contained in incr </li>
<li> Store temp at index j + incr </li>
<li> Increment i by the value contained in incr
					</li>
</ol>
</li>
</ol>
</li>
</ol>
<h1 id="cEfficiencyOfShellSort"> Efficiency of Shell Sort </h1>
<p> The efficiency of shell sorting can be determined with the help of mathematical analysis.<br />
	Although it is easy to develop an intuitive sense of how this algorithm works, it is very difficult to analyze its execution time. However, estimates range from O(n log<sub>2</sub>n) to O(n<sup>1.5</sup>) depending on the initial size of the increment value. </p>
<p> <strong><em>Note: </em></strong><em>The efficiency of this sorting algorithm is based on the size of the increments. It is recommended to use prime numbers as increment values because if the increment values are powers of 2 then the same elements are compared again in the next passes. <br />	</em> </p>
<p> Given below is a C# implementation of the Shell sort algorithm:- </p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre class="csharp"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> ShellSortArray<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> a<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> incr <span style="color: #008000;">=</span> <span style="color: #FF0000;">3</span><span style="color: #008000;">;</span> incr <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> incr<span style="color: #008000;">--</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> L <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> L <span style="color: #008000;">&lt;</span> incr<span style="color: #008000;">;</span> L<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> L <span style="color: #008000;">+</span> incr<span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> a.<span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span> i <span style="color: #008000;">+=</span> incr<span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #FF0000;">int</span> temp <span style="color: #008000;">=</span> a<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
				<span style="color: #FF0000;">int</span> j <span style="color: #008000;">=</span> i <span style="color: #008000;">-</span> incr<span style="color: #008000;">;</span>
				<span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span>j <span style="color: #008000;">&gt;=</span> <span style="color: #FF0000;">0</span> <span style="color: #008000;">&amp;&amp;</span> a<span style="color: #000000;">&#91;</span>j<span style="color: #000000;">&#93;</span> <span style="color: #008000;">&gt;</span> temp<span style="color: #000000;">&#41;</span>
				<span style="color: #000000;">&#123;</span>
					a<span style="color: #000000;">&#91;</span>j <span style="color: #008000;">+</span> incr<span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> a<span style="color: #000000;">&#91;</span>j<span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
					j <span style="color: #008000;">-=</span> incr<span style="color: #008000;">;</span>
				<span style="color: #000000;">&#125;</span>
				a<span style="color: #000000;">&#91;</span>j <span style="color: #008000;">+</span> incr<span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> temp<span style="color: #008000;">;</span>
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
	<span style="color: #0600FF;">return</span> a<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p> <em>You will learn about other sorting techniques, such as quick sort, merge sort, and heap<br />
	sort in subsequent tutorials</em> </p>
<div class="tutnav"></div>
]]></content:encoded>
			<wfw:commentRss>http://maxotek.net/blog/data-structure-tutorial-lesson-2-implementing-sorting-algorithms-part-i-t185.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Data Structure Tutorial : Lesson 01 &#8211; Role of Algorithms and Data structure in problem Solving</title>
		<link>http://maxotek.net/blog/data-structure-tutorial-lesson-1-role-of-algorithms-and-data-structure-in-problem-solving-t146.html</link>
		<comments>http://maxotek.net/blog/data-structure-tutorial-lesson-1-role-of-algorithms-and-data-structure-in-problem-solving-t146.html#comments</comments>
		<pubDate>Wed, 06 Aug 2008 06:15:47 +0000</pubDate>
		<dc:creator>partho</dc:creator>
				<category><![CDATA[Data Structures & Algorithms]]></category>

		<guid isPermaLink="false">http://maxotek.net/blog/?p=146</guid>
		<description><![CDATA[Tutorial Map Introduction Role of Algorithms and Data Structures in Problem Solving Role of Algorithms Role of Data Structures Types of Data Structures Designing Algorithms and measuring their efficiency Identifying Techniques for Designing Algorithms Divide and Conquer Approach Greedy Approach Designing Algorithms using Recursion Tower of Hanoi Determining the Efficiency of an Algorithm Time/Space Tradeoff [...]]]></description>
			<content:encoded><![CDATA[<div class="tutnav"></div>
<h1>Tutorial Map</h1>
<ol>
<li>
		<a href="#cIntroduction">Introduction</a>
	</li>
<li>
		<a href="#cRoleOfAlgorithmsAndDataStructuresInProblemSolving">Role of Algorithms and Data Structures in Problem Solving</a>
	</li>
<li>
		<a href="#cRoleOfAlgorithms">Role of Algorithms</a>
	</li>
<li>
		<a href="#cRoleOfDataStructures">Role of Data Structures</a>
	</li>
<li>
		<a href="#cTypesOfDataStructures">Types of Data Structures</a>
	</li>
<li>
		<a href="#cDesigningAlgorithmsAndMeasuringTheirEfficiency">Designing Algorithms and measuring their efficiency</a>
	</li>
<li>
		<a href="#cIdentifyingTechiniquesForDesigningAlgorithms">Identifying Techniques for Designing Algorithms</a></p>
<ol type="a">
<li>
				<a href="#cDivideAndConquerApproach">Divide and Conquer Approach</a>
			</li>
<li>
				<a href="#cGreedyApproach">Greedy Approach</a>
			</li>
</ol>
</li>
<li>
		<a href="#cDesigningAlgorithmsUsingRecursion">Designing Algorithms using Recursion</a></p>
<ol type="a">
<li>
				<a href="#cTowerOfHanoi">Tower of Hanoi</a>
			</li>
</ol>
</li>
<li>
		<a href="#cDeterminingTheEfficiencyOfAnAlgorithm">Determining the Efficiency of an Algorithm</a></p>
<ol type="a">
<li>
				<a href="#cTimeSpaceTradeoff">Time/Space Tradeoff</a>
			</li>
<li>
				<a href="#cMethodForDeterminingEfficiency">Method for determining Efficiency</a>
			</li>
<li>
				<a href="#cSelectingAnEfficientAlgorithm">Selecting an Efficient Algorithm</a>
			</li>
<li>
				<a href="#cBestWorstAndAverageCaseEfficiency">Best, Worst, and Average Case Efficiency</a>
			</li>
</ol>
</li>
</ol>
<p><br/></p>
<h1 id="cIntroduction">Introduction</h1>
<p>
	Computer science is a field of study that deals with solving a variety of problems by using computers. The problem to be solved could be as simple as performing the addition of two numbers, or as complex as designing a robot capable of making decisions in a real-time environment. To solve a given problem by using computer, you need to design an algorithm for it. The nature of an algorithm often depends closely on the nature of the data on which the algorithm works. Therefore, the study of algorithms also involves the study of the data structures that algorithms work on.
</p>
<p>
	This tutorial discusses the role of algorithms and data structures in problem solving through computers. It also discusses some standard techniques that can be used to design algorithms. In addition, it discusses the effect of the selected algorithm on the efficiency of the solution.
</p>
<h1 id="cRoleOfAlgorithmsAndDataStructuresInProblemSolving">Role of Algorithms and Data Structures in Problem Solving</h1>
<p>
	Problem solving is an essential part of every scientific discipline. In today&#8217;s world, computers are widely used to solve problems pertaining to various domains, such as banking, commerce, medicine, manufacturing, and transport.
</p>
<p>
	To solve a given problem by using a computer, you need to write a program for it. A program consists of two components, algorithm and data structure.
</p>
<p>
	Many different algorithms can be used to solve the same problem. Similarly, various types of data structures can be used to represent a problem in a computer.
</p>
<p>
	To solve the problem in an efficient manner,you need to select a combination of algorithms and data structures that provide maximum efficiency.
</p>
<h1 id="cRoleOfAlgorithms">Role of Algorithms</h1>
<p>
	The word algorithm is derived from the name of the persian mathematician Al Khwarizmi.
</p>
<p>
	An algorithms can be defined as a step-by-step procedure for solving a problem. It helps the user arrive at the correct result in a finite number of steps. Consider the following step-by-step procedure to display the first 10 natural numbers:-
</p>
<ol>
<li>
		Set the value of counter to 1
	</li>
<li>
		Display counter
	</li>
<li>
		Increment counter by 1
	</li>
<li>
		If counter &lt;=10, go to step 2
	</li>
</ol>
<p>The preceding step-by-step procedure is an algorithm because it produces the correct result in a finite number of steps.</p>
<p>An algorithm has five important properties:-</p>
<ul>
<li>
		<strong>Finiteness:</strong> An algorithm terminates after a finite number of steps.
	</li>
<li>
		<strong>Definiteness:</strong> Each step in an algorithm is unambiguous. This means that the action specified by the step cannot be interpreted in multiple ways and can be performed without any confusion.
	</li>
<li>
		<strong>Input:</strong> An algorithm accepts zero or more inputs.
	</li>
<li>
		<strong>Output:</strong> An algorithm produces at least one output.
	</li>
<li>
		<strong>Effectiveness:</strong> An algorithm consists of basic instructions that are realizable. This means that the instructions can be performed by using the given inputs in a finite amount of time.
	</li>
</ul>
<p>A problem can be solved by using a computer only if an algorithm can be written for it. In addition, the use of algorithm provides many other benefits:-</p>
<ul>
<li>
		While writing an algorithm, you identify the step-by-step procedure, the major decision points, and the variables necessary to solve the problem. This helps you in the development of the corresponding program.
	</li>
<li>
		Identification of the procedure and the decision points reduces the problem into a series of smaller problems of more manageable size. Therefore, problems that would be difficult or impossible to solve as a whole can be approached as a series of small solvable sub problems.
	</li>
<li>
		With the use of an algorithm, decision making becomes a more rational process. This is because algorithms comprise of sub tasks, where each sub task is atomic in nature and is supported by facts.
	</li>
<li>
		With the use of an algorithm, the same specified steps are used for performing the tasks. This makes the process more consistent and reliable.
	</li>
</ul>
<h1 id="cRoleOfDataStructures">Role of Data Structures</h1>
<p>
	Multiple algorithms can be designed to solve a particular problem. However, the algorithm may differ in how efficiently they can solve the problem. In such a situation, an algorithm that provides the maximum efficiency should be used for solving the problem. Efficiency here means that the algorithm should work in minimal time and use minimal memory.
</p>
<p>
	One of the basic techniques for improving the efficiency of algorithms is to structure the data that they operate on in such a way that the resulting operations can be efficiently performed.
</p>
<p>
	The way in which the various data elements are organized in memory with respect to each other is called a data structure.
</p>
<p>
	Data can be organized in many different ways; therefore, you can create as many data structures as you want. However, there are some standard data structures that have proved useful over the years. These include arrays, linked lists, stacks, queues, trees and graphs. We will learn more about these data structures in the subsequent tutorials.
</p>
<p>
	All these data structures are designed to hold a collection of data items. However, the differences lies in the way the data items are arranged with respect to each other and the operations that they allow. Because of the different ways in which the data items are arranged with respect to each other, some data structures prove to be more efficient that others to solve a given problem.
</p>
<p>
	Suppose you have to write an algorithm that enables printer to service the requests of multiple users on a first-come-first-served (FCFS) basis. In this case, using a data structure that stores and retrieves the requests in the order of their arrival would be much more efficient than a data structure that stores and retrieves the requests in a random order.
</p>
<p>
	In addition to improving the efficiency of an algorithm, the use of appropriate data structures also allows you to overcome some other programming challenges, such as:-
</p>
<ul>
<li>
		Simplifying complex problems
	</li>
<li>
		Creating standard, reusable code components</p>
</li>
<li>
		Creating programs that are easy to understand and maintain
	</li>
</ul>
<p>Consider an example where you have to find the maximum value in a set of 50 numbers, In this scenario, you can either use 50 variables or a data structure, such as an array of size 50, to store the numbers. When 50 different variables are used to store the numbers, the algorithm to determine the maximum value among the numbers can be written as:</p>
<ol>
<li>
		Accept 50 numbers and store them in num1, num2, num3, .. num50
	</li>
<li>
		Set max = num1
	</li>
<li>
		If num2 &gt; max then: max = num2
	</li>
<li>
		If num3 &gt; max then: max = num3:<br />
		.<br />
		.
	</li>
<li>
		If num50 &gt; max then max = num50
	</li>
<li>
		Display max
	</li>
</ol>
<p>On the other hand, when an array of size 50 is used, the algorithm can be written as:-</p>
<ol>
<li>
		Set max = num[0]
	</li>
<li>
		Repeat step3 varying i from 1 to 49
	</li>
<li>
		If num[i] &gt; max then: max = num[i]
	</li>
<li>
		Display max
	</li>
</ol>
<p>
	From the preceding two algorithms, it can be seen that the algorithm using an array manipulates memory much more efficiently than the algorithm using 50 variables. Also, the algorithm using an array involves few steps and is therefore, easier to understand and implement as compared to the algorithm that uses 50 variables.
</p>
<p>
	Data structures also enable the creation of reusable code components. Suppose you have created a class to implement a data structure that stores and retrieves requests in the order of their arrival. Once the class is created, the same class can be used in several different applications that need to service the requests of multiple users on a FCFS basis.
</p>
<p>
	This means that a data structure once implemented can be used as a standard component to provide a standard solution to a specific set of problems. The use of standard components helps simplify the maintenance process. This is because the standard components are time tested and therefore, do not need much maintenance.
</p>
<h1 id="cTypesOfDataStructures">Types of Data Structures</h1>
<p>Data structures can be classified under the following two categories:</p>
<ul>
<li>
		<strong>Static:</strong> These are data structures whose size is fixed at compile time, and does not grow or shrink at runtime. An example of a static structure is an array. Suppose you declare an array of size 50, but store only 5 elements in it; the memory space allocated for the remaining 45 elements will be wasted. Similarly, if you have declared an array of size 50 but later want to store 20 more elements, you will not be able to store these extra required elements because of the fixed size of an array.
	</li>
<li>
		<strong>Dynamic:</strong> These are data structures whose size is not fixed at compile time and that can grow and shrink at run time to make efficient use of memory. An example of a dynamic data structure would be a list of items for which memory is not allocated in advance. As and when items are added to the lists, memory is allocated for those elements. Similarly, when items are removed from the list, memory allocated to those elements is de-allocated. Such a list is called a linked list.
	</li>
</ul>
<p>
	<em><strong>Note:</strong> Arrays and linked lists are basic data structures that are used to implement other data structures such as stacks, queues, trees and graphs.</em>
</p>
<p>
	<em>An array is always a static data structure, and a linked list is always a dynamic data structure. However, the other data structures can be static or dynamic depending on whether they are implemented using an array or a linked list.</em>
</p>
<h1 id="cDesigningAlgorithmsAndMeasuringTheirEfficiency">Designing Algorithms and measuring their efficiency</h1>
<p>Designing an algorithm for a given problem is a difficult intellectual exercise. This is because there is no systematic method for designing an algorithm. Moreover, there may exist more than one algorithm to solve a problem, Writing an effective algorithm for a new problem or writing a better algorithm for an already existing one is an art as well as science because it requires both creativity and insight.</p>
<h1 id="cIdentifyingTechiniquesForDesigningAlgorithms">Identifying Techniques for Designing Algorithms</h1>
<p>Although there is no systematic method for designing an algorithm, there are some well-known techniques that have probed to be quite useful in designing algorithms. Two commonly used techniques for designing algorithms are:-</p>
<h5 id="cDivideAndConquerApproach">Divide and Conquer Approach</h5>
<p>
	The divide an conquer approach is an algorithm design technique that involves breaking down the problem recursively into sub problems until the sub problems become so small that they can directly be solved. The solutions to the sub problems are then combined to give a solution to the original problem.
</p>
<p>
	Divide and conquer is a powerful approach for solving conceptually difficult problems. It simply requires you to find a way of:-
</p>
<ol>
<li>
		Breaking the problem into sub problems
	</li>
<li>
		Solving the trivial cases.
	</li>
<li>
		Combining the solutions of the sub problems to solve the original problem.
	</li>
</ol>
<p>
	Divide and conquer often provides a natural way to design efficient algorithms.
</p>
<p>
	Consider an example where you have to find the minimum value in a list of numbers. The lists is as shown in the figure:-
</p>
<p>
	<img src="/images/blog/ds_tut_lesson_1_divide_and_conquer_1.png" alt="" />
</p>
<p>
	To find the minimum value, you can divide the list into two halves, as shown in the following figure:-
</p>
<p>
	<img src="/images/blog/ds_tut_lesson_1_divide_and_conquer_2.png" alt="" />
</p>
<p>
	Again, divide each of the two lists into two halves as shown in the following figure:-
</p>
<p>
	<img src="/images/blog/ds_tut_lesson_1_divide_and_conquer_3.png" alt="" />
</p>
<p>
	Now, there are only two elements in each list. At this stage, compare the two elements in each lists to find the minimum of the two. The minimum values from each of the four lists is shown in the following figures.
</p>
<p>
	<img src="/images/blog/ds_tut_lesson_1_divide_and_conquer_4.png" alt="" />
</p>
<p>
	Again, compare the first two minimum values to determine their minimum. Also compare the last two minimum values to determine their minimum. The two minimum values thus obtained are shown in the following figure:-
</p>
<p>
	<img src="/images/blog/ds_tut_lesson_1_divide_and_conquer_5.png" alt="" />
</p>
<p>
	Again, compare the two final minimum values to obtain the overall minimum value, which is 1 in the preceding example.
</p>
<li>
<h5 id="cGreedyApproach">Greedy Approach</h5>
<p>	The greedy approach is an algorithm design technique that selects the best possible option at any given time. Algorithms based on the greedy approach are used for solving optimization problems, where you need to maximize profits or minimize costs under a given set of conditions. Some examples of optimization problems are:-</p>
<ul>
<li>
			Finding the shortest distance from an originating city to a set of destination cities, given the distances between the pairs of cities.
		</li>
<li>
			Finding the minimum number of currency notes required for an amount, where an arbitrary number of notes for each denomination are available.
		</li>
<li>
			Selecting items with maximum value from a given set of items, where the total weight of the selected items cannot exceed a given value.
		</li>
</ul>
<p>
		Consider an example, where you have to fill a bag of capacity 10kg by selecting items, (from a set of items) whose weights and values are given in the following table.
	</p>
<div style="text-align:center">
<table class="joomlaTable" border="0">
<tbody>
<tr>
<th><strong>Item</strong></th>
<th><strong>Weight (in kg)</strong></th>
<th><strong>Value (in $/kg)</strong></th>
<th><strong>Total Value (in $)</strong></th>
</tr>
<tr>
<td>A</td>
<td>2</td>
<td>200</td>
<td>400</td>
</tr>
<tr>
<td>B</td>
<td>3</td>
<td>150</td>
<td>450</td>
</tr>
<tr>
<td>C</td>
<td>4</td>
<td>200</td>
<td>800</td>
</tr>
<tr>
<td>D</td>
<td>1</td>
<td>50</td>
<td>50</td>
</tr>
<tr>
<td>E</td>
<td>5</td>
<td>100</td>
<td>500</td>
</tr>
</tbody>
</table>
<p>		Weights and Values of Items</p></div>
<p>
		A greedy algorithms acts greedy, and therefore selects the item with the maximum total value at each stage. Therefore, first of all, item C with total value of $800 and weight 4 kg will be selected. Next, item E with total value $500 and weight 5 kg will be selected. The next item with the highest value is item B with a total value of $450 and weight 3 kg. However, if this item is selected, the total weight of the selected items will be 12 kg (4 + 5 + 3), which is more than the capacity of the bag.
	</p>
<p>
		Therefore, we discard item B and search for the item with the next highest value. The item with the next higher value is item A having a total value of $400 and a total weight of 2 kg. However, the item also cannot be selected because if it is selected, the total weight of the selected items will be 11 kg ( 4 + 5 + 2). Now, there is only one item left, that is, item D with a total value of $50 and a weight of 1 kg. This item can be selected as it makes the total weight equal to 10 kg.
	</p>
<p>	The selected items and their total weights are listed in the following table.</p>
<div style="text-align:center">
<table class="joomlaTable" border="0">
<tbody>
<tr>
<th><strong>Item</strong></th>
<th><strong>Weight (in kg)</strong></th>
<th><strong>Total value (in $)</strong></th>
</tr>
<tr>
<td>C</td>
<td>4</td>
<td>800</td>
</tr>
<tr>
<td>E</td>
<td>5</td>
<td>500</td>
</tr>
<tr>
<td>D</td>
<td>1</td>
<td>50</td>
</tr>
<tr>
<td><strong>Total</strong></td>
<td><strong>10</strong></td>
<td><strong>1350</strong></td>
</tr>
</tbody>
</table>
<p>		Items selected using Greedy Approach</p></div>
<p>
		For most problems, greedy algorithms usually fail to find the globally optimal solution. This is because they usually don&#8217;t operate exhaustively on all data. They can make commitments to certain choices too early, which prevent them from finding the best overall solution later.
	</p>
<p>
		This can be seen from the preceding example, where the use of a greedy algorithm selects item with a total value of $1350 only. However, if the items were selected in the sequence depicted by the following table, the total value would have been much greater, with the weight being 10 kg only.
	</p>
<div style="text-align:center">
<table class="joomlaTable" border="0">
<tbody>
<tr>
<th><strong>Item</strong></th>
<th><strong>Weight (in kg)</strong></th>
<th><strong>Total value (in $)</strong></th>
</tr>
<tr>
<td>C</td>
<td>4</td>
<td>800</td>
</tr>
<tr>
<td>B</td>
<td>3</td>
<td>450</td>
</tr>
<tr>
<td>A</td>
<td>2</td>
<td>400</td>
</tr>
<tr>
<td>D</td>
<td>1</td>
<td>50</td>
</tr>
<tr>
<td><strong>Total</strong></td>
<td><strong>10</strong></td>
<td><strong>1700</strong></td>
</tr>
</tbody>
</table>
<p>		Optimal selection of Items</p></div>
<p>
		In the preceding example you can observe that the greedy approach commits to item E very early. This prevents it from determining the best overall solution later. Nevertheless, greedy approach is useful because its quick and easy to implement. Moreover, it often gives good approximation to the optimal value.
	</p>
</li>
<h1 id="cDesigningAlgorithmsUsingRecursion">Designing Algorithms using Recursion</h1>
<p>
	Recursion refers to the technique of defining a process in terms of itself. It is used to solve complex programming problems that are repetitive in nature.
</p>
<p>
	The basic idea behind recursion is to break a problem into smaller versions of itself, and then build up a solution for the entire problem. This may sound similar to the divide and conquer technique. However, recursions not similar to the divide and conquer technique. Divide and conquer is a theoretical concept that may be implemented in a computer program with the help of recursion.
</p>
<p>
	Recursion is implemented in a program by using a recursive procedure or function. A recursive procedure is a function which invokes itself.
</p>
<p>
	Consider a function f(n), which is the sum of the first n natural numbers. This function can be defined in several different ways.
</p>
<p>
	In mathematics, the function will be defined as:-
</p>
<p>
	f(n) = 1 + 2 + 3 + &#8230;. + n
</p>
<p>
	However, the same function can be defined in a recursive manner as:-
</p>
<p>
	f(n) = f(n &#8211; 1) + n
</p>
<p>
	Where n &gt;1; and f(1) = 1
</p>
<p>
	In this case, the recursive definition of the function f(n) calls the same function, but with its arguments reduced by one. The recursion will end n = 1, in which case f(1) = 1 has been defined.
</p>
<p>
	To understand this concept, consider a factorial function. A factorial function is defined as:-
</p>
<p>
	n! = 1 x 2 x 3 x 4 x .. x n
</p>
<p>
	This same factorial function can be redefined as:-
</p>
<p>
	n! = (n &#8211; 1)! x n</p>
<p>	Where n &gt; 1; and 0! = 1
</p>
<p>
	This definition of n! is recursive because it refers to itself when it uses (n &#8211; 1)!.<br />
	The value of n! is explicitly given where n = 0; and the value of n! for arbitrary n is defined in terms of the smaller value of n, which is closer to the base value 0.
</p>
<p>
	If you have to calculate 3! By using recursion. you first define 3! in terms of 2!:-
</p>
<p>
	3! = (3 x 2!)
</p>
<p>
	Now, you will define 2! in terms of 1!:-
</p>
<p>
	3! = (3 x (2 x 1!))
</p>
<p>
	Now, 1! will be defined in terms of 0!:-
</p>
<p>
	3! = (3 x (2 x (1 x 0!)))
</p>
<p>
	As, 0! is defined as 1, the expression becomes:-
</p>
<p>
	3! = (3 x (2 x (1 x 1)))
</p>
<p>
	3! = (3 x (2 x 1 ))
</p>
<p>
	3! = (3 x 2)</p>
<p>	3! = 6
</p>
<p>
	This recursive algorithm for determining the factorial of a number n can be written as:-
</p>
<p>
	<strong> Algorithm: Factorial(n)</strong>
</p>
<ol>
<li>
		If n = 0, then: //Terminating condition</p>
<ol type="a">
<li>
				Return (1)
			</li>
</ol>
</li>
<li>
		Return (n x Factorial(n &#8211; 1))
	</li>
</ol>
<p>
	Please note that every recursive algorithm should have a terminating condition. Otherwise, the algorithm will keep on calling itself infinitely.
</p>
<p>
	The main advantage of recursion is that it is useful in writing clear, short, and simple programs. One of the most common and interesting problems that can be solved using recursion is the Tower of Hanoi problem.
</p>
<h5 id="cTowerOfHanoi">Tower of Hanoi</h5>
<p>
	Tower of Hanoi is a classical problem, which consists on n different sized disks and three pins over which these disks can be mounted. All the disks are placed on the first pin with the largest disk at the bottom and the remaining disks in decreasing order of their size as shown in the following figure:-
</p>
<p>
	<img src="/images/blog/ds_tut_lesson_1_tower_of_hanoi.png" alt="" />
</p>
<p>
	The objective of the game is to move all disks from the first pin to the third pin in the least number of moves by using the second pin as an intermediary.
</p>
<p>
	To play this game, you need to follow rules:-
</p>
<ul>
<li>
		Only one disk can be moved at a time
	</li>
<li>
		A larger disk cannot be placed over a smaller one
	</li>
</ul>
<p>
	Let n be the number of the discs. If n = 3, it will require seven moves to transfer all discs from pin one to pin three, as shown in the table below.
</p>
<table class="joomlaTable" border="0">
<tbody>
<tr>
<th>Steps</th>
<th>Moves</th>
</tr>
<tr>
<td>1.</td>
<td>move top disc from pin 1 to pin 3</td>
</tr>
<tr>
<td>2.</td>
<td>move top disc from pin 1 to pin 2</td>
</tr>
<tr>
<td>3.</td>
<td>move top disc from pin 3 to pin 2</td>
</tr>
<tr>
<td>4.</td>
<td>move top disc from pin 1 to pin 3</td>
</tr>
<tr>
<td>5.</td>
<td>move top disc from pin 2 to pin 1</td>
</tr>
<tr>
<td>6.</td>
<td>move top disc from pin 2 to pin 3</td>
</tr>
<tr>
<td>7.</td>
<td>move top disc from pin 1 to pin 3</td>
</tr>
</tbody>
</table>
<p>
	The moves given in the preceding table ar illustrated in the following figures:-
</p>
<p>
	<img class="float" src="/blog/images/ds_tut_lesson_1_tower_of_hanoi_0.png" alt="" /> <img class="float" src="/blog/images/ds_tut_lesson_1_tower_of_hanoi_1.png" alt="" /> <img class="float" src="/blog/images/ds_tut_lesson_1_tower_of_hanoi_2.png" alt="" /> <img class="float" src="/blog/images/ds_tut_lesson_1_tower_of_hanoi_3.png" alt="" /> <img class="float" src="/blog/images/ds_tut_lesson_1_tower_of_hanoi_4.png" alt="" /> <img class="float" src="/blog/images/ds_tut_lesson_1_tower_of_hanoi_5.png" alt="" /> <img class="float" src="/blog/images/ds_tut_lesson_1_tower_of_hanoi_6.png" alt="" /> <img class="float" src="/blog/images/ds_tut_lesson_1_tower_of_hanoi_7.png" alt="" />
</p>
<p>
	When n = 2, we should move the top disc from pin 1 to pin 2, ,move the top disc from pin 1 to pin 3, and then move the top disc from pin 2 to pin 3.
</p>
<p>
	The solution for n = 1 will be to move the disc from pin 1 to pin 3.
</p>
<p>
	In general, to move n discs from pin 1 to pin 3 using pin 2 as an intermediary, you first need to move the top n &#8211; 1 discs from pin 1 to pin 2 using pin 3 as intermediary.
</p>
<p>
	The following algorithm can be used to move the top n discs from the first pin START to final pin FINISH through the temporary pin TEMP:-
</p>
<p>
	<strong> Move (n, START, TEMP, FINISH)</strong>
</p>
<ol>
<li>
		When n = 1:</p>
<ol type="a">
<li>
				MOVE a disc from START to FINISH
			</li>
<li>
				Return
			</li>
</ol>
</li>
<li>
		Move the top n -1 discs from START to TEMP using FINISH as an intermediary [MOVE (n - 1, START, FINISH, TEMP)]
	</li>
<li>
		Move the top disc from START to FINISH
	</li>
<li>
		Move the top n &#8211; 1 discs from TEMP to FINISH using START as an intermediary [MOVE (n - 1, TEMP, START, FINISH)]
	</li>
</ol>
<p>
	In general, this solution requires 2<sup>n</sup>-1 moves for n discs.
</p>
<h1 id="cDeterminingTheEfficiencyOfAnAlgorithm">Determining the Efficiency of an Algorithm</h1>
<p>
	The greatest difficulty in solving programming problems is not how to solve the problem, but how to solve the problem efficiently. Factors that affect the efficiency of a program include the speed of the machine, the compiler. the operating system, the programming language, and the size of the input. However, in addition to these factors, the way data of a program is organized, and the algorithm used to solve the problem also has a significant impact on the efficiency of a program.
</p>
<p>
	There can be cases, where a number of methods and algorithms can be used to solve a problem. In such a situation, it can be difficult to decide which algorithm to use.
</p>
<p>
	When there are several different ways to organize data and devise algorithms, it becomes important to develop criteria to recommend a choice. Therefore, you need to study the behavior of algorithms under various conditions and compare their efficiency.
</p>
<p>
	The efficiency of an algorithm can be computed by determining the amount of resources it consumes. The primary resources that an algorithm consumes are:
</p>
<ul>
<li>
		<strong>Time</strong>: The CPU time required to execute the algorithm
	</li>
<li>
		<strong>Space</strong>: The amount of memory used by the algorithm for execution
	</li>
</ul>
<p>The lesser resources that an algorithm uses, the more efficient it is.</p>
<h5 id="cTimeSpaceTradeoff">Time/Space Tradeoff</h5>
<p>
	To solve a given programming problem, many different algorithms may be used. Some of these algorithms may be extremely time-efficient and others extremely space-efficient.
</p>
<p>
	Time/space tradeoff refers to a situation where you can reduce the use of memory at the cost of slower program execution, or reduce the running time at the cost of increased memory usage.
</p>
<p>
	An example of a situation where a time/space tradeoff can be applied is that of data storage. If data is stored in a compressed form, the memory usage is less because data compression reduces the amount of space required. However, it is more time consuming because some additional time is required to run the compression algorithm. Similarly, if data is stored in its uncompressed form, the memory usage is more, but the running time is less.
</p>
<p>
	Memory is generally perceived to be extensible because you can increase the memory of your computer. Time, however, is not extensible. Therefore, time considerations generally override memory considerations.
</p>
<h5 id="cMethodForDeterminingEfficiency">Method for determining Efficiency</h5>
<p>
	Although, the efficiency of an algorithm depends on how efficiently it uses time and memory space, the scope of this course is limited to determining only the time efficiency of an algorithm.
</p>
<p>
	To measure the time efficiency of an algorithm, you can write a program based on the algorithm, execute it, and measure the time it takes to run. The execution time that you measure in this case would depend on a number of factors such as:-
</p>
<ul>
<li>
		Speed of the machine
	</li>
<li>
		Compiler
	</li>
<li>
		Operating System
	</li>
<li>
		Programming language
	</li>
<li>
		Input data
	</li>
</ul>
<p>
	However, to determine how efficiently an algorithm solves a given problem, you would like to determine how the execution time is affected by the nature of the algorithm. Therefore, you need to develop fundamental laws that determine the efficiency of a program in terms of the nature of the underlying algorithm.
</p>
<p>
	To understand how the nature of an algorithm affects the execution, consider a simple example. Suppose assignment, comparison, write, and increment statements take a, b, c, and d time units to execute respectively. Now, consider the following code used to display the elements stored in an array:-
</p>
<ol>
<li>
		Set I = 0 // 1 assignment
	</li>
<li>
		While(I &lt; n): // n comparisons</p>
<ol type="a">
<li>
				Display a[I] // n writes
			</li>
<li>
				Increment I by 1 // n increments
			</li>
</ol>
</li>
</ol>
<p>
	The execution time required for the preceding algorithm is given by:-
</p>
<p>
	T = a + b x n + c x n + d x n
</p>
<p>
	T = a + n(b + c + d)
</p>
<p>
	Here, T is the total running time of the algorithm expressed as a linear function of the number of elements (n) in the array. From the preceding expression, it is clear that T is directly proportional to n.
</p>
<p>
	In fact, the total running time T is directly proportional to the number of iterations involved in the algorithm. The number of iterations can be determined by counting the number of comparisons involved in the algorithm.
</p>
<p>
	In the preceding code segment, a total of n comparisons are being made. Therefore, the total running time of the algorithm, T is directly proportional to n.
</p>
<p>
	As T is directly proportional to n, an increase in the value of n will result in a proportional increase in the value of T, as shown in the following figure.
</p>
<p>
	<img src="/images/blog/ds_tut_lesson_1_graph_1.png" alt="" />
</p>
<p>
	Now, consider the following algorithm:-
</p>
<ol>
<li>
		Set I = 0 // 1 assignment
	</li>
<li>
		While (I &lt; n): // n comparisons</p>
<ol type="a">
<li>
				Set J = 0 // n assignments
			</li>
<li>
				While (J &lt; n): // n x n comparisons</p>
<ol type="i">
<li>
						Display (a[I][J]) // n x n writes
					</li>
<li>
						Increment J by 1 // n x n increments
					</li>
</ol>
</li>
<li>
				Increment I by 1 // n increments
			</li>
</ol>
</li>
</ol>
<p>
	If you count the number of comparisons in the preceding code segment, they come out to be (n <sup>2</sup> + n), which is a quadratic function of n. Therefore, the total running time is directly proportional to n<sup>2</sup>.
</p>
<p>
	<em><strong>Note:</strong> Although the number of comparisons is n<sup>2</sup> + n, the value of n is very small as compared to the value of n<sup>2</sup> (especially when n is very large). Therefore, the value of n can be ignored for finding the approximate running time.</em>
</p>
<p>
	As the running time is directly proportional to n<sup>2</sup>, an increase in the value of n will result in a quadratic increase in the running time. This means that if the value of n is doubled, the running time will increase four times. The rate of change of T with an increase in the value of n is depicted in the following figure:-
</p>
<p>
	<img src="/images/blog/ds_tut_lesson_1_graph_2.png" alt="" />
</p>
<p>
	From the preceding discussion, you can conclude that the running time of a program is a function of n, where n is the size of the input data. The rate at which the running time of an algorithm increases as a result of an increase in the volume of input data is called the order of growth of the algorithm.
</p>
<p>
	The order of growth of an algorithm is defined by using the big O notation. The big O notation has been accepted as a fundamental technique for describing the efficiency of an algorithm.
</p>
<p>
	The following table lists some possible orders of growth, and their corresponding big O notations.
</p>
<div style="text-align:center">
<table class="joomlaTable" border="0">
<tbody>
<tr>
<th><strong>Order of Growth</strong></th>
<th><strong>Big O notation</strong></th>
</tr>
<tr>
<td>Constant</td>
<td>O(1)</td>
</tr>
<tr>
<td>Logarithmic</td>
<td>O(log n)</td>
</tr>
<tr>
<td>Linear</td>
<td>O(n)</td>
</tr>
<tr>
<td>Loglinear</td>
<td>O(n log n)</td>
</tr>
<tr>
<td>Quadratic</td>
<td>O(n<sup>2</sup>)</td>
</tr>
<tr>
<td>Cubic</td>
<td>O(n<sup>3</sup>)</td>
</tr>
<tr>
<td>Exponential</td>
<td>O(2<sup>n</sup>), O(10<sup>n</sup>)</td>
</tr>
</tbody>
</table>
<p>	Big O Notations </p></div>
<p>
	If an algorithm has a linear order of growth, the algorithm is said to be of the order O(n). Similarly, if an algorithm has a quadratic order of growth, the algorithm is said to be of the order O(n <sup>2</sup>).
</p>
<h5 id="cSelectingAnEfficientAlgorithm">Selecting an Efficient Algorithm</h5>
<p>
	Now that you know how the efficiency of a particular algorithm is determined, let us see how this knowledge can be used to select an efficient algorithm.
</p>
<p>
	According to their orders of growth, the big O notations can be arranged in an increasing order as:-
</p>
<p>
	O(1)   &lt; O(log n) &lt; O(n) &lt; O(n log n) &lt; O(n<sup>2</sup>) &lt; O(n<sup>3</sup>) &lt; O (2<sup>n</sup>) &lt; O (10<sup>n</sup>)
</p>
<p>
	Therefore, if a problem can be solved by using algorithms each of the preceding orders of growth, an algorithm of the order of O(1) will be considered best, and an algorithm of the order O(10<sup>n</sup>) will be considered the worst. The goal of algorithm development should be to make algorithms of the smallest possible orders.
</p>
<p>
	The following table depicts the orders of growth for the preceding big O notations.
</p>
<table class="joomlaTable" border="0">
<tbody>
<tr>
<th>Big O Notation</th>
<th>Order of growth</th>
</tr>
<tr>
<td>O(1)</td>
<td><img src="/images/blog/ds_tut_lesson_1_constant.png" alt="" /></td>
</tr>
<tr>
<td>O(log n)</td>
<td><img src="/images/blog/ds_tut_lesson_1_logarithmic.png" alt="" /></td>
</tr>
<tr>
<td>O(n)</td>
<td><img src="/images/blog/ds_tut_lesson_1_linear.png" alt="" /></td>
</tr>
<tr>
<td>O(n log n)</td>
<td><img src="/images/blog/ds_tut_lesson_1_log_linear.png" alt="" /></td>
</tr>
<tr>
<td>O(n<sup>2</sup>)</td>
<td><img src="/images/blog/ds_tut_lesson_1_quadratic.png" alt="" /></td>
</tr>
<tr>
<td>O(n<sup>3</sup>)</td>
<td><img src="/images/blog/ds_tut_lesson_1_cubic.png" alt="" /></td>
</tr>
<tr>
<td>O(2<sup>n</sup>)</td>
<td><img src="/images/blog/ds_tut_lesson_1_exponential_base_2.png" alt="" /></td>
</tr>
<tr>
<td>O(10<sup>n</sup>)</td>
<td><img src="/images/blog/ds_tut_lesson_1_exponential_base_10.png" alt="" /></td>
</tr>
</tbody>
</table>
<p>
	Now, consider that the assignment, comparisons, write and increment statements take a,b,c and d time units to execute, respectively. Also, suppose all arithmetic operations require e time units to execute. Now, consider the following two algorithms to find the sum of first n natural numbers:-
</p>
<p>
	<strong>Algorithm A</strong>
</p>
<ol>
<li>
		Set sum = 0 // 1 assignment
	</li>
<li>
		Set i = 0 // 1 assignment
	</li>
<li>
		While (i &lt;= n) // n comparisons</p>
<ol type="a">
<li>
				Set sum = sum + i // n arithmetic operations, n assignments
			</li>
<li>
				Increment i by 1 // n increments
			</li>
</ol>
</li>
<li>
		Display (sum) // 1 write
	</li>
</ol>
<p>
	<strong>Algorithm B</strong>
</p>
<ol>
<li>
		Set sum = (n x (n + 1))/2 // 3 arithmetic operations, 1 assignment
	</li>
<li>
		Display (sum) // 1 write
	</li>
</ol>
<p>
	Both Algorithm A and Algorithm B perform the same task, that is, both determine the sum of the first n natural numbers. Algorithm A adds each number iteratively to a variable sum. However, Algorithm B uses a formula to calculate the sum of the first n natural numbers.
</p>
<p>
	The execution time requires for Algorithm A is given by:-
</p>
<p>
	T = (n + 2) x a + (n x b) + (1 x c) + (n x d) + (n x e)
</p>
<p>
	T = an + 2n + bn + c + dn + en
</p>
<p>
	T = c + n(a + b + d + e + 2)
</p>
<p>
	As T is a linear function of n.
</p>
<p>
	Therefore, the algorithm is of the order O(n).
</p>
<p>
	Now determine the time required to execute algorithm B:-
</p>
<p>
	T = (1 x a) + (1 x c) + (3 x e)
</p>
<p>
	T = a + c + 3e
</p>
<p>
	Unlike Algorithm A, the time taken by Algorithm B is a constant, and does not depend on the value of n. Therefore, the algorithm is of the order O(1).
</p>
<p>
	Because Algorithm A is of the order O(n), the execution time of Algorithm A increases linearly with the value of n. However, Algorithm B is of the order O(1). Therefore, the execution time of Algorithm B is constant. This means that an increase in the value of n does not have any impact on the execution time of the algorithm. Therefore, however large the problem be, Algorithm A solves it in the same amount of time.
</p>
<p>
	Suppose for n = 10, both Algorithm A and B take 10 nanoseconds(ns) to execute. However when n is increased to 100, Algorithm A will take 100 ns to execute, but Algorithm B will take only 10 ns to execute, Similarly, when n is increased to 1000, Algorithm A will take 1000 ns to execute, but Algorithm B will take only 10 ns.
</p>
<p>
	This means that when the problem is very large in size, Algorithm B would prove to be much more efficient than Algorithm A.
</p>
<h5 id="cBestWorstAndAverageCaseEfficiency">Best, Worst, and Average Case Efficiency</h5>
<p>
	Suppose you have a list of names in which you have to search for a particular name. You have designed an algorithm that searches the name in the list of n elements, by comparing the name to be searched with each element in the list sequentially.
</p>
<p>
	The best case in this scenario would be if the first element in the list matches the name to be searched. The efficiency in that case would be expressed as O(1) because only one comparison was made.
</p>
<p>
	Similarly, the worst case in this scenario would be if the complete list is traversed and the element is found at the end of the list or is not found in the list. The efficiency in that case would be expressed as O(n) because n comparisons were made.
</p>
<p>
	Continuing with the same example, the average case efficiency can be obtained by finding the average number of comparisons. Here,
</p>
<p>
	Minimum number of comparisons = 1
</p>
<p>
	Maximum number of comparisons = n
</p>
<p>
	Therefore, average number of comparisons = (n + 1)/2
</p>
<p>
	(n + 1)2 is a linear function of n. Therefore, the average case efficiency will be expressed as O(n).
</p>
<p>
	The worst case efficiency of the preceding search program can be improved by using an alternate search algorithm that provides a better worst case efficiency.
</p>
<p>
	A search algorithm with a better worst case efficiency is binary search that provides an efficiency of O(log n) in the worst case. We will learn more about this algorithm in the subsequent tutorials.
</p>
<div class="tutnav"></div>
]]></content:encoded>
			<wfw:commentRss>http://maxotek.net/blog/data-structure-tutorial-lesson-1-role-of-algorithms-and-data-structure-in-problem-solving-t146.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Programming Tutorial &#8211; Lesson 1: Instructions</title>
		<link>http://maxotek.net/blog/programming-tutorial-lesson-1-instructions-t65.html</link>
		<comments>http://maxotek.net/blog/programming-tutorial-lesson-1-instructions-t65.html#comments</comments>
		<pubDate>Tue, 15 Jul 2008 14:19:36 +0000</pubDate>
		<dc:creator>partho</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://maxotek.net/blog/?p=65</guid>
		<description><![CDATA[Programming or coding as some call it, is the backbone of the software industry. It comprises of writing Programs &#8211; lines of instructions. These instructions are fed to the computer for processing and output. Sounds simple, doesn&#8217;t it? Well, trust me, its no Rocket science. But, I have seen students think of it as such [...]]]></description>
			<content:encoded><![CDATA[<p>Programming or coding as some call it, is the backbone of the software industry. It comprises of writing Programs &#8211; lines of instructions. These instructions are fed to the computer for processing and output. Sounds simple, doesn&#8217;t it? Well, trust me, its no Rocket science. But, I have seen students think of it as such and ultimately scraping the entire idea of learning how to program.</p>
<p>Many courses being taught at the higher levels of education, today, don&#8217;t start off at the absolute beginning of Programming. For a person who has no prior experience in programming, knowing the basics is very essential. In these series of tutorials, I will take you on the journey that I did, back at school and learned whatever little of Programming, that I know today. We will begin by using <a href="http://en.wikipedia.org/wiki/Logo_(programming_language)">LOGO</a>, the programming language. I believe it will impart good insights into the bare-bones of Programming.</p>
<p>We need to know a few terms before we begin using LOGO. Don&#8217;t worry these are no jargons, only the bare minimals.</p>
<h5>Instruction</h5>
<p>An instruction is a command to the computer which causes a certain action to be performed.</p>
<h5>Program</h5>
<p>Lines of instruction that are executed or processed by the computer one by one.</p>
<p>We will need the MSWLogo software to go any further. You can get it from <a href="http://www.softronix.com/download/mswlogo65.exe">here</a>. Once installed, start Microsoft Windows Logo from the start menu. The application is composed of a pair of Windows. The upper one with a white background and a Triangle in the middle is your drawing area.</p>
<p><img src="/images/blog/pgming_tut_lesson_1_logo_main_window.png" alt="" /></p>
<p>The lower one titled <strong>Commander</strong> is where you will write the instructions and feed it to the computer.</p>
<p><img src="/images/blog/pgming_tut_lesson_1_logo_commander_window.png" alt="" /></p>
<p>LOGO is a programming language that works on turtle graphics. A triangle shaped object (the Turtle) is used to draw lines &amp; curves on the screen. To draw these lines you instruct the turtle to move forward or backward and while doing so, it leaves a line behind. Try typing the instruction given below in the textbox to the right of the Execute button in the Commander window and then press enter or click on Execute.</p>

<div class="wp_syntax"><div class="code"><pre class="logo"><span style="color: #993333; font-weight: bold;">FORWARD</span> <span style="color: #cc66cc;">50</span></pre></div></div>

<p><img src="/images/blog/pgming_tut_lesson_1_example_1.png" alt="" /></p>
<p>As you can see, the turtle moved upward, or forward in its own orientation, leaving behind the black line. The instruction we provided is composed of two parts: <strong>FORWARD</strong> &#8211; the command to make the turtle go forward and <strong>50</strong> the distance it moved forward by. Now type in the following command and press enter.</p>

<div class="wp_syntax"><div class="code"><pre class="logo"><span style="color: #993333; font-weight: bold;">BACK</span> <span style="color: #cc66cc;">100</span></pre></div></div>

<p><img src="/images/blog/pgming_tut_lesson_1_example_2.png" alt="" /></p>
<p>The turtle comes back 100 units drawing over the old line and extending it by 50 at the bottom. Unlike in real life, an overwrite on the same line doesn&#8217;t make the line thicker. Let us try some more tricks, type in the following instruction and press enter.</p>

<div class="wp_syntax"><div class="code"><pre class="logo"><span style="color: #993333; font-weight: bold;">RIGHT</span> <span style="color: #cc66cc;">90</span></pre></div></div>

<p><img src="/images/blog/pgming_tut_lesson_1_example_3.png" alt="" /></p>
<p>This makes the turtle turn right (clockwise) by 90 degrees allowing us to draw a horizontal line. The <strong>RIGHT</strong> command is followed by an angle in degrees. The turtle can turn in any direction with this. But, we have a command to make it turn the other way too &#8211; the <strong>LEFT</strong> command. Now execute the following command.</p>

<div class="wp_syntax"><div class="code"><pre class="logo"><span style="color: #993333; font-weight: bold;">FD</span> <span style="color: #cc66cc;">100</span></pre></div></div>

<p><img src="/images/blog/pgming_tut_lesson_1_example_4.png" alt="" /></p>
<p>FD command is an alias (another name) for the FORWARD command and it provides the exact same functionality. The Aliases for the commands are given in the table below.</p>
<table class="joomlaTable" border="0">
<tbody>
<tr>
<th>Command</th>
<th>Alias</th>
</tr>
<tr>
<td>FORWARD</td>
<td>FD</td>
</tr>
<tr>
<td>BACK</td>
<td>BK</td>
</tr>
<tr>
<td>RIGHT</td>
<td>RT</td>
</tr>
<tr>
<td>LEFT</td>
<td>LT</td>
</tr>
</tbody>
</table>
<p>Lets complete our diagram by executing these instructions one by one.</p>

<div class="wp_syntax"><div class="code"><pre class="logo"><span style="color: #993333; font-weight: bold;">LT</span> <span style="color: #cc66cc;">90</span>
<span style="color: #993333; font-weight: bold;">FD</span> <span style="color: #cc66cc;">100</span>
<span style="color: #993333; font-weight: bold;">RT</span> <span style="color: #cc66cc;">270</span>
<span style="color: #993333; font-weight: bold;">FD</span> <span style="color: #cc66cc;">100</span></pre></div></div>

<p><img src="/images/blog/pgming_tut_lesson_1_example_5.png" alt="" /></p>
<p>There we have it; Mr. Paniter has drawn us our very own Square. The third instruction, RT 270 can be replaced by an equivalent LT 90. You can provide a negative value to the FD and BK commands to make them go the opposite way. You can even give negative angles or angles greater than 360 degrees and make the poor turtle go round and round (apparently, the slow dude turns so fast, we won&#8217;t be able to see it spin its head off, or maybe it has some sharp geomerty skills and does only one round).</p>
<h5>Drawing dashed lines or discontinous figures</h5>
<p>Did I mention how the turtle goes about drawing all those lines? The guy uses a pen! Not your average pen that runs out of ink every second day, though. Just as we raise our pen in between writing two words, you can make the turtle raise its pen and move to some place else. During this movement, it won&#8217;t draw a line. We use the <strong>PENUP</strong> command to raise the pen up. While in this state, moving in no direction will result in a line being drawn. To lower the pen use the <strong>PENDOWN </strong>command. When the pen has been lowered, lines will again begin to be drawn following an FD OR BK command (unless offcourse if you move it by 0 units).</p>
<p><strong>Note:</strong> <em>PENUP &amp; PENDOWN commands are not followed by any numbers. You can also use their aliases PU &amp; PD.</em></p>
<p>Clear the screen using the <strong>CS</strong> command and execute the following instruction to draw a dashed line.</p>

<div class="wp_syntax"><div class="code"><pre class="logo"><span style="color: #993333; font-weight: bold;">FD</span> <span style="color: #cc66cc;">30</span>
<span style="color: #993333; font-weight: bold;">PU</span>
<span style="color: #993333; font-weight: bold;">FD</span> <span style="color: #cc66cc;">30</span>
<span style="color: #993333; font-weight: bold;">PD</span>
<span style="color: #993333; font-weight: bold;">FD</span> <span style="color: #cc66cc;">30</span>
<span style="color: #993333; font-weight: bold;">PU</span>
<span style="color: #993333; font-weight: bold;">FD</span> <span style="color: #cc66cc;">30</span>
<span style="color: #993333; font-weight: bold;">PD</span>
<span style="color: #993333; font-weight: bold;">FD</span> <span style="color: #cc66cc;">30</span></pre></div></div>

<p><img src="/images/blog/pgming_tut_lesson_1_example_6.png" alt="" /></p>
<h5>Chaining Instuctions in one line</h5>
<p>You can write multiple instuctions in one line. For example, the dashed line can be drawn using the following command.</p>

<div class="wp_syntax"><div class="code"><pre class="logo">CS <span style="color: #993333; font-weight: bold;">FD</span> <span style="color: #cc66cc;">30</span> <span style="color: #993333; font-weight: bold;">PU</span> <span style="color: #993333; font-weight: bold;">FD</span> <span style="color: #cc66cc;">30</span> <span style="color: #993333; font-weight: bold;">PD</span> <span style="color: #993333; font-weight: bold;">FD</span> <span style="color: #cc66cc;">30</span> <span style="color: #993333; font-weight: bold;">PU</span> <span style="color: #993333; font-weight: bold;">FD</span> <span style="color: #cc66cc;">30</span> <span style="color: #993333; font-weight: bold;">PD</span> <span style="color: #993333; font-weight: bold;">FD</span> <span style="color: #cc66cc;">30</span></pre></div></div>

<h5>The REPEAT command</h5>
<p>The REPEAT command is used to repeat a series of instructions a given number of times. For example, you can use the following code to draw a square.</p>

<div class="wp_syntax"><div class="code"><pre class="logo"><span style="color: #993333; font-weight: bold;">REPEAT</span> <span style="color: #cc66cc;">4</span> <span style="color: #66cc66;">&#91;</span><span style="color: #993333; font-weight: bold;">FD</span> <span style="color: #cc66cc;">100</span> <span style="color: #993333; font-weight: bold;">RT</span> <span style="color: #cc66cc;">90</span><span style="color: #66cc66;">&#93;</span></pre></div></div>

<p>The dashed line can be drawn with the REPEAT command.</p>

<div class="wp_syntax"><div class="code"><pre class="logo">CS <span style="color: #993333; font-weight: bold;">REPEAT</span> <span style="color: #cc66cc;">3</span> <span style="color: #66cc66;">&#91;</span><span style="color: #993333; font-weight: bold;">PD</span> <span style="color: #993333; font-weight: bold;">FD</span> <span style="color: #cc66cc;">30</span> <span style="color: #993333; font-weight: bold;">PU</span> <span style="color: #993333; font-weight: bold;">FD</span> <span style="color: #cc66cc;">30</span><span style="color: #66cc66;">&#93;</span></pre></div></div>

<p>The REPEAT command is followed by a number specifying how many times to repeat the steps and the set of instructions to be repeated. The instructions to be repeated are enclosed between the square brackets [ and ].</p>
<h5>Syntax</h5>
<p>Syntax is the grammar of instructions. Every command has a syntax. For example, the FD command is followed by a number, whereas the syntax of PU &amp; PD commands don&#8217;t contain any numbers following them. We can express these syntax in general forms:-</p>
<table class="joomlaTable" border="0">
<tbody>
<tr>
<th>Command</th>
<th>Syntax</th>
</tr>
<tr>
<td>FD</td>
<td>FD distance</td>
</tr>
<tr>
<td>RT</td>
<td>RT angle</td>
</tr>
<tr>
<td>REPEAT</td>
<td>REPEAT count [ instructions ]</td>
</tr>
</tbody>
</table>
<p>Syntax helps us understand the structure of the command, how its written/used. Every Programming language has its own set of commands, statements &amp; their corresponding syntaxes.</p>
<p><strong>Note:</strong> <em>Description of statements is not relevant to the LOGO language. We will go through it in a later tutorial.</em></p>
<p>In this tutorial we have seen what instructions are and how to write them for the LOGO language. The LOGO language offers more than what we have covered here. But, our aim was only to get familiar with instructions. In the next tutorial we will be continuing our journey into the world of Programming.</p>
]]></content:encoded>
			<wfw:commentRss>http://maxotek.net/blog/programming-tutorial-lesson-1-instructions-t65.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

