{"id":1258,"date":"2015-03-21T00:01:09","date_gmt":"2015-03-21T07:01:09","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1258"},"modified":"2015-03-28T07:20:54","modified_gmt":"2015-03-28T14:20:54","slug":"the-quicksort","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1258","title":{"rendered":"The Quicksort"},"content":{"rendered":"<p>Computers excel at searching and sorting. That, and they can occasionally screw up a phone bill.<br \/>\n<!--more--><br \/>\nFor years, programmers have tried to improve upon the basic search and sort operations. Algorithms exist to search and sort faster. One of the more popular sorting algorithms is the quick sort. It&#8217;s so popular, the space between the words was evicted, so it&#8217;s known as <em>quicksort<\/em>.<\/p>\n<p>I&#8217;ll demonstrated how the quicksort works in detail in next week&#8217;s Lesson. The details are interesting, but not vital to know because a quicksort function is available in the standard C library: <em>qsort()<\/em><\/p>\n<p>The <em>qsort()<\/em> function is defined in the <code>stdlib.h<\/code> header file. It swallows four arguments:<\/p>\n<p><code>*base<\/code> The address of the array to be sorted<\/p>\n<p><code>nel<\/code> The number of elements in the array<\/p>\n<p><code>width<\/code> The size of each element, generally obtained by using the <em>sizeof<\/em> operator<\/p>\n<p><code>compar<\/code> The name of a function that determines how the array elements are compared<\/p>\n<p>Possibly the weirdest argument is <code>compar<\/code>, which is simply the name of another function elsewhere the code. (Internally, it&#8217;s the address of that function, which is a topic I&#8217;ll save for a future Lesson.)<\/p>\n<p>The <code>compar<\/code> function, which is traditionally named <em>compare()<\/em> has a very specific format. Here&#8217;s what&#8217;s most often used:<\/p>\n<pre class=\"screen\">\r\nint compare(const void *a, const void *b)\r\n{\r\n    return( *(int *)a - *(int *)b);\r\n}<\/pre>\n<p>You can change the function&#8217;s insides, and the variables used in the function (<code>a<\/code> and <code>b<\/code> above) must match the array&#8217;s variable type. Otherwise, the return value is less than zero, zero, or greater than zero depending on how the two values compare. Using the minus operator, as shown above, is how just about everyone does the comparison for an array of values sorted least to greatest.<\/p>\n<p>Below is updated code from <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1247\">last week&#8217;s Lesson<\/a>. This code uses the <em>qsort()<\/em> function instead of a bubble sort.<\/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 compare(const void *a, const void *b);\r\n\r\nint main()\r\n{\r\n    int qarray[SIZE];\r\n    int 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        qarray[x] = rand();\r\n        qarray[x] = (qarray[x] % 100)+1;\r\n    }\r\n    printf(\"%d element array created.\\n\",SIZE);\r\n\r\n    <span class=\"comments\">\/* quicksort *\/<\/span>\r\n    printf(\"Sorting . . . \");\r\n    qsort(qarray,SIZE,sizeof(int),compare);\r\n    puts(\"done!\");\r\n\r\n    return(0);\r\n}\r\n\r\nint compare(const void *a, const void *b)\r\n{\r\n    return( *(int *)a - *(int *)b);\r\n}<\/pre>\n<p>Here is the program&#8217;s output when monitored by the <em>time<\/em> command:<\/p>\n<pre><code>100000 element array created.\r\nSorting . . . done!\r\n\r\nreal\t0m0.026s\r\nuser\t0m0.007s\r\nsys\t0m0.001s<\/code><\/pre>\n<p>For reference, here is the output from last week&#8217;s code, which used a bubble sort:<\/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>As you can see, the <em>qsort()<\/em> is just insanely more efficient at its job than the bubble sort; it&#8217;s more than 9 seconds faster on my machine.<\/p>\n<p>If you want to reverse the sort, change the variable order in the <em>compare()<\/em> function, swapping <code>b<\/code> for <code>a<\/code> in the comparison:<\/p>\n<pre class=\"screen\">\r\nint compare(const void *a, const void *b)\r\n{\r\n    return( *(int *)b - *(int *)a);\r\n}<\/pre>\n<p>Of course, you have to modify the code to display all 100,000 values to prove that the sort is reversed. Trust me: It works.<\/p>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1269\">Next week<\/a> I&#8217;ll cover how the quicksort works. It&#8217;s really rather ingenious.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yes, it&#8217;s much faster than a bubble sort. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1258\">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-1258","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\/1258","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=1258"}],"version-history":[{"count":9,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1258\/revisions"}],"predecessor-version":[{"id":1307,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1258\/revisions\/1307"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1258"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1258"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1258"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}