insertion sort in c++ program
C++
/ C++ program for insertion sort
#include <bits/stdc++.h>
using namespace std;
/* Function to sort an array using insertion sort*/
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i - 1;
/* Move elements of arr[0..i-1], that are
greater than key, to one position ahead
of their current position */
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
// A utility function to print an array of size n
void printArray(int arr[], int n)
{
int i;
for (i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
}
/* Driver code */
int main()
{
int arr[] = { 12, 11, 13, 5, 6 };
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
printArray(arr, n);
return 0;
}
// This is code is contributed by rathbhupendra
Also in C++:
- Title
- char to string c++
- Category
- C++
- Title
- c++ program for matrix addition
- Category
- C++
- Title
- remove item from layout
- Category
- C++
- Title
- error: ‘memset’ was not declared in this scope in cpp
- Category
- C++
- Title
- time conversion hackerrank solution in c++
- Category
- C++
- Title
- c++ overload operator
- Category
- C++
- Title
- visual studio 2019 read and write text file c++
- Category
- C++
- Title
- iterate through unordered_map c++ in reverse order
- Category
- C++
- Title
- check if key exists in map c++
- Category
- C++
- Title
- system("pause") note working c++
- Category
- C++
- Title
- c++ ambigous error
- Category
- C++
- Title
- binary search stl in c++
- Category
- C++
- Title
- sort a string alphabetically c++
- Category
- C++
- Title
- COunt the number of continous subsequences such that the sum is between
- Category
- C++
- Title
- convert int to binary string c++
- Category
- C++
- Title
- how to compile opencv c++ in ubuntu
- Category
- C++
- Title
- least number of coins to form a sum
- Category
- C++
- Title
- euler's totient function c++
- Category
- C++
- Title
- double to int c++
- Category
- C++
- Title
- stl sort in c++
- Category
- C++
- Title
- lisy stl C++
- Category
- C++
- Title
- C++ and endl
- Category
- C++
- Title
- restting a queue stl
- Category
- C++
- Title
- c++ string
- Category
- C++
- Title
- graph using djacency matrix c++
- Category
- C++
- Title
- error: invalid use of template-name without an argument list
- Category
- C++
- Title
- new keyword in cpp
- Category
- C++
- Title
- function declerations in C++
- Category
- C++
- Title
- c++ method name
- Category
- C++
- Title
- c++ formatting
- Category
- C++
- Title
- time function c++
- Category
- C++
- Title
- how to use a new node c++
- Category
- C++
- Title
- else if c++
- Category
- C++
- Title
- longest common subsequence
- Category
- C++
- Title
- how to convert string into number
- Category
- C++
- Title
- c++ throw exception
- Category
- C++
- Title
- how to modulo 10^9+7
- Category
- C++
- Title
- c++ function to find length of array
- Category
- C++
- Title
- c++ pi
- Category
- C++
- Title
- how to switch to another branch in git
- Category
- C++
- Title
- write to file in C++
- Category
- C++
- Title
- c++ loop through array
- Category
- C++
- Title
- c++ char if
- Category
- C++
- Title
- count function c++
- Category
- C++
- Title
- in c, is class uppercase or lowercase
- Category
- C++
- Title
- int max in c++
- Category
- C++
- Title
- c++ sort
- Category
- C++
- Title
- preorder traversal c++
- Category
- C++
- Title
- array<string, 7> c++
- Category
- C++
- Title
- how to find the size of a character array in c++
- Category
- C++
- Title
- set of vectors c++
- Category
- C++
- Title
- calling by reference and pointers c++
- Category
- C++
- Title
- c++ yes no question
- Category
- C++
- Title
- c++ read matttrix from text file
- Category
- C++
- Title
- c++ template function
- Category
- C++
- Title
- how to take input in C++ in coding
- Category
- C++
- Title
- initialize vector of pointers c++
- Category
- C++
- Title
- c++ function return pointer to itself
- Category
- C++
- Title
- pass by reference c++
- Category
- C++
- Title
- double max value c++
- Category
- C++
- Title
- & in xml
- Category
- C++
- Title
- sort function in c++
- Category
- C++
- Title
- first prime numbers
- Category
- C++
- Title
- sorting of array in c++
- Category
- C++
- Title
- c++ argv
- Category
- C++
- Title
- c++ scanf
- Category
- C++
- Title
- COnvert string to char * C++
- Category
- C++
- Title
- c++ how to add something at the start of a vector
- Category
- C++
- Title
- how to find hcf in c++
- Category
- C++
- Title
- c++ operator overloading not equal
- Category
- C++
- Title
- Create a program that finds the minimum value in these numbers
- Category
- C++
- Title
- c++ vector iterator
- Category
- C++
- Title
- gfg right view of tree
- Category
- C++
- Title
- dynamic 2d array c++
- Category
- C++
- Title
- retu7rn this c++
- Category
- C++
- Title
- lower bound c++ for array in decreasing order
- Category
- C++
- Title
- bool function in c++
- Category
- C++
- Title
- how to print a 2d array in c++
- Category
- C++
- Title
- hashmap in c++
- Category
- C++
- Title
- modulo c++
- Category
- C++
- Title
- char size length c++
- Category
- C++
- Title
- double to float c++
- Category
- C++
- Title
- string substr c++
- Category
- C++
- Title
- c++ cli convert string to string^
- Category
- C++
- Title
- memcmp in cpp
- Category
- C++
- Title
- how to get the prime number in c++ where time complexity is 0(log n)
- Category
- C++
- Title
- c++ modulo make it give only positive numbers
- Category
- C++
- Title
- rgb(100,100,100,0.5) validation c++
- Category
- C++
- Title
- c++ vector add element
- Category
- C++
- Title
- power c++
- Category
- C++
- Title
- loop through words in string c++
- Category
- C++
- Title
- how to load from files C++
- Category
- C++
- Title
- how print fload wiht 3 decimal in c++
- Category
- C++
- Title
- how to sort an array c++
- Category
- C++
- Title
- subarray sum in c++
- Category
- C++
- Title
- c++ char define
- Category
- C++
- Title
- elseif c++
- Category
- C++
- Title
- how to sort a vector in reverse c++
- Category
- C++
- Title
- convert entire string to lowercase c++
- Category
- C++
- Title
- how to dynamically allocate an array c++
- Category
- C++