冒泡排序
/* 冒泡 */ void bubbleSort(int a[], int n) { int i, j, tmp; for(i =0 ; i< n-1; ++i) { for(j = 0; j < n-i-1; ++j) { if(a[j] > a[j+1]) { tmp = a[j]; a[j] = a[j+1]; a[j+1] = tmp; } } } } /* 改良 */ void Bubble_1( int a[], int n) { int i, j, tmp; int pos = 0; i = n - 1; while(i > 0) { pos = 0; for(j = 0; j < i; j++) { if(a[j] > a[j+1]) { pos = j; //记录交换的位置 tmp = a[j]; a[j] = a[j+1]; a[j+1] = tmp; } } i= pos; } }
一阶滤波
/* 一阶滤波 Y(n)=αX(n) + (1-α)Y(n-1) */ long lvbo_test(long NEW_DATA,unsigned char k) { long result; //声明一个变量作为滤波缓存 if( NEW_DATA < OLD_DATA ) { result = OLD_DATA - NEW_DATA; result *= k; result += 128; //+128是为了四舍五入 result /= 256; result = OLD_DATA - result; } else if( NEW_DATA > OLD_DATA ) { result = NEW_DATA - OLD_DATA; result *= k; result += 128; //+128是为了四舍五入 result /= 256; result = OLD_DATA + result; } else { result = OLD_DATA; } OLD_DATA = result; return result; }