mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
382 lines
29 KiB
HTML
382 lines
29 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" lang="en-US">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
|
|
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
|
|
<meta name="generator" content="Doxygen 1.9.5"/>
|
|
<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 id="projectrow">
|
|
<td id="projectalign">
|
|
<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.9.5 -->
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
|
|
var searchBox = new SearchBox("searchBox", "../../search/",'.html');
|
|
/* @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:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
|
|
$(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:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
|
|
$(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">
|
|
<div id="MSearchResults">
|
|
<div class="SRPage">
|
|
<div id="SRIndex">
|
|
<div id="SRResults"></div>
|
|
<div class="SRStatus" id="Loading">Loading...</div>
|
|
<div class="SRStatus" id="Searching">Searching...</div>
|
|
<div class="SRStatus" id="NoMatches">No Matches</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</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" target="_blank">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 />
|
|
<code>#include <omp.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="416" height="124"><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 id="define-members" name="define-members"></a>
|
|
Macros</h2></td></tr>
|
|
<tr class="memitem:a6cb9b08aacb61416795ee78bfceacd38"><td class="memItemLeft" align="right" valign="top"><a id="a6cb9b08aacb61416795ee78bfceacd38" name="a6cb9b08aacb61416795ee78bfceacd38"></a>
|
|
#define </td><td class="memItemRight" valign="bottom"><b>MAX_NAMES</b>   6000</td></tr>
|
|
<tr class="memdesc:a6cb9b08aacb61416795ee78bfceacd38"><td class="mdescLeft"> </td><td class="mdescRight">Maximum number of names to store. <br /></td></tr>
|
|
<tr class="separator:a6cb9b08aacb61416795ee78bfceacd38"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:afd709f201d7643c3909621f620ea648a"><td class="memItemLeft" align="right" valign="top"><a id="afd709f201d7643c3909621f620ea648a" name="afd709f201d7643c3909621f620ea648a"></a>
|
|
#define </td><td class="memItemRight" valign="bottom"><b>MAX_NAME_LEN</b>   20</td></tr>
|
|
<tr class="memdesc:afd709f201d7643c3909621f620ea648a"><td class="mdescLeft"> </td><td class="mdescRight">Maximum length of each name. <br /></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 id="func-members" 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 <a class="el" href="../../df/d27/alaw_8c.html#a05b49c662c073f89e86804f7856622a0">LEN</a>)</td></tr>
|
|
<tr class="memdesc:a5bc3659aa0949ea33118c95b1dee5f63"><td class="mdescLeft"> </td><td class="mdescRight">Alphabetical sorting using 'shell sort' algorithm. <a href="../../dd/d8b/problem__22_2sol1_8c.html#a5bc3659aa0949ea33118c95b1dee5f63">More...</a><br /></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 <a class="el" href="../../df/d27/alaw_8c.html#a05b49c662c073f89e86804f7856622a0">LEN</a>)</td></tr>
|
|
<tr class="memdesc:ae359b8a4656b164c91ef91a084c15c9d"><td class="mdescLeft"> </td><td class="mdescRight">Alphabetical sorting using 'lazy sort' algorithm. <a href="../../dd/d8b/problem__22_2sol1_8c.html#ae359b8a4656b164c91ef91a084c15c9d">More...</a><br /></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="memdesc:a3c04138a5bfe5d72780bb7e82a18e627"><td class="mdescLeft"> </td><td class="mdescRight">Main function. <a href="../../dd/d8b/problem__22_2sol1_8c.html#a3c04138a5bfe5d72780bb7e82a18e627">More...</a><br /></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" target="_blank">Problem 22</a> solution </p>
|
|
<dl class="section author"><dt>Author</dt><dd><a href="https://github.com/kvedala" target="_blank">Krishna Vedala</a> </dd></dl>
|
|
</div><h2 class="groupheader">Function Documentation</h2>
|
|
<a id="ae359b8a4656b164c91ef91a084c15c9d" name="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"><span class="lineno"> 49</span>{</div>
|
|
<div class="line"><span class="lineno"> 50</span> <span class="keywordtype">int</span> i, j;</div>
|
|
<div class="line"><span class="lineno"> 51</span> <span class="keywordflow">for</span> (i = 0; i < <a class="code hl_define" href="../../df/d27/alaw_8c.html#a05b49c662c073f89e86804f7856622a0">LEN</a>; i++)</div>
|
|
<div class="line"><span class="lineno"> 52</span> {</div>
|
|
<div class="line"><span class="lineno"> 53</span> <span class="keywordflow">for</span> (j = i + 1; j < <a class="code hl_define" href="../../df/d27/alaw_8c.html#a05b49c662c073f89e86804f7856622a0">LEN</a>; j++)</div>
|
|
<div class="line"><span class="lineno"> 54</span> {</div>
|
|
<div class="line"><span class="lineno"> 55</span> <span class="keywordflow">if</span> (strcmp(<a class="code hl_struct" href="../../df/dea/structdata.html">data</a>[i], <a class="code hl_struct" href="../../df/dea/structdata.html">data</a>[j]) > 0)</div>
|
|
<div class="line"><span class="lineno"> 56</span> {</div>
|
|
<div class="line"><span class="lineno"> 57</span> <span class="keywordtype">char</span> tmp_buffer[<a class="code hl_define" href="../../dd/d8b/problem__22_2sol1_8c.html#afd709f201d7643c3909621f620ea648a">MAX_NAME_LEN</a>];</div>
|
|
<div class="line"><span class="lineno"> 58</span> strcpy(tmp_buffer, <a class="code hl_struct" href="../../df/dea/structdata.html">data</a>[i]);</div>
|
|
<div class="line"><span class="lineno"> 59</span> strcpy(<a class="code hl_struct" href="../../df/dea/structdata.html">data</a>[i], <a class="code hl_struct" href="../../df/dea/structdata.html">data</a>[j]);</div>
|
|
<div class="line"><span class="lineno"> 60</span> strcpy(<a class="code hl_struct" href="../../df/dea/structdata.html">data</a>[j], tmp_buffer);</div>
|
|
<div class="line"><span class="lineno"> 61</span> }</div>
|
|
<div class="line"><span class="lineno"> 62</span> }</div>
|
|
<div class="line"><span class="lineno"> 63</span> }</div>
|
|
<div class="line"><span class="lineno"> 64</span><span class="preprocessor">#ifdef DEBUG</span></div>
|
|
<div class="line"><span class="lineno"> 65</span> <span class="keywordflow">for</span> (i = 0; i < <a class="code hl_define" href="../../df/d27/alaw_8c.html#a05b49c662c073f89e86804f7856622a0">LEN</a>; i++) printf(<span class="stringliteral">"%s\t"</span>, <a class="code hl_struct" href="../../df/dea/structdata.html">data</a>[i]);</div>
|
|
<div class="line"><span class="lineno"> 66</span><span class="preprocessor">#endif</span></div>
|
|
<div class="line"><span class="lineno"> 67</span>}</div>
|
|
<div class="ttc" id="aalaw_8c_html_a05b49c662c073f89e86804f7856622a0"><div class="ttname"><a href="../../df/d27/alaw_8c.html#a05b49c662c073f89e86804f7856622a0">LEN</a></div><div class="ttdeci">#define LEN</div><div class="ttdoc">Linear input code | Compressed code | Linear output code ---------------—+--------------—+-----------...</div><div class="ttdef"><b>Definition:</b> alaw.c:28</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="ttdoc">Maximum length of each name.</div><div class="ttdef"><b>Definition:</b> sol1.c:15</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><!-- fragment -->
|
|
</div>
|
|
</div>
|
|
<a id="a3c04138a5bfe5d72780bb7e82a18e627" name="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"><span class="lineno"> 71</span>{</div>
|
|
<div class="line"><span class="lineno"> 72</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> COUNT = 0;</div>
|
|
<div class="line"><span class="lineno"> 73</span> <span class="keywordtype">char</span> *fname = <span class="stringliteral">"names.txt"</span>;</div>
|
|
<div class="line"><span class="lineno"> 74</span> <span class="keywordtype">char</span> names[<a class="code hl_define" href="../../dd/d8b/problem__22_2sol1_8c.html#a6cb9b08aacb61416795ee78bfceacd38">MAX_NAMES</a>][<a class="code hl_define" href="../../dd/d8b/problem__22_2sol1_8c.html#afd709f201d7643c3909621f620ea648a">MAX_NAME_LEN</a>];</div>
|
|
<div class="line"><span class="lineno"> 75</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"><span class="lineno"> 76</span> </div>
|
|
<div class="line"><span class="lineno"> 77</span> <span class="keywordflow">if</span> (argc == 2)</div>
|
|
<div class="line"><span class="lineno"> 78</span> method = atoi(argv[1]);</div>
|
|
<div class="line"><span class="lineno"> 79</span> </div>
|
|
<div class="line"><span class="lineno"> 80</span> FILE *fp = fopen(fname, <span class="stringliteral">"rt"</span>);</div>
|
|
<div class="line"><span class="lineno"> 81</span> <span class="keywordflow">if</span> (!fp)</div>
|
|
<div class="line"><span class="lineno"> 82</span> {</div>
|
|
<div class="line"><span class="lineno"> 83</span> perror(<span class="stringliteral">"Unable to open file"</span>);</div>
|
|
<div class="line"><span class="lineno"> 84</span> <span class="keywordflow">return</span> -1;</div>
|
|
<div class="line"><span class="lineno"> 85</span> }</div>
|
|
<div class="line"><span class="lineno"> 86</span> </div>
|
|
<div class="line"><span class="lineno"> 87</span> <span class="comment">/*</span></div>
|
|
<div class="line"><span class="lineno"> 88</span><span class="comment"> * Loops to get total number of rows and columns in the file</span></div>
|
|
<div class="line"><span class="lineno"> 89</span><span class="comment"> */</span></div>
|
|
<div class="line"><span class="lineno"> 90</span> <span class="keywordflow">do</span></div>
|
|
<div class="line"><span class="lineno"> 91</span> {</div>
|
|
<div class="line"><span class="lineno"> 92</span> <span class="keywordtype">int</span> ret = fscanf(fp, <span class="stringliteral">"\"%[^\",]\","</span>, names[COUNT++]);</div>
|
|
<div class="line"><span class="lineno"> 93</span> <span class="keywordflow">if</span> (ret <= 0)</div>
|
|
<div class="line"><span class="lineno"> 94</span> <span class="keywordflow">continue</span>;</div>
|
|
<div class="line"><span class="lineno"> 95</span> <span class="comment">// printf("%s\t", names[COUNT - 1]);</span></div>
|
|
<div class="line"><span class="lineno"> 96</span> } <span class="keywordflow">while</span> (!feof(fp));</div>
|
|
<div class="line"><span class="lineno"> 97</span> fclose(fp);</div>
|
|
<div class="line"><span class="lineno"> 98</span> </div>
|
|
<div class="line"><span class="lineno"> 99</span> printf(<span class="stringliteral">"\nTotal number of names: %lu\n"</span>, COUNT);</div>
|
|
<div class="line"><span class="lineno"> 100</span> </div>
|
|
<div class="line"><span class="lineno"> 101</span> <span class="keywordflow">if</span> (method == 0)</div>
|
|
<div class="line"><span class="lineno"> 102</span> {</div>
|
|
<div class="line"><span class="lineno"> 103</span> clock_t start_time = clock();</div>
|
|
<div class="line"><span class="lineno"> 104</span> <a class="code hl_function" href="../../dd/d8b/problem__22_2sol1_8c.html#a5bc3659aa0949ea33118c95b1dee5f63">shell_sort</a>(names, COUNT);</div>
|
|
<div class="line"><span class="lineno"> 105</span> clock_t end_time = clock();</div>
|
|
<div class="line"><span class="lineno"> 106</span> printf(<span class="stringliteral">"\nShell sort: %.4g millisecond\n"</span>,</div>
|
|
<div class="line"><span class="lineno"> 107</span> 1e3 * (end_time - start_time) / CLOCKS_PER_SEC);</div>
|
|
<div class="line"><span class="lineno"> 108</span> }</div>
|
|
<div class="line"><span class="lineno"> 109</span> <span class="keywordflow">else</span> <span class="keywordflow">if</span> (method == 1)</div>
|
|
<div class="line"><span class="lineno"> 110</span> {</div>
|
|
<div class="line"><span class="lineno"> 111</span> clock_t start_time = clock();</div>
|
|
<div class="line"><span class="lineno"> 112</span> <a class="code hl_function" href="../../dd/d8b/problem__22_2sol1_8c.html#ae359b8a4656b164c91ef91a084c15c9d">lazy_sort</a>(names, COUNT);</div>
|
|
<div class="line"><span class="lineno"> 113</span> clock_t end_time = clock();</div>
|
|
<div class="line"><span class="lineno"> 114</span> printf(<span class="stringliteral">"\nLazy sort: %.4g millisecond\n"</span>,</div>
|
|
<div class="line"><span class="lineno"> 115</span> 1e3 * (end_time - start_time) / CLOCKS_PER_SEC);</div>
|
|
<div class="line"><span class="lineno"> 116</span> }</div>
|
|
<div class="line"><span class="lineno"> 117</span> </div>
|
|
<div class="line"><span class="lineno"> 118</span> <span class="keywordtype">long</span> sum_score = 0;</div>
|
|
<div class="line"><span class="lineno"> 119</span> clock_t start_time = clock();</div>
|
|
<div class="line"><span class="lineno"> 120</span> <span class="keywordtype">int</span> i;</div>
|
|
<div class="line"><span class="lineno"> 121</span> </div>
|
|
<div class="line"><span class="lineno"> 122</span><span class="preprocessor">#ifdef _OPENMP</span></div>
|
|
<div class="line"><span class="lineno"> 123</span><span class="preprocessor">#pragma omp parallel for schedule(runtime) reduction(+ : sum_score)</span></div>
|
|
<div class="line"><span class="lineno"> 124</span><span class="preprocessor">#endif</span></div>
|
|
<div class="line"><span class="lineno"> 125</span><span class="preprocessor">#ifdef DEBUG</span></div>
|
|
<div class="line"><span class="lineno"> 126</span> <span class="keywordflow">for</span> (i = 935; i < 940; i++)</div>
|
|
<div class="line"><span class="lineno"> 127</span>#<span class="keywordflow">else</span></div>
|
|
<div class="line"><span class="lineno"> 128</span> <span class="keywordflow">for</span> (i = 0; i < COUNT; i++)</div>
|
|
<div class="line"><span class="lineno"> 129</span>#endif</div>
|
|
<div class="line"><span class="lineno"> 130</span> {</div>
|
|
<div class="line"><span class="lineno"> 131</span> <span class="keywordtype">long</span> score = 0;</div>
|
|
<div class="line"><span class="lineno"> 132</span> <span class="comment">/* score the alphabets in i^th name */</span></div>
|
|
<div class="line"><span class="lineno"> 133</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"><span class="lineno"> 134</span> score += names[i][j] - <span class="charliteral">'A'</span> +</div>
|
|
<div class="line"><span class="lineno"> 135</span> 1; <span class="comment">/* convert ASCII character to integer score */</span></div>
|
|
<div class="line"><span class="lineno"> 136</span> sum_score += score * (i + 1);</div>
|
|
<div class="line"><span class="lineno"> 137</span><span class="preprocessor">#ifdef DEBUG</span></div>
|
|
<div class="line"><span class="lineno"> 138</span> printf(<span class="stringliteral">"Name: %s\tScore: %u x %u = %lu\n"</span>, names[i], score, i + 1,</div>
|
|
<div class="line"><span class="lineno"> 139</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span>)score * (i + 1));</div>
|
|
<div class="line"><span class="lineno"> 140</span><span class="preprocessor">#endif</span></div>
|
|
<div class="line"><span class="lineno"> 141</span> }</div>
|
|
<div class="line"><span class="lineno"> 142</span> clock_t end_time = clock();</div>
|
|
<div class="line"><span class="lineno"> 143</span> printf(<span class="stringliteral">"Scoring time: %.4g millisecond\n"</span>,</div>
|
|
<div class="line"><span class="lineno"> 144</span> 1e3 * (end_time - start_time) / CLOCKS_PER_SEC);</div>
|
|
<div class="line"><span class="lineno"> 145</span> </div>
|
|
<div class="line"><span class="lineno"> 146</span> printf(<span class="stringliteral">"Total Score = %lu\n"</span>, sum_score);</div>
|
|
<div class="line"><span class="lineno"> 147</span> </div>
|
|
<div class="line"><span class="lineno"> 148</span> <span class="keywordflow">return</span> 0;</div>
|
|
<div class="line"><span class="lineno"> 149</span>}</div>
|
|
<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="ttdoc">Alphabetical sorting using 'shell sort' algorithm.</div><div class="ttdef"><b>Definition:</b> sol1.c:20</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="ttdoc">Maximum number of names to store.</div><div class="ttdef"><b>Definition:</b> sol1.c:14</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="ttdoc">Alphabetical sorting using 'lazy sort' algorithm.</div><div class="ttdef"><b>Definition:</b> sol1.c:48</div></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="86"><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" name="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"><span class="lineno"> 21</span>{</div>
|
|
<div class="line"><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"><span class="lineno"> 23</span> <span class="keyword">const</span> <span class="keywordtype">int</span> gap_len = 8;</div>
|
|
<div class="line"><span class="lineno"> 24</span> <span class="keywordtype">int</span> i, j, g;</div>
|
|
<div class="line"><span class="lineno"> 25</span> </div>
|
|
<div class="line"><span class="lineno"> 26</span> <span class="keywordflow">for</span> (g = 0; g < gap_len; g++)</div>
|
|
<div class="line"><span class="lineno"> 27</span> {</div>
|
|
<div class="line"><span class="lineno"> 28</span> <span class="keywordtype">int</span> gap = gaps[g];</div>
|
|
<div class="line"><span class="lineno"> 29</span> <span class="keywordflow">for</span> (i = gap; i < <a class="code hl_define" href="../../df/d27/alaw_8c.html#a05b49c662c073f89e86804f7856622a0">LEN</a>; i++)</div>
|
|
<div class="line"><span class="lineno"> 30</span> {</div>
|
|
<div class="line"><span class="lineno"> 31</span> <span class="keywordtype">char</span> tmp_buffer[<a class="code hl_define" href="../../dd/d8b/problem__22_2sol1_8c.html#afd709f201d7643c3909621f620ea648a">MAX_NAME_LEN</a>];</div>
|
|
<div class="line"><span class="lineno"> 32</span> strcpy(tmp_buffer, <a class="code hl_struct" href="../../df/dea/structdata.html">data</a>[i]);</div>
|
|
<div class="line"><span class="lineno"> 33</span> </div>
|
|
<div class="line"><span class="lineno"> 34</span> <span class="keywordflow">for</span> (j = i; j >= gap && strcmp(<a class="code hl_struct" href="../../df/dea/structdata.html">data</a>[j - gap], tmp_buffer) > 0;</div>
|
|
<div class="line"><span class="lineno"> 35</span> j -= gap)</div>
|
|
<div class="line"><span class="lineno"> 36</span> strcpy(<a class="code hl_struct" href="../../df/dea/structdata.html">data</a>[j], <a class="code hl_struct" href="../../df/dea/structdata.html">data</a>[j - gap]);</div>
|
|
<div class="line"><span class="lineno"> 37</span> strcpy(<a class="code hl_struct" href="../../df/dea/structdata.html">data</a>[j], tmp_buffer);</div>
|
|
<div class="line"><span class="lineno"> 38</span> }</div>
|
|
<div class="line"><span class="lineno"> 39</span> }</div>
|
|
<div class="line"><span class="lineno"> 40</span><span class="preprocessor">#ifdef DEBUG</span></div>
|
|
<div class="line"><span class="lineno"> 41</span> <span class="keywordflow">for</span> (i = 0; i < <a class="code hl_define" href="../../df/d27/alaw_8c.html#a05b49c662c073f89e86804f7856622a0">LEN</a>; i++) printf(<span class="stringliteral">"%s\t"</span>, <a class="code hl_struct" href="../../df/dea/structdata.html">data</a>[i]);</div>
|
|
<div class="line"><span class="lineno"> 42</span><span class="preprocessor">#endif</span></div>
|
|
<div class="line"><span class="lineno"> 43</span>}</div>
|
|
</div><!-- fragment -->
|
|
</div>
|
|
</div>
|
|
</div><!-- contents -->
|
|
</div><!-- doc-content -->
|
|
<!-- 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 by <a href="https://www.doxygen.org/index.html"><img class="footer" src="../../doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.5 </li>
|
|
</ul>
|
|
</div>
|
|
</body>
|
|
</html>
|