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
| #include <bits/stdc++.h> using namespace std; void swap(int &a, int &b) { int temp = a; a = b; b = temp; }
void insertSort(int a[], int length) { for (int i = 1; i < length; i++) { for (int j = i - 1; j >= 0 && a[j + 1] < a[j]; j--) { swap(a[j], a[j + 1]); } }
}
int main() { int a[] = { 2,1,4,5,3,8,7,9,0,6 };
insertSort(a, 10);
for (int i = 0; i < 10; i++) { cout << a[i] << "";
} cout << endl; system("pause"); return 0; }
|