From fdb942da83282ad127155fbee6f16f1c82e6f9fa Mon Sep 17 00:00:00 2001 From: Anay Omar <89692029+AnayAsItIS@users.noreply.github.com> Date: Sun, 17 Oct 2021 15:25:30 +0530 Subject: [PATCH] Add files via upload --- Merge Two Sorted List.py | 30 +++++++++++++++++++++++ Rearrange Positive and Negative Values.py | 19 ++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 Merge Two Sorted List.py create mode 100644 Rearrange Positive and Negative Values.py diff --git a/Merge Two Sorted List.py b/Merge Two Sorted List.py new file mode 100644 index 0000000..916d638 --- /dev/null +++ b/Merge Two Sorted List.py @@ -0,0 +1,30 @@ +# Merge two Sorted List +def merge(arr1, arr2, n, m): + i = 0 + j = 0 + temp = [0]*(n+m) + k = 0 + while i < n and j < m: + if arr1[i] < arr2[j]: + temp[k] = arr1[i] + i += 1 + else: + temp[k] = arr2[j] + j += 1 + k += 1 + + while i < n: + temp[k] = arr1[i] + i += 1 + k += 1 + + while j < m: + temp[k] = arr2[j] + j += 1 + k += 1 + return temp + + +arr1 = [1, 3, 5, 7] +arr2 = [0, 2, 6, 8, 9] +print(merge(arr1, arr2, len(arr1), len(arr2))) diff --git a/Rearrange Positive and Negative Values.py b/Rearrange Positive and Negative Values.py new file mode 100644 index 0000000..4a8a2a4 --- /dev/null +++ b/Rearrange Positive and Negative Values.py @@ -0,0 +1,19 @@ +# Rearrange Positive and Negative Values +def moveAll(arr, low, high): + while low <= high: + if arr[low] > 0 and arr[high] < 0: + arr[low], arr[high] = arr[high], arr[low] + low += 1 + high -= 1 + elif arr[low] > 0 and arr[high] > 0: + high -= 1 + elif arr[low] < 0 and arr[high] < 0: + low += 1 + else: + low += 1 + high -= 1 + print(arr) + + +arr = [-12, 11, -13, -5, 6, -7, 5, -3, 11] +moveAll(arr, 0, len(arr)-1)