用二分法实现排序
#include<stdio.h>int main(){ int i,array[100],n,j; PRintf("Please enter the number quantities:/n"); scanf("%d",&n); printf("Please input some numbers:/n"); for(i = 0; i < n; i++) { scanf("%d",&array[i]); } for ( i = 0; i < n; i++) { int start = 0; int end = i - 1; int middle = 0; int temp = array[i]; while (start <= end) //左边大于右边结束 { middle = (start + end) / 2; //取中间元素 if (array[middle] > temp)//要排序元素在已经排过序的数组左边 { end = middle - 1; } else { start = middle + 1; } } for ( j = i - 1; j > end; j--)//找到了插入的位置,然后将这个位置以后的所有元素向后移动 { array[j + 1] = array[j]; } array[end + 1] = temp; } for(i = 0; i < n; i++) { printf("%d ",array[i]); }}
新闻热点
疑难解答