Miễn bàn
Vì bản tiếng tây
Binary Search Flowchart
Binary Search Flowchart
Binary Search implemented in C.
view source
print?
01 int binary_search(int sorted_list[], int low, int high, int element) {
02 while (low <= high) {
03 int middle = low + (high - low)/2;
04 if (element > sorted_list[middle])
05 low = middle + 1;
06 else if (element < sorted_list[middle])
07 high = middle - 1;
08 else
09 return middle;
10 }
11 return -1;
12 }
Binary Search in using recursion technique.
view source
print?
01 int binary_search(int sorted_list[], int low, int high, int element) {
02 if (high < low)
03 return -1;
04 int middle = low + (high - low)/2;
05 if (element < sorted_list[middle])
06 return binary_search(sorted_list, low, middle-1, element);
07 else if (element > sorted_list[middle])
08 return binary_search(sorted_list, middle+1, high, element);
09 else
10 return middle;
11 }
Vì bản tiếng tây
Binary Search Flowchart
Binary Search Flowchart
Binary Search implemented in C.
view source
print?
01 int binary_search(int sorted_list[], int low, int high, int element) {
02 while (low <= high) {
03 int middle = low + (high - low)/2;
04 if (element > sorted_list[middle])
05 low = middle + 1;
06 else if (element < sorted_list[middle])
07 high = middle - 1;
08 else
09 return middle;
10 }
11 return -1;
12 }
Binary Search in using recursion technique.
view source
print?
01 int binary_search(int sorted_list[], int low, int high, int element) {
02 if (high < low)
03 return -1;
04 int middle = low + (high - low)/2;
05 if (element < sorted_list[middle])
06 return binary_search(sorted_list, low, middle-1, element);
07 else if (element > sorted_list[middle])
08 return binary_search(sorted_list, middle+1, high, element);
09 else
10 return middle;
11 }