微信图片_20210412210125.png

    1. #include<stdio.h>
    2. void printarry(int a[], int length);
    3. void insertSort(int arry[], int length);
    4. void printarry(int a[], int length,int n) {
    5. if (n)
    6. printf("排好序的数:");
    7. else
    8. printf("未排好序的数:");
    9. for (int i = 1; i < length; i++)
    10. {
    11. printf("-%d", a[i]);
    12. }
    13. printf("\n");
    14. }
    15. void insertSort(int arry[], int length) {
    16. int i;
    17. i = 2;
    18. for (i; i <= length - 1; i++) {
    19. arry[0] = arry[i];
    20. int j = i - 1;
    21. while (arry[0] < arry[j])
    22. {
    23. arry[j + 1] = arry[j];
    24. j--;
    25. }
    26. arry[j + 1] = arry[0];
    27. }
    28. }
    29. int main(void) {
    30. int arry[] = { 0,15,56,25,35,85,63,21,56,15,25,80,90,50,12,53,12,50 };
    31. int length = sizeof(arry) / sizeof(int);
    32. int flag;
    33. flag = 0;
    34. printarry(arry,length,flag);
    35. insertSort(arry,length);
    36. flag = 1;
    37. printarry(arry,length,flag);
    38. }