File tree Expand file tree Collapse file tree 4 files changed +110
-24
lines changed
Course Code (C++)/05-Recursion-Time-Complexity
Course Code (Java)/05-Recursion-Time-Complexity/src Expand file tree Collapse file tree 4 files changed +110
-24
lines changed Original file line number Diff line number Diff line change @@ -6,48 +6,48 @@ using namespace std;
6
6
// binarySearch
7
7
int binarySearch (int arr[], int l, int r, int target){
8
8
9
- if ( l > r )
9
+ if (l > r)
10
10
return -1 ;
11
11
12
- int mid = l + (r-l)/ 2 ;
13
- if ( arr[mid] == target )
12
+ int mid = l + (r - l) / 2 ;
13
+ if (arr[mid] == target)
14
14
return mid;
15
- else if ( arr[mid] > target )
16
- return binarySearch (arr, l, mid- 1 , target);
15
+ else if (arr[mid] > target)
16
+ return binarySearch (arr, l, mid - 1 , target);
17
17
else
18
- return binarySearch (arr, mid+ 1 , r, target);
18
+ return binarySearch (arr, mid + 1 , r, target);
19
19
20
20
}
21
21
22
22
// sum
23
- int sum ( int n ){
23
+ int sum (int n){
24
24
25
- assert ( n >= 0 );
25
+ assert (n >= 0 );
26
26
27
- if ( n == 0 )
27
+ if (n == 0 )
28
28
return 0 ;
29
- return n + sum (n- 1 );
29
+ return n + sum (n - 1 );
30
30
}
31
31
32
32
// pow2
33
- double pow ( double x, int n ){
33
+ double pow (double x, int n){
34
34
35
- assert ( n >= 0 );
35
+ assert (n >= 0 );
36
36
37
- if ( n == 0 )
37
+ if (n == 0 )
38
38
return 1.0 ;
39
39
40
- double t = pow (x, n/ 2 );
41
- if ( n% 2 )
42
- return x*t* t;
40
+ double t = pow (x, n / 2 );
41
+ if (n % 2 )
42
+ return x * t * t;
43
43
44
- return t* t;
44
+ return t * t;
45
45
}
46
46
47
47
int main () {
48
48
49
- cout<< sum (100 )<< endl;
50
- cout<< pow (2 ,10 )<< endl;
49
+ cout << sum (100 ) << endl;
50
+ cout << pow (2 , 10 ) << endl;
51
51
52
52
return 0 ;
53
53
}
Original file line number Diff line number Diff line change @@ -8,27 +8,29 @@ int f(int n){
8
8
9
9
assert ( n >= 0 );
10
10
11
- if ( n == 0 )
11
+ if (n == 0 )
12
12
return 1 ;
13
13
14
- return f (n- 1 ) + f (n- 1 );
14
+ return f (n - 1 ) + f (n - 1 );
15
15
}
16
16
17
+ /*
17
18
// mergeSort
18
19
void mergeSort(int arr[], int l, int r){
19
20
20
- if ( l >= r )
21
+ if(l >= r)
21
22
return;
22
23
23
24
int mid = (l+r)/2;
24
25
mergeSort(arr, l, mid);
25
- mergeSort (arr, mid+ 1 , r);
26
+ mergeSort(arr, mid + 1, r);
26
27
merge(arr, l, mid, r);
27
28
}
29
+ */
28
30
29
31
int main () {
30
32
31
- cout<< f ( 4 )<< endl;
33
+ cout << f ( 10 ) << endl;
32
34
33
35
return 0 ;
34
36
}
Original file line number Diff line number Diff line change
1
+ public class Main {
2
+
3
+ // binarySearch
4
+ private static int binarySearch (Comparable [] arr , int l , int r , int target ){
5
+
6
+ if (l > r )
7
+ return -1 ;
8
+
9
+ int mid = l + (r - l ) / 2 ;
10
+ if (arr [mid ].compareTo (target ) == 0 )
11
+ return mid ;
12
+ else if (arr [mid ].compareTo (target ) > 0 )
13
+ return binarySearch (arr , l , mid - 1 , target );
14
+ else
15
+ return binarySearch (arr , mid + 1 , r , target );
16
+
17
+ }
18
+
19
+ // sum
20
+ private static int sum (int n ){
21
+
22
+ assert n >= 0 ;
23
+
24
+ if (n == 0 )
25
+ return 0 ;
26
+ return n + sum (n - 1 );
27
+ }
28
+
29
+ // pow2
30
+ private static double pow (double x , int n ){
31
+
32
+ assert n >= 0 ;
33
+
34
+ if (n == 0 )
35
+ return 1.0 ;
36
+
37
+ double t = pow (x , n / 2 );
38
+ if (n % 2 == 1 )
39
+ return x * t * t ;
40
+
41
+ return t * t ;
42
+ }
43
+
44
+ public static void main (String [] args ) {
45
+
46
+ System .out .println (sum (100 ));
47
+ System .out .println (pow (2 , 10 ));
48
+ }
49
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Created by liuyubobobo.
3
+ */
4
+ public class Main2 {
5
+
6
+ // f
7
+ private static int f (int n ){
8
+
9
+ assert ( n >= 0 );
10
+
11
+ if (n == 0 )
12
+ return 1 ;
13
+
14
+ return f (n - 1 ) + f (n - 1 );
15
+ }
16
+
17
+ /*
18
+ // mergeSort
19
+ private static void mergeSort(Comparable[] arr, int l, int r){
20
+
21
+ if(l >= r)
22
+ return;
23
+
24
+ int mid = (l+r)/2;
25
+ mergeSort(arr, l, mid);
26
+ mergeSort(arr, mid + 1, r);
27
+ merge(arr, l, mid, r);
28
+ }
29
+ */
30
+
31
+ public static void main (String [] args ) {
32
+
33
+ System .out .println (f (10 ));
34
+ }
35
+ }
You can’t perform that action at this time.
0 commit comments