C++, LUA, PYTHON, BASH混用

CPP:

 1 #include
 2 #include
 3 #include
 4 #include
 5 using namespace std;
 6 template
 7 int binarySearch(const vector &a, const T &x, int& aiTimes)
 8 {
 9     int low = 0;
10     int high = a.size() - 1;
11
12     while(low <= high)
13     {
14         ++aiTimes;
15         int mid = (low + high) / 2;
16         
17         if(a[mid] < x)
18         {
19             low = mid + 1;
20         }
21         else if(a[mid] > x)
22         {
23             high = mid - 1;
24         }
25         else
26         {
27             return mid;
28         }
29     }
30
31     return -1;
32 }
33
34 int main(int argc, char* argv[])
35 {
36     vector lveci;
37     lveci.resize(100);
38     for(int i = 0; i < 100; ++i)
39     {
40         lveci[i] = i;
41     }
42
43     int liTimes = 0;
44     cout << binarySearch(lveci, -1, liTimes);
45     cout <<"\tcost:" < 46     liTimes = 0;
47     cout << binarySearch(lveci, 0, liTimes);
48     cout <<"\tcost:" < 49     liTimes = 0;
50     cout << binarySearch(lveci, 1, liTimes);
51     cout <<"\tcost:" < 52     liTimes = 0;
53     cout << binarySearch(lveci, 2, liTimes);
54     cout <<"\tcost:" < 55     liTimes = 0;
56     cout << binarySearch(lveci, 100, liTimes);
57     cout <<"\tcost:"< 58
59
60
61     exit(0);
62 }
63



LUA:

 1 #!/usr/bin/env lua
 2 function binarySearch(a, v)
 3     low = 1
 4     high = #a
 5
 6     times = 0
 7     while(low <= high) do
 8         times = times + 1
 9         mid = math.floor(( low + high) / 2)
10         if a[mid] < v  then
11             low = mid + 1
12         elseif a[mid] > v then
13             high = mid - 1
14         else
15             return mid,times
16         end
17     end
18
19     return -1,times
20 end
21
22 -- test code
23 array = {}
24 for i=1,100 do
25     array[i] = i
26 end
27
28 print(binarySearch(array, 0))
29 print(binarySearch(array, 1))
30 print(binarySearch(array, 2))
31 print(binarySearch(array, 3))
32 print(binarySearch(array, 101))
33



PYTHON:

 1 #!/usr/bin/env python
 2
 3 def binarySearch(a, v):
 4     low = 0
 5     high = len(a) - 1
 6     times = 0
 7
 8     while low <= high:
 9         times += 1
10         mid = (low + high) // 2
11
12         if v > a[mid]:
13             low = mid + 1
14         elif v < a[mid]:
15             high = mid - 1
16         else:
17             return mid,times
18     
19     return -1,times
20
21 array = range(100)
22 print binarySearch(array, -1)
23 print binarySearch(array, 0)
24 print binarySearch(array, 1)
25 print binarySearch(array, 2)
26 print binarySearch(array, 100)
27     
28



BASH:

 1 #!/usr/bin/env bash
 2
 3
 4 binarySearch()
 5 {
 6     v=$2
 7     an="$1[@]"
 8     a=${!an}
 9     for i in $a
10     do
11         ar[i]=$i
12     done
13     low=1
14     high=${#ar[*]}
15     (( times=0 ))
16
17     while (( low <= high ))
18     do
19         (( ++times ))
20         (( mid=(low+high)/2 ))
21         #echo -n " mid="$mid
22         #echo -n " ar[mid]="${ar[mid]}
23         if (( v > ar[mid] ))
24         then
25             (( low = mid + 1 ))
26         elif (( v < ar[mid] ))
27         then
28             (( high = mid - 1 ))
29         else
30             #echo -e "\nTimes="$times
31             return $mid
32         fi
33     done
34
35     #echo -e "\nTimes="$times
36     return -1
37 }
38
39 for ((i=1; i<= 100; ++i))
40 do
41     (( array[i] = i ))
42 done
43
44 binarySearch array 0
45 echo -e "$?\t$times"
46 binarySearch array 1
47 echo -e "$?\t$times"
48 binarySearch array 2
49 echo -e "$?\t$times"
50 binarySearch array 3
51 echo -e "$?\t$times"
52 binarySearch array 101
53 echo -e "$?\t$times"
54
55 exit 0

你可能感兴趣的:(python,lua,c++,iostream,bash,vector)