Last active
August 29, 2015 14:13
-
-
Save josmithua/ae01201911bb7730241a to your computer and use it in GitHub Desktop.
Different algorithms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int arrayMax(int * a, int n) { | |
if (n == 1) { | |
return a[0]; | |
} | |
return max(arrayMax(a, n-1), a[n-1]); | |
} | |
int arrayMax(int * a, int i, int j) { | |
if (i >= j) { | |
return a[i]; //or a[j] | |
} | |
return max(arrayMax(a, i, (i+j)/2), arrayMax(a, (i+j)/2+1, j)); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void arrayReverse(int * a, int n) { | |
int end = n-1 | |
for (int i = 0; i <= n/2; i++) { | |
int temp = a[i]; | |
a[i] = a[n-i-1] | |
a[n-i-1] = temp | |
} | |
} | |
void arrayReverse(int * a, int i, int j) { | |
if (i < j) { | |
swap(a[i], a[j]); | |
arrayReverse(a, i+1, j-1); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int arraySum(int * a, int n, int i) { | |
if (i == n-1) { | |
return a[n-1]; | |
} | |
return (a[i] + arraySum(a, n, i+1)); | |
} | |
int arraySum(int * a, int i) { | |
if (i == 0) { | |
return a[0]; | |
} | |
return (a[i] + arraySum(a, i-1)); | |
} | |
int arraySum(int * a, int i, int j) { | |
if (i ==j) { | |
return a[i]; | |
} | |
return (arraySum(a, i, (i+j)/2) + arraySum(a, (i+j)/2+1, j); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment