[算法] 插入排序
#include <iostream>using namespace std;
void insertionSort(int arr[, int n)
{
int i, j;
int key;
for (i = 1; i < n; i++)
{
key = arr[i;
j = i - 1;
while ((j >= 0) && (arr[j > key))
{
arr[j+1 = arr[j;
j--;
}
arr[j+1 = key;
}
}
int main()
{
int arr[15 = {10, 21, 2, 1, 3, 45, 2, 932, 32, 27, 86, 65, 576, 434, 76753};
int i;
cout << "Original array" << endl;
for (i = 0; i < 15; i++)
cout << arr[i << " ";
cout << endl << endl;
insertionSort(arr, 15);
cout << "Sorted array" << endl;
for (i = 0; i < 15; i++)
cout << arr[i << " ";
cout << endl;
return 0;
}
页:
[1]