Skip to content

Instantly share code, notes, and snippets.

@shabbir-hasan
Created December 17, 2018 00:07
Show Gist options
  • Save shabbir-hasan/e6eca5beb80d5ffe9792e6e2547c46c0 to your computer and use it in GitHub Desktop.
Save shabbir-hasan/e6eca5beb80d5ffe9792e6e2547c46c0 to your computer and use it in GitHub Desktop.
find Minimum from an unsorted list at a certain index point without sorting the list completely
#!/usr/bin/python3.7
import time
from random import randint
from functools import update_wrapper
def decorator(d):
"Make function d a decorator: d wraps a function fn."
def _d(fn):
return update_wrapper(d(fn), fn)
update_wrapper(_d, d)
return _d
@decorator
def timeit(f):
"""time a function, used as decorator"""
def new_f(*args, **kwargs):
bt = time.time()
r = f(*args, **kwargs)
et = time.time()
print("time spent on {0}: {1:.20f}s".format(f.__name__, et - bt))
return r
return new_f
# UnsortedArray = [10, 2, 18, 23, 5, 4, 11, 65, 0, 33, 19, 25]
@timeit
def sorts(arr: list):
for i in range(len(arr)):
for j in range(len(arr)):
if arr[i] < arr[j]:
arr[i], arr[j] = arr[j], arr[i]
print(arr)
@timeit
def minfromunsortedlist(Array: list, Index: int):
for i in range(Index+1):
for j in range(i, len(Array)):
if Array[i] > Array[j]:
Array[i], Array[j] = Array[j], Array[i]
print(f"the sorted full array upto the {Index} index point id is {Array}")
print(f"the sorted array upto the {Index} index point id is {Array[:Index+1]}")
print(f"the min value at {Index} index point is {Array[Index]}")
def generaterandomarrayofsize(size: int):
unsortedarray = []
for _ in range(size):
unsortedarray.append(randint(0, 100))
return unsortedarray
UnsortedArray = generaterandomarrayofsize(35)
print(UnsortedArray)
sorts(UnsortedArray.copy())
minfromunsortedlist(UnsortedArray, 5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment