插入排序

来源:互联网 发布:移动网络不稳定 编辑:程序博客网 时间:2024/05/02 02:27
#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/timeb.h>#include <time.h>#define MAX 9long get_sys_time(){struct timeb tb;ftime(&tb);return tb.time * 1000 + tb.millitm;}void print_array(int *a, int size){int i = 0;for (i = 0; i < size; i++){printf("%d\t", a[i]);}}void insert_sort(int* a, int size){int i = 0, j = 0;for (i = 1; i < size; i++){if (a[i] < a[i -1]){int tmp = a[i];for (j = i - 1; j >= 0 && tmp < a[j]; j--){a[j + 1] = a[j];}a[j + 1] = tmp;}}}int main(void){int a[MAX] = { 324, 43, 2, 643, 76, 3, 5, 25, 362 };print_array(a, MAX);insert_sort(a, MAX);printf("\n");print_array(a, MAX);printf("\n");system("pause");return 0;}

0 0
原创粉丝点击