Last active
February 7, 2017 20:16
-
-
Save discort/05d09604f84c66a3ee450db9864469c5 to your computer and use it in GitHub Desktop.
This file contains 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
def merge(A, p, q, r): | |
L = A[p:q + 1] | |
R = A[q + 1: r + 1] | |
i = j = 0 | |
for k in range(p, r + 1): | |
if i == len(L): | |
A[k] = R[j] | |
j += 1 | |
elif j == len(R): | |
A[k] = L[i] | |
i += 1 | |
elif L[i] <= R[j]: | |
A[k] = L[i] | |
i += 1 | |
else: | |
A[k] = R[j] | |
j += 1 | |
def merge_sort(A, p, r): | |
if p < r: | |
q = (p + r) // 2 | |
merge_sort(A, p, q) | |
merge_sort(A, q + 1, r) | |
merge(A, p, q, r) | |
def find_sum(S, x): | |
merge_sort(S, 0, len(S) - 1) | |
left = 0 | |
right = len(S) - 1 | |
while left <= right: | |
Sum = S[left] + S[right] | |
if Sum > x: | |
right -= 1 | |
elif Sum < x: | |
left += 1 | |
else: | |
print("Items are {0} and {1}".format(S[left], S[right])) | |
right -= 1 | |
left += 1 | |
def main(): | |
S = [1, 3, 9, 6, 7, 5, 4, 10] | |
x = 8 | |
print(S) | |
find_sum(S, x) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment