Difinition
import time
import random
length = int(input("Length of array: "))
arr = [random.random() for i in range(length)]
t0 = time.time() * 1000
arr = arr.sort()
t = time.time() * 1000
print(f"array sorted in {t - t0:.3f} ms.")
def summation(arr):
sum = 0
for n in arr:
sum += n
return sum
def summation(arr):
sum = 0
for n in arr:
sum += n
return sum
Exercise
\(\mathcal O(1)\)
n = int(input())
print(n)
\(\mathcal O(n)\)
n = int(input())
for i in range(1, n, 100):
print(i)
\(\mathcal O(n^2)\)
def insertionSort(arr):
length = len(arr)
for i in range(length):
j = i
while j > 0 and arr[j] < arr[j - 1]:
arr[j], arr[j - 1] = arr[j - 1], arr[j]
j -= 1
return arr
\(\mathcal O(n \log n)\)
print(sorted([int(x) for x in input().split()]))
def mergeSort(arr):
if len(arr) <= 1:
return arr
m = len(arr) // 2
L, R = mergeSort(arr[:m]), mergeSort(arr[m:])
ans = []
while len(L) and len(R):
if L[0] < R[0]:
ans.append(L.pop(0))
else:
ans.append(R.pop(0))
return ans + L + R
\(\mathcal O(\log(a + b))\) ???
def GCD(a, b):
if a == 0:
return b
return GCD(b % a, a)
Application
Thank you!