插入排序

 

#include <stdio.h>

void insertSort(int a[], int n)
{
	int temp;
	int i;

	for(i = 1; i < n; i++)
	{
		temp = a[i];
		int j = i - 1;

		while(temp < a[j])
		{
			a[j + 1] = a[j];
			j--;
		}
		a[j + 1] = temp;
	}
}

int main(void)
{
	int a[5] = {1,5,4,3,2};
	int i = 0;

	insertSort(a, 5);

	for(;i < 5; i++)
		printf("%d ", a[i]);

	printf("\n");
}

  

//折半查找排序
#include <stdio.h> void binsort(int a[], int n) { int i; for(i = 1; i < n; i++) { int temp = a[i]; int j = i; int low = 0; int hight = i - 1; while(low <= hight) { int middle = (low + hight)/2; if(a[middle] < temp) low = middle + 1; else hight = middle - 1; } for(; j >= low; j--) a[j] = a[j - 1]; a[low] = temp; } } int main() { int a[4] = {5, 1, 4, 8}; int i; binsort(a, 4); for(i = 0; i < 4; i++) printf("%d ", a[i]); printf("\n"); }

  

posted @ 2014-03-22 14:18  elroyzhang  阅读(139)  评论(0编辑  收藏  举报