ITeye博客

c

共 2 篇文章

插入排序【c语言】

#include <stdio.h> void insertSort( int arr[] , int size ) { int i , j , k , f /*First element*/; // 0 1 2 3 4 // 4 3,2,1 // 3 4 2,1 // 2 3 4 1 // 1. 遍历无序区域 for ( i = 1; i < size; i++ ) { f = arr[i] ; // 无序区的第一个元素,与有序区比较 // 2. 遍历有序区域, 找到第一个比无序区大的元素 for ( j = 0; j < i; j++ ) { if ( f < arr[j] ) { //…

2012-05-22 13:49 · c

二分查找【c语言】

#include <stdio.h> int halfSearch( int arr[], int num, int size ) { int min = 0, max = ( size - 1), mid; if ( size == 0 ) { return -1; } while ( 1 ) { // 1. when the boundary is "min" or "max" if ( num == arr[ min ] ) { return min ; } if ( num == arr[ max ] ) { return max ; } // 2. check the not fo…

2012-05-21 09:19 · c