insertion sort
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
// Por ter uma complexidade alta,
// não é recomendado para um conjunto de dados muito grande.
// Complexidade: O(n²) / O(n**2) / O(n^2)
// @see https://www.youtube.com/watch?v=TZRWRjq2CAg
// @see https://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html
function insertionSort(vetor) {
let current;
for (let i = 1; i < vetor.length; i += 1) {
let j = i - 1;
current = vetor[i];
while (j >= 0 && current < vetor[j]) {
vetor[j + 1] = vetor[j];
j--;
}
vetor[j + 1] = current;
}
return vetor;
}
insertionSort([1, 2, 5, 8, 3, 4])
Also in C:
- Title
- iterar en map
- Category
- C
- Title
- functions return type in c
- Category
- C
- Title
- duplicar cadena
- Category
- C
- Title
- how to ascii art in c
- Category
- C
- Title
- C largest unsigned int
- Category
- C
- Title
- how to feed a char array to function in C
- Category
- C
- Title
- how to open a website in c
- Category
- C
- Title
- matplotlib plot circle marker
- Category
- C
- Title
- download file by command line windows
- Category
- C
- Title
- enum c
- Category
- C
- Title
- how to rebasde
- Category
- C
- Title
- how to check the size of a file in linux c
- Category
- C
- Title
- append to list in c
- Category
- C
- Title
- command line arguments c
- Category
- C
- Title
- arduino millis()
- Category
- C
- Title
- mangoosejs
- Category
- C
- Title
- router solicitation and advertisement magic is used by
- Category
- C
- Title
- malloc in c
- Category
- C
- Title
- multiplication operator in c
- Category
- C
- Title
- remove an element from a set
- Category
- C
- Title
- suma de n numeros recursiva
- Category
- C
- Title
- how to put quotes inside string c
- Category
- C
- Title
- c allocate array
- Category
- C
- Title
- strtok
- Category
- C
- Title
- docker images command
- Category
- C
- Title
- reset c array to zero
- Category
- C
- Title
- c print hello world
- Category
- C
- Title
- c struct
- Category
- C
- Title
- bella ciao lyrics
- Category
- C
- Title
- long commands makes terminal lag after modifying PS1
- Category
- C
- Title
- delete docker image repository none
- Category
- C
- Title
- select all file from date powershell
- Category
- C
- Title
- atoi
- Category
- C
- Title
- ModuleNotFoundError: No module named 'tensorboardX'
- Category
- C
- Title
- version of libgcc
- Category
- C
- Title
- Print the number 0 using write()
- Category
- C
- Title
- simpy process return value
- Category
- C
- Title
- 'int' is not a subtype of type 'double' dart
- Category
- C
- Title
- Difference between ** and *
- Category
- C
- Title
- boolean function c
- Category
- C
- Title
- arduino knn
- Category
- C
- Title
- c program to count duplicates in an array
- Category
- C
- Title
- primo
- Category
- C
- Title
- xamarin command listview button
- Category
- C
- Title
- tar cmd
- Category
- C
- Title
- c vs python
- Category
- C
- Title
- variadic function in c
- Category
- C
- Title
- potencia recursiva
- Category
- C
- Title
- remove element queue
- Category
- C
- Title
- ModuleNotFoundError: No module named 'cv2'
- Category
- C
- Title
- first person view unity
- Category
- C
- Title
- generate all permutations of string
- Category
- C
- Title
- array length c
- Category
- C
- Title
- CL/cl.h: No such file or directory
- Category
- C
- Title
- c pause for 1 second
- Category
- C
- Title
- piramide
- Category
- C
- Title
- what is the meaningof noremap
- Category
- C
- Title
- switch case c
- Category
- C
- Title
- scanf read line
- Category
- C
- Title
- how to make a linked list in c
- Category
- C
- Title
- -> operator
- Category
- C
- Title
- buscar caracter
- Category
- C
- Title
- dani
- Category
- C
- Title
- vector aleatorio sin repetir
- Category
- C
- Title
- time now c
- Category
- C
- Title
- c radians
- Category
- C
- Title
- strtoul C
- Category
- C
- Title
- convert string to float c
- Category
- C
- Title
- a enum data type in c
- Category
- C
- Title
- passing 'const char *' to parameter of type 'char *' discards qualifiers
- Category
- C
- Title
- c float to int
- Category
- C
- Title
- C why is is & nit used in scan f fr string
- Category
- C
- Title
- fibonacci in c
- Category
- C
- Title
- where is /dev/kvm
- Category
- C
- Title
- what are the causes of memory leaks in c
- Category
- C
- Title
- accessing elements 2D array using pointers
- Category
- C
- Title
- add border to image android
- Category
- C
- Title
- update ubuntu in terminal
- Category
- C
- Title
- pass the pointer in C
- Category
- C
- Title
- write a program to ask the user 8 integer numbers. your program will then move all negative elements of your array to the end of the array without changing the order of positive elements and negative elements
- Category
- C
- Title
- how to get user input in c
- Category
- C
- Title
- div
- Category
- C
- Title
- write in a file using c
- Category
- C
- Title
- install postgres on linux
- Category
- C
- Title
- measure time in c
- Category
- C
- Title
- write a binary file c
- Category
- C
- Title
- mitch mcconnell
- Category
- C
- Title
- c check if array is empty
- Category
- C
- Title
- jock cranley
- Category
- C
- Title
- how to remove \n from a string c
- Category
- C
- Title
- '&&' within '||'
- Category
- C
- Title
- change a attribute in dataframe
- Category
- C
- Title
- heitai bestial
- Category
- C
- Title
- find gcd iteratively
- Category
- C
- Title
- v
- Category
- C
- Title
- random en c
- Category
- C
- Title
- slug urls django
- Category
- C
- Title
- diferencia entre * y & en c
- Category
- C
- Title
- c defined value sum
- Category
- C
- Title
- isalpha c
- Category
- C