希尔排序实例程序(C程序)

算法 ww9992019com 发表于 5 天前

<div style="width:100%;float:left;" class="article-content">

                <input type="hidden" name="article_id" id="article_id" value="7835">
                <input type="hidden" name="catid" id="catid" value="0">

                <p>

使用C语言来编写 shell 排序程序实例,如下所示:

</p>

<p>

</p><pre class="prettyprint notranslate tryit">#include &lt;stdio.h&gt;

include &lt;stdbool.h&gt;

define MAX 7

int intArray[MAX] = {4,6,3,2,1,9,7};

void printline(int count){

int i;

for(i = 0;i &lt;count-1;i++){

  printf("=");

}

printf("=\n");

}

void display(){

int i;

printf("[");

// navigate through all items

for(i = 0;i&lt;MAX;i++){

  printf("%d ",intArray[i]);

}

printf("]\n");

}

void shellSort(){

int inner, outer;

int valueToInsert;

int interval = 1;

int elements = MAX;

int i = 0;

while(interval &lt;= elements/3) {

  interval = interval*3 +1;

}

while(interval &gt; 0) {

  printf("iteration %d#:",i);

  display();



  for(outer = interval; outer &lt; elements; outer++) {

     valueToInsert = intArray[outer];

     inner = outer;



     while(inner &gt; interval -1 &amp;&amp; intArray[inner - interval] &gt;= valueToInsert) {

        intArray[inner] = intArray[inner - interval];

        inner -= interval;

        printf(" item moved :%d\n",intArray[inner]);

     }



     intArray[inner] = valueToInsert;

     printf(" item inserted :%d, at position :%d\n",valueToInsert,inner);

  }

  interval = (interval -1) /3;

  i++;

}

}

int main() {

printf("Input Array: ");

display();

printline(50);

shellSort();

printf("Output Array: ");

display();

printline(50);

return 1;

}
</pre>

<p>

    如果我们编译并运行上述程序那么这将产生以下结果 -

</p>

<pre class="result notranslate">Input Array: [4, 6, 3, 2, 1, 9, 7]

==================================================

iteration 0#: [4, 6, 3, 2, 1, 9, 7]

item moved :4

item inserted :1, at position :0

item inserted :9, at position :5

item inserted :7, at position :6

iteration 1#: [1, 6, 3, 2, 4, 9, 7]

item inserted :6, at position :1

item moved :6

item inserted :3, at position :1

item moved :6

item moved 😸

item inserted :2, at position :1

item moved :6

item inserted :4, at position 😸

item inserted :9, at position :5

item moved :9

item inserted :7, at position :5

Output Array: [1, 2, 3, 4, 6, 7, 9]

==================================================</pre>

                <br>
                <p style="text-align:center;float:left;width:100%;margin-top:24px;margin-bottom:18px;">
                        <button class="layui-btn layui-btn-danger" id="btn-reward"> ¥ 我要打赏 </button>  
                        <button class="layui-btn" id="article-perfect">
                            <i class="layui-icon layui-icon-edit"></i>教程纠错
                        </button>
                        <button class="layui-btn layui-btn-normal" id="action-collection">
                            <i class="layui-icon layui-icon-rate"></i>收藏
                        </button>
                </p>



                <div style="width:100%;margin-bottom: 16px; height: 32px;margin-top:18px;float:left;">

                    <span style="float:left;">上一篇:<a href="http://f2e.im/u/ww9992019com" title="希尔排序"><i class="layui-icon"></i>
                            希尔排序</a></span>
                                            <span style="float:right;">下一篇:<a href="http://f2e.im/u/ww9992019com" style="float:right;" title="下一篇:快速排序"><i class="layui-icon"></i>
                            快速排序</a></span>
                                            <div id="googlead" style="float:left;margin-top:8px;">
                        <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
                        <!-- yiibai下方 -->
                        <ins class="adsbygoogle" style="display:inline-block;width:728px;height:90px" data-ad-client="ca-pub-1090193214637198" data-ad-slot="6494738921"></ins>
                        <script>
                            (adsbygoogle = window.adsbygoogle || []).push({});
                        </script>
                    </div>
                </div>

                <div style="width:100%;margin-top:18px;float:left;">


            </div>
暂无回复,说出你的观点吧
登录后即可参与回复