Skip to content

Instantly share code, notes, and snippets.

@tooshitaka
Created February 28, 2017 02:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tooshitaka/9bc97b8b935211285ee025a9312ee372 to your computer and use it in GitHub Desktop.
Save tooshitaka/9bc97b8b935211285ee025a9312ee372 to your computer and use it in GitHub Desktop.
#include<iostream>
// Print all elemets in array
void print(int A[], int N)
{
for (int i = 0; i < N; i++) {
if (i != 0)
std::cout << " ";
std::cout << A[i];
}
std::cout << std::endl;
}
// Insertion sort
void insertion(int A[], int N)
{
for (int i = 1; i < N; i++) {
// Value to insert
int v = A[i];
int j = i - 1;
while (j >= 0 && A[j] > v) {
A[j + 1] = A[j];
j--;
}
A[j+1] = v;
print(A, N);
}
}
// MAIN FUNCTION
int main()
{
// Input
int n;
int A[1000];
std::cin >> n;
for (int i = 0; i < n; i++) {
std::cin >> A[i];
}
print(A, n);
// Insertion sort
insertion(A, n);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment