Implement Insertion sort algorithm in C++

Code

// insertion sort

#include <iostream>

using namespace std;

int main()
{
int n, i, j, temp;
int arr[50];
cout << "Enter the size of array: ";
cin >> n;
cout << "Enter the elements of array: " << endl;
for (i = 0; i < n; i++)
{
cin >> arr[i];
}
for (i = 1; i < n; i++)
{
temp = arr[i];
j = i - 1;
while (temp < arr[j] && j >= 0)
{
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = temp;
}
cout << "Sorted array: ";
for (i = 0; i < n; i++)
{
cout << arr[i] << " ";
}

cout << endl;
return 0;
}


Output

Enter the size of array: 7
Enter the elements of array: 
34 
46 
13
65
33
23
46
Sorted array: 13 23 33 34 46 46 65