数据结构和算法
冒泡排序:
1 void bubble_sort(int a[],int len){ 2 for (int i = 0; i<len-1; i++) { 3 for (int j=len-1; j>i; j--) { 4 if (a[j]<a[j-1]) { 5 int temp = a[j]; 6 a[j] = a[j-1]; 7 a[j-1] = temp; 8 } 9 } 10 } 11 }
二分查找:
1 int binary_search(int a[],int len,int goal){ 2 int low = 0; 3 int high = len - 1; 4 while (low <= high) { 5 int mid = (low + high)/2; 6 if (goal==a[mid]) { 7 return mid; 8 }else if(goal<a[mid]){ 9 high = mid-1; 10 }else{ 11 low = mid+1; 12 } 13 } 14 return -1; 15 }