C program for recursive insertion sort
Insertion sort is a sorting algorithm that is based on in-place comparison.
This algorithm works by placing an element at a position in a sorted subarray, i.e. the subarray before the element is the sorted subarray.
Algorithm
Step1 - Loop from 1 to n-1 and execute -
Step2 .1 - Select the element at position i, array[i].
Step2.2 - Insert the element into the sorted subarray array[0] at its position arr[i].
Let’s understand the algorithm through an example
Array = [34, 7, 12, 90, 51]
For i = 1, arr[1] = 7, put the position in the subarray arr[0] - arr[1].
[7, 34, 12, 90, 51]
For i = 2, arr[2] = 12, put the position in the subarray arr[0] - arr[2].
[7, 12, 34, 90, 51]
For i = 3, arr[3] = 90, place it in the subarray arr[0] - arr[3].
[7, 12, 34, 90, 51]
For i = 4, arr[4] = 51, place it in the correct position in the subarray arr[0] - arr[4].
[7, 12, 34, 54, 90]
Here we will see how recursive insertion sort works. It works in the opposite way, i.e. we will recursively call the recursiveInsertionSort() function to sort an array of n-1 elements compared to the current iteration. Then in the sorted array returned by the function, we insert the nth element into its position in the sorted array.
The program of recursive insertion sort is as follows:
Example
Demonstration
#include <stdio.h> void recursiveInsertionSort(int arr[], int n){ if (n <= 1) return; recursiveInsertionSort( arr, n-1 ); int nth = arr[n-1]; int j = n-2; while (j >= 0 && arr[j] > nth){ arr[j+1] = arr[j]; j--; } arr[j+1] = nth; } int main(){ int array[] = {34, 7, 12, 90, 51}; int n = sizeof(array)/sizeof(array[0]); printf("Unsorted Array:\t"); for (int i=0; i < n; i++) printf("%d ",array[i]); recursiveInsertionSort(array, n); printf("</p><p>Sorted Array:\t"); for (int i=0; i < n; i++) printf("%d ",array[i]); return 0; }
Output
Unsorted Array: 34 7 12 90 51 Sorted Array: 7 12 34 51 90
The above is the detailed content of C program for recursive insertion sort. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The recursion depth of C++ functions is limited, and exceeding this limit will result in a stack overflow error. The limit value varies between systems and compilers, but is usually between 1,000 and 10,000. Solutions include: 1. Tail recursion optimization; 2. Tail call; 3. Iterative implementation.

Yes, C++ Lambda expressions can support recursion by using std::function: Use std::function to capture a reference to a Lambda expression. With a captured reference, a Lambda expression can call itself recursively.

The recursive algorithm solves structured problems through function self-calling. The advantage is that it is simple and easy to understand, but the disadvantage is that it is less efficient and may cause stack overflow. The non-recursive algorithm avoids recursion by explicitly managing the stack data structure. The advantage is that it is more efficient and avoids the stack. Overflow, the disadvantage is that the code may be more complex. The choice of recursive or non-recursive depends on the problem and the specific constraints of the implementation.

Given two strings str_1 and str_2. The goal is to count the number of occurrences of substring str2 in string str1 using a recursive procedure. A recursive function is a function that calls itself within its definition. If str1 is "Iknowthatyouknowthatiknow" and str2 is "know" the number of occurrences is -3. Let us understand through examples. For example, input str1="TPisTPareTPamTP", str2="TP"; output Countofoccurrencesofasubstringrecursi

Hyperbolic functions are defined using hyperbolas instead of circles and are equivalent to ordinary trigonometric functions. It returns the ratio parameter in the hyperbolic sine function from the supplied angle in radians. But do the opposite, or in other words. If we want to calculate an angle from a hyperbolic sine, we need an inverse hyperbolic trigonometric operation like the hyperbolic inverse sine operation. This course will demonstrate how to use the hyperbolic inverse sine (asinh) function in C++ to calculate angles using the hyperbolic sine value in radians. The hyperbolic arcsine operation follows the following formula -$$\mathrm{sinh^{-1}x\:=\:In(x\:+\:\sqrt{x^2\:+\:1})}, Where\:In\:is\:natural logarithm\:(log_e\:k)

The rename function changes a file or directory from its old name to its new name. This operation is similar to the move operation. So we can also use this rename function to move files. This function exists in the stdio.h library header file. The syntax of the rename function is as follows: intrename(constchar*oldname,constchar*newname); The function of the rename() function accepts two parameters. One is oldname and the other is newname. Both parameters are pointers to constant characters that define the old and new names of the file. Returns zero if the file was renamed successfully; otherwise, returns a nonzero integer. During a rename operation

A recursive function is a technique that calls itself repeatedly to solve a problem in string processing. It requires a termination condition to prevent infinite recursion. Recursion is widely used in operations such as string reversal and palindrome checking.

Tail recursion optimization (TRO) improves the efficiency of certain recursive calls. It converts tail-recursive calls into jump instructions and saves the context state in registers instead of on the stack, thereby eliminating extra calls and return operations to the stack and improving algorithm efficiency. Using TRO, we can optimize tail recursive functions (such as factorial calculations). By replacing the tail recursive call with a goto statement, the compiler will convert the goto jump into TRO and optimize the execution of the recursive algorithm.
