mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
399 lines
31 KiB
HTML
399 lines
31 KiB
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
<html xmlns="http://www.w3.org/1999/xhtml">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
|
|
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
|
|
<meta name="generator" content="Doxygen 1.8.18"/>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
|
<title>Algorithms_in_C: project_euler/problem_22/sol1.c File Reference</title>
|
|
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="../../jquery.js"></script>
|
|
<script type="text/javascript" src="../../dynsections.js"></script>
|
|
<link href="../../navtree.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="../../resize.js"></script>
|
|
<script type="text/javascript" src="../../navtreedata.js"></script>
|
|
<script type="text/javascript" src="../../navtree.js"></script>
|
|
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="../../search/searchdata.js"></script>
|
|
<script type="text/javascript" src="../../search/search.js"></script>
|
|
<script type="text/x-mathjax-config">
|
|
MathJax.Hub.Config({
|
|
extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
|
|
jax: ["input/TeX","output/HTML-CSS"],
|
|
});
|
|
</script>
|
|
<script type="text/javascript" async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML/MathJax.js"></script>
|
|
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
|
|
</head>
|
|
<body>
|
|
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
|
|
<div id="titlearea">
|
|
<table cellspacing="0" cellpadding="0">
|
|
<tbody>
|
|
<tr style="height: 56px;">
|
|
<td id="projectalign" style="padding-left: 0.5em;">
|
|
<div id="projectname">Algorithms_in_C
|
|
 <span id="projectnumber">1.0.0</span>
|
|
</div>
|
|
<div id="projectbrief">Set of algorithms implemented in C.</div>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<!-- end header part -->
|
|
<!-- Generated by Doxygen 1.8.18 -->
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */
|
|
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
|
|
/* @license-end */
|
|
</script>
|
|
<script type="text/javascript" src="../../menudata.js"></script>
|
|
<script type="text/javascript" src="../../menu.js"></script>
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */
|
|
$(function() {
|
|
initMenu('../../',true,false,'search.php','Search');
|
|
$(document).ready(function() { init_search(); });
|
|
});
|
|
/* @license-end */</script>
|
|
<div id="main-nav"></div>
|
|
</div><!-- top -->
|
|
<div id="side-nav" class="ui-resizable side-nav-resizable">
|
|
<div id="nav-tree">
|
|
<div id="nav-tree-contents">
|
|
<div id="nav-sync" class="sync"></div>
|
|
</div>
|
|
</div>
|
|
<div id="splitbar" style="-moz-user-select:none;"
|
|
class="ui-resizable-handle">
|
|
</div>
|
|
</div>
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */
|
|
$(document).ready(function(){initNavTree('dd/d8b/problem__22_2sol1_8c.html','../../'); initResizable(); });
|
|
/* @license-end */
|
|
</script>
|
|
<div id="doc-content">
|
|
<!-- window showing the filter options -->
|
|
<div id="MSearchSelectWindow"
|
|
onmouseover="return searchBox.OnSearchSelectShow()"
|
|
onmouseout="return searchBox.OnSearchSelectHide()"
|
|
onkeydown="return searchBox.OnSearchSelectKey(event)">
|
|
</div>
|
|
|
|
<!-- iframe showing the search results (closed by default) -->
|
|
<div id="MSearchResultsWindow">
|
|
<iframe src="javascript:void(0)" frameborder="0"
|
|
name="MSearchResults" id="MSearchResults">
|
|
</iframe>
|
|
</div>
|
|
|
|
<div class="header">
|
|
<div class="summary">
|
|
<a href="#define-members">Macros</a> |
|
|
<a href="#func-members">Functions</a> </div>
|
|
<div class="headertitle">
|
|
<div class="title">sol1.c File Reference</div> </div>
|
|
</div><!--header-->
|
|
<div class="contents">
|
|
|
|
<p><a href="https://projecteuler.net/problem=22">Problem 22</a> solution
|
|
<a href="#details">More...</a></p>
|
|
<div class="textblock"><code>#include <stdio.h></code><br />
|
|
<code>#include <stdlib.h></code><br />
|
|
<code>#include <string.h></code><br />
|
|
<code>#include <time.h></code><br />
|
|
</div><div class="textblock"><div class="dynheader">
|
|
Include dependency graph for sol1.c:</div>
|
|
<div class="dyncontent">
|
|
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d6/d42/problem__22_2sol1_8c__incl.svg" width="334" height="127"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
|
|
</div>
|
|
</div>
|
|
</div><table class="memberdecls">
|
|
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="define-members"></a>
|
|
Macros</h2></td></tr>
|
|
<tr class="memitem:a6cb9b08aacb61416795ee78bfceacd38"><td class="memItemLeft" align="right" valign="top">#define </td><td class="memItemRight" valign="bottom"><a class="el" href="../../dd/d8b/problem__22_2sol1_8c.html#a6cb9b08aacb61416795ee78bfceacd38">MAX_NAMES</a>   6000</td></tr>
|
|
<tr class="separator:a6cb9b08aacb61416795ee78bfceacd38"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:afd709f201d7643c3909621f620ea648a"><td class="memItemLeft" align="right" valign="top">#define </td><td class="memItemRight" valign="bottom"><a class="el" href="../../dd/d8b/problem__22_2sol1_8c.html#afd709f201d7643c3909621f620ea648a">MAX_NAME_LEN</a>   20</td></tr>
|
|
<tr class="separator:afd709f201d7643c3909621f620ea648a"><td class="memSeparator" colspan="2"> </td></tr>
|
|
</table><table class="memberdecls">
|
|
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="func-members"></a>
|
|
Functions</h2></td></tr>
|
|
<tr class="memitem:a5bc3659aa0949ea33118c95b1dee5f63"><td class="memItemLeft" align="right" valign="top">void </td><td class="memItemRight" valign="bottom"><a class="el" href="../../dd/d8b/problem__22_2sol1_8c.html#a5bc3659aa0949ea33118c95b1dee5f63">shell_sort</a> (char <a class="el" href="../../df/dea/structdata.html">data</a>[][<a class="el" href="../../dd/d8b/problem__22_2sol1_8c.html#afd709f201d7643c3909621f620ea648a">MAX_NAME_LEN</a>], int LEN)</td></tr>
|
|
<tr class="separator:a5bc3659aa0949ea33118c95b1dee5f63"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:ae359b8a4656b164c91ef91a084c15c9d"><td class="memItemLeft" align="right" valign="top">void </td><td class="memItemRight" valign="bottom"><a class="el" href="../../dd/d8b/problem__22_2sol1_8c.html#ae359b8a4656b164c91ef91a084c15c9d">lazy_sort</a> (char <a class="el" href="../../df/dea/structdata.html">data</a>[][<a class="el" href="../../dd/d8b/problem__22_2sol1_8c.html#afd709f201d7643c3909621f620ea648a">MAX_NAME_LEN</a>], int LEN)</td></tr>
|
|
<tr class="separator:ae359b8a4656b164c91ef91a084c15c9d"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:a3c04138a5bfe5d72780bb7e82a18e627"><td class="memItemLeft" align="right" valign="top">int </td><td class="memItemRight" valign="bottom"><a class="el" href="../../dd/d8b/problem__22_2sol1_8c.html#a3c04138a5bfe5d72780bb7e82a18e627">main</a> (int argc, char **argv)</td></tr>
|
|
<tr class="separator:a3c04138a5bfe5d72780bb7e82a18e627"><td class="memSeparator" colspan="2"> </td></tr>
|
|
</table>
|
|
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
|
|
<div class="textblock"><p><a href="https://projecteuler.net/problem=22">Problem 22</a> solution </p>
|
|
<dl class="section author"><dt>Author</dt><dd><a href="https://github.com/kvedala">Krishna Vedala</a> </dd></dl>
|
|
</div><h2 class="groupheader">Macro Definition Documentation</h2>
|
|
<a id="afd709f201d7643c3909621f620ea648a"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#afd709f201d7643c3909621f620ea648a">◆ </a></span>MAX_NAME_LEN</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">#define MAX_NAME_LEN   20</td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
<p>Maximum length of each name </p>
|
|
|
|
</div>
|
|
</div>
|
|
<a id="a6cb9b08aacb61416795ee78bfceacd38"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#a6cb9b08aacb61416795ee78bfceacd38">◆ </a></span>MAX_NAMES</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">#define MAX_NAMES   6000</td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
<p>Maximum number of names to store </p>
|
|
|
|
</div>
|
|
</div>
|
|
<h2 class="groupheader">Function Documentation</h2>
|
|
<a id="ae359b8a4656b164c91ef91a084c15c9d"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#ae359b8a4656b164c91ef91a084c15c9d">◆ </a></span>lazy_sort()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">void lazy_sort </td>
|
|
<td>(</td>
|
|
<td class="paramtype">char </td>
|
|
<td class="paramname"><em>data</em>[][MAX_NAME_LEN], </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">int </td>
|
|
<td class="paramname"><em>LEN</em> </td>
|
|
</tr>
|
|
<tr>
|
|
<td></td>
|
|
<td>)</td>
|
|
<td></td><td></td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
<p>Alphabetical sorting using 'lazy sort' algorithm </p>
|
|
<div class="fragment"><div class="line"><a name="l00050"></a><span class="lineno"> 50</span> {</div>
|
|
<div class="line"><a name="l00051"></a><span class="lineno"> 51</span>  <span class="keywordtype">int</span> i, j;</div>
|
|
<div class="line"><a name="l00052"></a><span class="lineno"> 52</span>  <span class="keywordflow">for</span> (i = 0; i < LEN; i++)</div>
|
|
<div class="line"><a name="l00053"></a><span class="lineno"> 53</span>  {</div>
|
|
<div class="line"><a name="l00054"></a><span class="lineno"> 54</span>  <span class="keywordflow">for</span> (j = i + 1; j < LEN; j++)</div>
|
|
<div class="line"><a name="l00055"></a><span class="lineno"> 55</span>  {</div>
|
|
<div class="line"><a name="l00056"></a><span class="lineno"> 56</span>  <span class="keywordflow">if</span> (strcmp(<a class="code" href="../../df/dea/structdata.html">data</a>[i], <a class="code" href="../../df/dea/structdata.html">data</a>[j]) > 0)</div>
|
|
<div class="line"><a name="l00057"></a><span class="lineno"> 57</span>  {</div>
|
|
<div class="line"><a name="l00058"></a><span class="lineno"> 58</span>  <span class="keywordtype">char</span> tmp_buffer[<a class="code" href="../../dd/d8b/problem__22_2sol1_8c.html#afd709f201d7643c3909621f620ea648a">MAX_NAME_LEN</a>];</div>
|
|
<div class="line"><a name="l00059"></a><span class="lineno"> 59</span>  strcpy(tmp_buffer, <a class="code" href="../../df/dea/structdata.html">data</a>[i]);</div>
|
|
<div class="line"><a name="l00060"></a><span class="lineno"> 60</span>  strcpy(<a class="code" href="../../df/dea/structdata.html">data</a>[i], <a class="code" href="../../df/dea/structdata.html">data</a>[j]);</div>
|
|
<div class="line"><a name="l00061"></a><span class="lineno"> 61</span>  strcpy(<a class="code" href="../../df/dea/structdata.html">data</a>[j], tmp_buffer);</div>
|
|
<div class="line"><a name="l00062"></a><span class="lineno"> 62</span>  }</div>
|
|
<div class="line"><a name="l00063"></a><span class="lineno"> 63</span>  }</div>
|
|
<div class="line"><a name="l00064"></a><span class="lineno"> 64</span>  }</div>
|
|
<div class="line"><a name="l00065"></a><span class="lineno"> 65</span> <span class="preprocessor">#ifdef DEBUG</span></div>
|
|
<div class="line"><a name="l00066"></a><span class="lineno"> 66</span>  <span class="keywordflow">for</span> (i = 0; i < LEN; i++)</div>
|
|
<div class="line"><a name="l00067"></a><span class="lineno"> 67</span>  printf(<span class="stringliteral">"%s\t"</span>, <a class="code" href="../../df/dea/structdata.html">data</a>[i]);</div>
|
|
<div class="line"><a name="l00068"></a><span class="lineno"> 68</span> <span class="preprocessor">#endif</span></div>
|
|
<div class="line"><a name="l00069"></a><span class="lineno"> 69</span> }</div>
|
|
</div><!-- fragment -->
|
|
</div>
|
|
</div>
|
|
<a id="a3c04138a5bfe5d72780bb7e82a18e627"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#a3c04138a5bfe5d72780bb7e82a18e627">◆ </a></span>main()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">int main </td>
|
|
<td>(</td>
|
|
<td class="paramtype">int </td>
|
|
<td class="paramname"><em>argc</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">char ** </td>
|
|
<td class="paramname"><em>argv</em> </td>
|
|
</tr>
|
|
<tr>
|
|
<td></td>
|
|
<td>)</td>
|
|
<td></td><td></td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
<p>Main function </p>
|
|
<div class="fragment"><div class="line"><a name="l00073"></a><span class="lineno"> 73</span> {</div>
|
|
<div class="line"><a name="l00074"></a><span class="lineno"> 74</span>  <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> COUNT = 0;</div>
|
|
<div class="line"><a name="l00075"></a><span class="lineno"> 75</span>  <span class="keywordtype">char</span> *fname = <span class="stringliteral">"names.txt"</span>;</div>
|
|
<div class="line"><a name="l00076"></a><span class="lineno"> 76</span>  <span class="keywordtype">char</span> names[<a class="code" href="../../dd/d8b/problem__22_2sol1_8c.html#a6cb9b08aacb61416795ee78bfceacd38">MAX_NAMES</a>][<a class="code" href="../../dd/d8b/problem__22_2sol1_8c.html#afd709f201d7643c3909621f620ea648a">MAX_NAME_LEN</a>];</div>
|
|
<div class="line"><a name="l00077"></a><span class="lineno"> 77</span>  <span class="keywordtype">short</span> method = 0; <span class="comment">/* sorting algorithm to use. 0 = lazy, 1 = shell-sort */</span></div>
|
|
<div class="line"><a name="l00078"></a><span class="lineno"> 78</span>  </div>
|
|
<div class="line"><a name="l00079"></a><span class="lineno"> 79</span>  <span class="keywordflow">if</span> (argc == 2)</div>
|
|
<div class="line"><a name="l00080"></a><span class="lineno"> 80</span>  method = atoi(argv[1]);</div>
|
|
<div class="line"><a name="l00081"></a><span class="lineno"> 81</span>  </div>
|
|
<div class="line"><a name="l00082"></a><span class="lineno"> 82</span>  FILE *fp = fopen(fname, <span class="stringliteral">"rt"</span>);</div>
|
|
<div class="line"><a name="l00083"></a><span class="lineno"> 83</span>  <span class="keywordflow">if</span> (!fp)</div>
|
|
<div class="line"><a name="l00084"></a><span class="lineno"> 84</span>  {</div>
|
|
<div class="line"><a name="l00085"></a><span class="lineno"> 85</span>  perror(<span class="stringliteral">"Unable to open file"</span>);</div>
|
|
<div class="line"><a name="l00086"></a><span class="lineno"> 86</span>  <span class="keywordflow">return</span> -1;</div>
|
|
<div class="line"><a name="l00087"></a><span class="lineno"> 87</span>  }</div>
|
|
<div class="line"><a name="l00088"></a><span class="lineno"> 88</span>  </div>
|
|
<div class="line"><a name="l00089"></a><span class="lineno"> 89</span>  <span class="comment">/*</span></div>
|
|
<div class="line"><a name="l00090"></a><span class="lineno"> 90</span> <span class="comment"> * Loops to get total number of rows and columns in the file</span></div>
|
|
<div class="line"><a name="l00091"></a><span class="lineno"> 91</span> <span class="comment"> */</span></div>
|
|
<div class="line"><a name="l00092"></a><span class="lineno"> 92</span>  <span class="keywordflow">do</span></div>
|
|
<div class="line"><a name="l00093"></a><span class="lineno"> 93</span>  {</div>
|
|
<div class="line"><a name="l00094"></a><span class="lineno"> 94</span>  <span class="keywordtype">int</span> ret = fscanf(fp, <span class="stringliteral">"\"%[^\",]\","</span>, names[COUNT++]);</div>
|
|
<div class="line"><a name="l00095"></a><span class="lineno"> 95</span>  <span class="keywordflow">if</span> (ret <= 0)</div>
|
|
<div class="line"><a name="l00096"></a><span class="lineno"> 96</span>  <span class="keywordflow">continue</span>;</div>
|
|
<div class="line"><a name="l00097"></a><span class="lineno"> 97</span>  <span class="comment">// printf("%s\t", names[COUNT - 1]);</span></div>
|
|
<div class="line"><a name="l00098"></a><span class="lineno"> 98</span>  } <span class="keywordflow">while</span> (!feof(fp));</div>
|
|
<div class="line"><a name="l00099"></a><span class="lineno"> 99</span>  fclose(fp);</div>
|
|
<div class="line"><a name="l00100"></a><span class="lineno"> 100</span>  </div>
|
|
<div class="line"><a name="l00101"></a><span class="lineno"> 101</span>  printf(<span class="stringliteral">"\nTotal number of names: %lu\n"</span>, COUNT);</div>
|
|
<div class="line"><a name="l00102"></a><span class="lineno"> 102</span>  </div>
|
|
<div class="line"><a name="l00103"></a><span class="lineno"> 103</span>  <span class="keywordflow">if</span> (method == 0)</div>
|
|
<div class="line"><a name="l00104"></a><span class="lineno"> 104</span>  {</div>
|
|
<div class="line"><a name="l00105"></a><span class="lineno"> 105</span>  clock_t start_time = clock();</div>
|
|
<div class="line"><a name="l00106"></a><span class="lineno"> 106</span>  <a class="code" href="../../dd/d8b/problem__22_2sol1_8c.html#a5bc3659aa0949ea33118c95b1dee5f63">shell_sort</a>(names, COUNT);</div>
|
|
<div class="line"><a name="l00107"></a><span class="lineno"> 107</span>  clock_t end_time = clock();</div>
|
|
<div class="line"><a name="l00108"></a><span class="lineno"> 108</span>  printf(<span class="stringliteral">"\nShell sort: %.4g millisecond\n"</span>,</div>
|
|
<div class="line"><a name="l00109"></a><span class="lineno"> 109</span>  1e3 * (end_time - start_time) / CLOCKS_PER_SEC);</div>
|
|
<div class="line"><a name="l00110"></a><span class="lineno"> 110</span>  }</div>
|
|
<div class="line"><a name="l00111"></a><span class="lineno"> 111</span>  <span class="keywordflow">else</span> <span class="keywordflow">if</span> (method == 1)</div>
|
|
<div class="line"><a name="l00112"></a><span class="lineno"> 112</span>  {</div>
|
|
<div class="line"><a name="l00113"></a><span class="lineno"> 113</span>  clock_t start_time = clock();</div>
|
|
<div class="line"><a name="l00114"></a><span class="lineno"> 114</span>  <a class="code" href="../../dd/d8b/problem__22_2sol1_8c.html#ae359b8a4656b164c91ef91a084c15c9d">lazy_sort</a>(names, COUNT);</div>
|
|
<div class="line"><a name="l00115"></a><span class="lineno"> 115</span>  clock_t end_time = clock();</div>
|
|
<div class="line"><a name="l00116"></a><span class="lineno"> 116</span>  printf(<span class="stringliteral">"\nLazy sort: %.4g millisecond\n"</span>,</div>
|
|
<div class="line"><a name="l00117"></a><span class="lineno"> 117</span>  1e3 * (end_time - start_time) / CLOCKS_PER_SEC);</div>
|
|
<div class="line"><a name="l00118"></a><span class="lineno"> 118</span>  }</div>
|
|
<div class="line"><a name="l00119"></a><span class="lineno"> 119</span>  </div>
|
|
<div class="line"><a name="l00120"></a><span class="lineno"> 120</span>  <span class="keywordtype">long</span> sum_score = 0;</div>
|
|
<div class="line"><a name="l00121"></a><span class="lineno"> 121</span>  clock_t start_time = clock();</div>
|
|
<div class="line"><a name="l00122"></a><span class="lineno"> 122</span>  <span class="keywordtype">int</span> i;</div>
|
|
<div class="line"><a name="l00123"></a><span class="lineno"> 123</span>  </div>
|
|
<div class="line"><a name="l00124"></a><span class="lineno"> 124</span> <span class="preprocessor">#ifdef _OPENMP</span></div>
|
|
<div class="line"><a name="l00125"></a><span class="lineno"> 125</span> <span class="preprocessor">#pragma omp parallel for schedule(runtime) reduction(+ : sum_score)</span></div>
|
|
<div class="line"><a name="l00126"></a><span class="lineno"> 126</span> <span class="preprocessor">#endif</span></div>
|
|
<div class="line"><a name="l00127"></a><span class="lineno"> 127</span> <span class="preprocessor">#ifdef DEBUG</span></div>
|
|
<div class="line"><a name="l00128"></a><span class="lineno"> 128</span>  <span class="keywordflow">for</span> (i = 935; i < 940; i++)</div>
|
|
<div class="line"><a name="l00129"></a><span class="lineno"> 129</span> #<span class="keywordflow">else</span></div>
|
|
<div class="line"><a name="l00130"></a><span class="lineno"> 130</span>  <span class="keywordflow">for</span> (i = 0; i < COUNT; i++)</div>
|
|
<div class="line"><a name="l00131"></a><span class="lineno"> 131</span> #endif</div>
|
|
<div class="line"><a name="l00132"></a><span class="lineno"> 132</span>  {</div>
|
|
<div class="line"><a name="l00133"></a><span class="lineno"> 133</span>  <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> score = 0;</div>
|
|
<div class="line"><a name="l00134"></a><span class="lineno"> 134</span>  <span class="comment">/* score the alphabets in i^th name */</span></div>
|
|
<div class="line"><a name="l00135"></a><span class="lineno"> 135</span>  <span class="keywordflow">for</span> (<span class="keywordtype">int</span> j = 0; names[i][j] != <span class="charliteral">'\0'</span>; j++)</div>
|
|
<div class="line"><a name="l00136"></a><span class="lineno"> 136</span>  score += names[i][j] - <span class="charliteral">'A'</span> +</div>
|
|
<div class="line"><a name="l00137"></a><span class="lineno"> 137</span>  1; <span class="comment">/* convert ASCII character to integer score */</span></div>
|
|
<div class="line"><a name="l00138"></a><span class="lineno"> 138</span>  sum_score += score * (i + 1);</div>
|
|
<div class="line"><a name="l00139"></a><span class="lineno"> 139</span> <span class="preprocessor">#ifdef DEBUG</span></div>
|
|
<div class="line"><a name="l00140"></a><span class="lineno"> 140</span>  printf(<span class="stringliteral">"Name: %s\tScore: %u x %u = %lu\n"</span>, names[i], score, i + 1,</div>
|
|
<div class="line"><a name="l00141"></a><span class="lineno"> 141</span>  (<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span>)score * (i + 1));</div>
|
|
<div class="line"><a name="l00142"></a><span class="lineno"> 142</span> <span class="preprocessor">#endif</span></div>
|
|
<div class="line"><a name="l00143"></a><span class="lineno"> 143</span>  }</div>
|
|
<div class="line"><a name="l00144"></a><span class="lineno"> 144</span>  clock_t end_time = clock();</div>
|
|
<div class="line"><a name="l00145"></a><span class="lineno"> 145</span>  printf(<span class="stringliteral">"Scoring time: %.4g millisecond\n"</span>,</div>
|
|
<div class="line"><a name="l00146"></a><span class="lineno"> 146</span>  1e3 * (end_time - start_time) / CLOCKS_PER_SEC);</div>
|
|
<div class="line"><a name="l00147"></a><span class="lineno"> 147</span>  </div>
|
|
<div class="line"><a name="l00148"></a><span class="lineno"> 148</span>  printf(<span class="stringliteral">"Total Score = %lu\n"</span>, sum_score);</div>
|
|
<div class="line"><a name="l00149"></a><span class="lineno"> 149</span>  </div>
|
|
<div class="line"><a name="l00150"></a><span class="lineno"> 150</span>  <span class="keywordflow">return</span> 0;</div>
|
|
<div class="line"><a name="l00151"></a><span class="lineno"> 151</span> }</div>
|
|
</div><!-- fragment --><div class="dynheader">
|
|
Here is the call graph for this function:</div>
|
|
<div class="dyncontent">
|
|
<div class="center"><iframe scrolling="no" frameborder="0" src="../../dd/d8b/problem__22_2sol1_8c_a3c04138a5bfe5d72780bb7e82a18e627_cgraph.svg" width="188" height="88"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
|
|
</div>
|
|
</div>
|
|
|
|
</div>
|
|
</div>
|
|
<a id="a5bc3659aa0949ea33118c95b1dee5f63"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#a5bc3659aa0949ea33118c95b1dee5f63">◆ </a></span>shell_sort()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">void shell_sort </td>
|
|
<td>(</td>
|
|
<td class="paramtype">char </td>
|
|
<td class="paramname"><em>data</em>[][MAX_NAME_LEN], </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">int </td>
|
|
<td class="paramname"><em>LEN</em> </td>
|
|
</tr>
|
|
<tr>
|
|
<td></td>
|
|
<td>)</td>
|
|
<td></td><td></td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
<p>Alphabetical sorting using 'shell sort' algorithm </p>
|
|
<div class="fragment"><div class="line"><a name="l00021"></a><span class="lineno"> 21</span> {</div>
|
|
<div class="line"><a name="l00022"></a><span class="lineno"> 22</span>  <span class="keyword">const</span> <span class="keywordtype">int</span> gaps[] = {701, 301, 132, 57, 23, 10, 4, 1};</div>
|
|
<div class="line"><a name="l00023"></a><span class="lineno"> 23</span>  <span class="keyword">const</span> <span class="keywordtype">int</span> gap_len = 8;</div>
|
|
<div class="line"><a name="l00024"></a><span class="lineno"> 24</span>  <span class="keywordtype">int</span> i, j, g;</div>
|
|
<div class="line"><a name="l00025"></a><span class="lineno"> 25</span>  </div>
|
|
<div class="line"><a name="l00026"></a><span class="lineno"> 26</span>  <span class="keywordflow">for</span> (g = 0; g < gap_len; g++)</div>
|
|
<div class="line"><a name="l00027"></a><span class="lineno"> 27</span>  {</div>
|
|
<div class="line"><a name="l00028"></a><span class="lineno"> 28</span>  <span class="keywordtype">int</span> gap = gaps[g];</div>
|
|
<div class="line"><a name="l00029"></a><span class="lineno"> 29</span>  <span class="keywordflow">for</span> (i = gap; i < LEN; i++)</div>
|
|
<div class="line"><a name="l00030"></a><span class="lineno"> 30</span>  {</div>
|
|
<div class="line"><a name="l00031"></a><span class="lineno"> 31</span>  <span class="keywordtype">char</span> tmp_buffer[<a class="code" href="../../dd/d8b/problem__22_2sol1_8c.html#afd709f201d7643c3909621f620ea648a">MAX_NAME_LEN</a>];</div>
|
|
<div class="line"><a name="l00032"></a><span class="lineno"> 32</span>  strcpy(tmp_buffer, <a class="code" href="../../df/dea/structdata.html">data</a>[i]);</div>
|
|
<div class="line"><a name="l00033"></a><span class="lineno"> 33</span>  </div>
|
|
<div class="line"><a name="l00034"></a><span class="lineno"> 34</span>  <span class="keywordflow">for</span> (j = i; j >= gap && strcmp(<a class="code" href="../../df/dea/structdata.html">data</a>[j - gap], tmp_buffer) > 0;</div>
|
|
<div class="line"><a name="l00035"></a><span class="lineno"> 35</span>  j -= gap)</div>
|
|
<div class="line"><a name="l00036"></a><span class="lineno"> 36</span>  strcpy(<a class="code" href="../../df/dea/structdata.html">data</a>[j], <a class="code" href="../../df/dea/structdata.html">data</a>[j - gap]);</div>
|
|
<div class="line"><a name="l00037"></a><span class="lineno"> 37</span>  strcpy(<a class="code" href="../../df/dea/structdata.html">data</a>[j], tmp_buffer);</div>
|
|
<div class="line"><a name="l00038"></a><span class="lineno"> 38</span>  }</div>
|
|
<div class="line"><a name="l00039"></a><span class="lineno"> 39</span>  }</div>
|
|
<div class="line"><a name="l00040"></a><span class="lineno"> 40</span> <span class="preprocessor">#ifdef DEBUG</span></div>
|
|
<div class="line"><a name="l00041"></a><span class="lineno"> 41</span>  <span class="keywordflow">for</span> (i = 0; i < LEN; i++)</div>
|
|
<div class="line"><a name="l00042"></a><span class="lineno"> 42</span>  printf(<span class="stringliteral">"%s\t"</span>, <a class="code" href="../../df/dea/structdata.html">data</a>[i]);</div>
|
|
<div class="line"><a name="l00043"></a><span class="lineno"> 43</span> <span class="preprocessor">#endif</span></div>
|
|
<div class="line"><a name="l00044"></a><span class="lineno"> 44</span> }</div>
|
|
</div><!-- fragment -->
|
|
</div>
|
|
</div>
|
|
</div><!-- contents -->
|
|
</div><!-- doc-content -->
|
|
<div class="ttc" id="aproblem__22_2sol1_8c_html_a5bc3659aa0949ea33118c95b1dee5f63"><div class="ttname"><a href="../../dd/d8b/problem__22_2sol1_8c.html#a5bc3659aa0949ea33118c95b1dee5f63">shell_sort</a></div><div class="ttdeci">void shell_sort(char data[][MAX_NAME_LEN], int LEN)</div><div class="ttdef"><b>Definition:</b> sol1.c:20</div></div>
|
|
<div class="ttc" id="astructdata_html"><div class="ttname"><a href="../../df/dea/structdata.html">data</a></div><div class="ttdef"><b>Definition:</b> prime_factoriziation.c:25</div></div>
|
|
<div class="ttc" id="aproblem__22_2sol1_8c_html_ae359b8a4656b164c91ef91a084c15c9d"><div class="ttname"><a href="../../dd/d8b/problem__22_2sol1_8c.html#ae359b8a4656b164c91ef91a084c15c9d">lazy_sort</a></div><div class="ttdeci">void lazy_sort(char data[][MAX_NAME_LEN], int LEN)</div><div class="ttdef"><b>Definition:</b> sol1.c:49</div></div>
|
|
<div class="ttc" id="aproblem__22_2sol1_8c_html_afd709f201d7643c3909621f620ea648a"><div class="ttname"><a href="../../dd/d8b/problem__22_2sol1_8c.html#afd709f201d7643c3909621f620ea648a">MAX_NAME_LEN</a></div><div class="ttdeci">#define MAX_NAME_LEN</div><div class="ttdef"><b>Definition:</b> sol1.c:15</div></div>
|
|
<div class="ttc" id="aproblem__22_2sol1_8c_html_a6cb9b08aacb61416795ee78bfceacd38"><div class="ttname"><a href="../../dd/d8b/problem__22_2sol1_8c.html#a6cb9b08aacb61416795ee78bfceacd38">MAX_NAMES</a></div><div class="ttdeci">#define MAX_NAMES</div><div class="ttdef"><b>Definition:</b> sol1.c:14</div></div>
|
|
<!-- start footer part -->
|
|
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
|
|
<ul>
|
|
<li class="navelem"><a class="el" href="../../dir_b3d331eabd4234f247030c297aaf021c.html">project_euler</a></li><li class="navelem"><a class="el" href="../../dir_2abe2c0880d3fb0dba0f784bee3fc9e0.html">problem_22</a></li><li class="navelem"><a class="el" href="../../dd/d8b/problem__22_2sol1_8c.html">sol1.c</a></li>
|
|
<li class="footer">Generated on Sun Jun 28 2020 15:07:07 for Algorithms_in_C by
|
|
<a href="http://www.doxygen.org/index.html">
|
|
<img class="footer" src="../../doxygen.png" alt="doxygen"/></a> 1.8.18 </li>
|
|
</ul>
|
|
</div>
|
|
</body>
|
|
</html>
|