#include
using namespace std;
void insertsort(int* arr, int len);
int main(void)
{
int arr[] = { 45, 23, 1, 89, 34, 25, 18, 76, 136, 29, 36, 75, 98, 148, 254, 71, 89, 93 };
int len = sizeof(arr) / sizeof(arr[0]);
cout << "before sort: " << endl;
for (int i = 0; i < len; i++)
cout << arr[i] << " ";
cout << endl;
cout << "after sort: " << endl;
insertsort(arr, len);
for (int i = 0; i < len; i++)
cout << arr[i] << " ";
cout << endl;
system("pause");
return 0;
}
void insertsort(int* arr, int len)
{
int temp = 0;
for (int i = 1; i < len; i++)
{
temp = arr[i];
for (int j = i - 1; j >= 0; j--)
{
if (temp < arr[j])
{
arr[j + 1] = arr[j];
arr[j] = temp;
}
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45