博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
插入排序算法 ,递归实现_C程序实现递归插入排序
阅读量:2528 次
发布时间:2019-05-11

本文共 1421 字,大约阅读时间需要 4 分钟。

插入排序算法 ,递归实现

The only difference between and Recursive Insertion Sort is that in the Recursive method, we start from placing the last element in its correct position in the sorted array instead of starting from the first.

递归插入排序之间的唯一区别是,在递归方法中,我们从将最后一个元素放置在已排序数组中的正确位置开始,而不是从第一个元素开始。

Here, I will only be showing the C implementation of the sort as I have explained the basic theory in my previous article.

在这里,我将仅按照我在上一篇文章中解释的基本理论来说明该类的C实现。

Recursive Insertion Sort Implementation:

递归插入排序实现:

#include 
void rec_insertion(int arr[], int n){ // When the elements are all over if (n <= 1) return; // sorting n-1 elements rec_insertion(arr, n - 1); int last = arr[n - 1]; int j = n - 2; while (j >= 0 && last < arr[j]) { arr[j + 1] = arr[j]; j--; } arr[j + 1] = last; printf("\nAfter performing Insertion sort:\n"); for (int i = 0; i < n; i++) printf("%d ", arr[i]);}int main(){ int arr[] = { 10, 14, 3, 8, 5, 12 }; int n = sizeof(arr) / sizeof(arr[0]); rec_insertion(arr, n); return 0;}

Output:

输出:

After performing Insertion sort:10 14After performing Insertion sort:3 10 14After performing Insertion sort:3 8 10 14After performing Insertion sort:3 5 8 10 14After performing Insertion sort:3 5 8 10 12 14

This output shows the array after each ith iteration. Feel free to ask your doubts.

此输出显示每次 i 迭代后的数组。 随时提出您的疑问。

翻译自:

插入排序算法 ,递归实现

转载地址:http://eotzd.baihongyu.com/

你可能感兴趣的文章
女陔说"你不懂我", 到底什么意思
查看>>
uva11149
查看>>
S/4HANA中的销售计划管理
查看>>
【图灵学院09】RPC底层通讯原理之Netty线程模型源码分析
查看>>
非常的好的协同过滤入门文章(ZZ)
查看>>
数据结构:哈希表
查看>>
markdown 基本语法
查看>>
tensorflow之tf.slice()
查看>>
Python高阶函数-闭包
查看>>
Windows下安装Redis
查看>>
Ubuntu 12.04 部署 PostGIS 2.1
查看>>
手机web——自适应网页设计(html/css控制)
查看>>
*[codility]CartesianSequence
查看>>
Hadoop1重新格式化HDFS
查看>>
HttpClientUtil工具类
查看>>
random模块
查看>>
Windows FindFirstFile利用
查看>>
使用mptt在easyui中显示树形结构
查看>>
冒泡排序
查看>>
C#微型网页查看工具
查看>>