当前位置: 代码迷 >> 综合 >> 排序算法(4)--插入排序
  详细解决方案

排序算法(4)--插入排序

热度:46   发布时间:2023-11-18 03:30:04.0
插入排序实现:
#include <stdio.h>
#include <stdlib.h>void insertionSort(int *array, int n);
int main()
{int i, n, *array;printf("Input n:\n");scanf("%d", &n);array = malloc(sizeof(int) * n);for(i = 0; i < n; i++)scanf("%d", &array[i]);//sortinsertionSort(array, n);//printprintf("After sorted:\n");for(i = 0; i < n; i++)printf("%d ", array[i]);printf("\n");return 0;
}void insertionSort(int *array, int n)
{int i, j, temp;for(i = 1; i < n; i++){if(array[i - 1] > array[i]){j = i;temp = array[i];while(j > 0 && array[j - 1] > temp){array[j] = array[j - 1];j--;}array[j] = temp;}}}
  相关解决方案