#include<iostream> using namespace std; void swap(int *a, int *b) { int *c = a; a = b; b = c; } void SelectSort(int *A, int len) { for (int i = 0; i < len; i++) { int min = i; for (int j = i + 1; j < len; j++) { if (A[j] < A[min]) min = j; } if (min != i) swap(A[i],A[min]); } } void InsertSort(int *A, int len) { for (int i = 1; i < len; i++) { if (A[i] < A[i - 1]) { int tmp = A[i]; int j = 0; for ( j = i - 1; j >= 0 && A[j]>tmp; j--) A[j + 1] = A[j]; A[j + 1] = tmp; } } } int main() { int A[] = {1,5,3,4,0,7}; int N = sizeof A / sizeof A[0]; InsertSort(A, N); for (int i = 0; i < N; i++) { cout << A[i] << " "; } cout << "\n"; return 0; }
时间: 2024-10-03 02:20:35