#include <iostream> using namespace std; //直接插入排序:原數組、排序後得有序數組、數組元素個數 void insertSort(int *a, int *b, int dataNum) { b[0] = a[0]; for (int i=1;i<dataNum;i++) { for (int j=i-1;j>=0;j--) { if (a[i] < b[j]) { b[j + 1] = b[j]; b[j] = a[i]; continue; } b[j + 1] = a[i]; break; } } } int main(void) { int a[10] = { 2,4,5,2,1,6,10,7,93,40 }; int *b = new int[10](); insertSort(a, b, 10); for (int i=0;i<10;i++) { cout << b[i] << " "; } delete[]b; system("pause"); return 0; }