{"id":1247,"date":"2015-03-14T00:01:13","date_gmt":"2015-03-14T07:01:13","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1247"},"modified":"2015-03-21T07:25:49","modified_gmt":"2015-03-21T14:25:49","slug":"sorting-it-all-out","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1247","title":{"rendered":"Sorting It All Out"},"content":{"rendered":"<p>Computers are good at performing repetitive tasks; doing the same boring nonsense over and over. Two great examples are searching and sorting.<br \/>\n<!--more--><br \/>\nIn my C programming books, I demonstrate the most basic and obvious type of sorting technique: The bubble sort. It&#8217;s a brute-force method of comparing each item in a list to every other item in the list. Items out of place are swapped, and the whole process repeats.<\/p>\n<p>For simple, small lists, the bubble sort works fine. I demonstrate such a sort in Chapter 12 of my book, <em>Beginning Programming with C For Dummies<\/em>. A sort of 40 random integers is presented as the solution for the book&#8217;s <a href=\"http:\/\/www.c-for-dummies.com\/begc4d\/exercises\/ex1214.php\">Exercise 12-14<\/a>. It works well and demonstrates how a bubble sort works. And it&#8217;s pretty fast for sorting 40 values.<\/p>\n<p>How about creating and sorting an array of 100,000 random integers?<\/p>\n<p>A computer is quite content to sort 100,000 integers, although a bubble sort probably isn&#8217;t the most efficient way to order those values. (And at 4 bytes of storage per <em>int<\/em>, that&#8217;s nearly half a megabyte of data.)<\/p>\n<p>The change to the source code for Exercise 12-14 is simple: Modify the <code>SIZE<\/code> constant, changing it from 40 to 100000. To make the code run faster, I removed the output statements; displaying output slows down any program, but more importantly watching 100,000 sorted, random integers fly up the terminal screen is meaningless.<\/p>\n<p>Here is the final code:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;time.h&gt;\r\n\r\n#define SIZE 100000\r\n\r\nint main()\r\n{\r\n    int bubble[SIZE];\r\n    int inner,outer,temp,x;\r\n\r\n    srand((unsigned)time(NULL));\r\n    <span class=\"comments\">\/* populate array *\/<\/span>\r\n    for(x=0;x&lt;SIZE;x++)\r\n    {\r\n        bubble[x] = rand();\r\n        bubble[x] = (bubble[x] % 100)+1;\r\n    }\r\n    printf(\"%d element array created.\\n\",SIZE);\r\n\r\n    <span class=\"comments\">\/* bubble sort *\/<\/span>\r\n    printf(\"Sorting . . . \");\r\n    for(outer=0;outer&lt;SIZE-1;outer++)\r\n    {\r\n        for(inner=outer+1;inner&lt;=SIZE;inner++)\r\n        {\r\n            if(bubble[outer] &gt; bubble[inner])\r\n            {\r\n                temp=bubble[outer];\r\n                bubble[outer] = bubble[inner];\r\n                bubble[inner] = temp;\r\n            }\r\n        }\r\n    }\r\n    printf(\"Done!\\n\");\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Compile and run the program to see how long your computer takes to do a bubble sort of 100,000 integers. On a Unix terminal (Mac OS X, Linux, or CYGWIN) use the <em>time<\/em> command, followed by the compiled program name, such as:<\/p>\n<pre><code>$ time blog0314<\/code><\/pre>\n<p>Assuming that <code>blog0314<\/code> is the program generated by this Lesson&#8217;s source code, here&#8217;s the sample output on my screen:<\/p>\n<pre><code>100000 element array created.\r\nSorting . . . Done!\r\n\r\nreal\t0m9.910s\r\nuser\t0m9.899s\r\nsys\t0m0.011s<\/code><\/pre>\n<p>Nearly 10 seconds (9.910) elapsed between the time the first string was displayed and the program completed. That&#8217;s probably much faster than you (as a human) could sort the values, but it&#8217;s near an eternity for the computer. That&#8217;s because the bubble sort is inefficient.<\/p>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1258\">Next week<\/a> I&#8217;ll show you another way to sort that&#8217;s much, much faster. Or I should write, <em>quicker<\/em>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There has to be a better way to get to A to Z. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1247\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-1247","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1247","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1247"}],"version-history":[{"count":9,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1247\/revisions"}],"predecessor-version":[{"id":1297,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1247\/revisions\/1297"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1247"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1247"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1247"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}