[1] FINISH 1-st-exercise #148

Merged
git_admin merged 23 commits from IvanBud123/2026-rff_mp:1-st-exercise into develop 2026-03-15 06:42:24 +00:00
2 changed files with 163 additions and 1 deletions
Showing only changes of commit da65d02bd7 - Show all commits

View File

@ -134,6 +134,7 @@ def ht_insert(buckets, name, phone):
head = buckets[index] head = buckets[index]
new_head = ll_insert(head, name, phone) new_head = ll_insert(head, name, phone)
buckets[index] = new_head buckets[index] = new_head
return buckets
print(f"\n\n\n ====== TEST INSERT HASH ======") print(f"\n\n\n ====== TEST INSERT HASH ======")
print(buckets) print(buckets)
@ -177,6 +178,7 @@ def ht_delete(buckets, name):
head = buckets[index] head = buckets[index]
new_head = ll_delete(head, name) new_head = ll_delete(head, name)
buckets[index] = new_head buckets[index] = new_head
return buckets
print("====== GLOBAL TEST FOR HASH BASED FUN ======") print("====== GLOBAL TEST FOR HASH BASED FUN ======")
@ -275,7 +277,7 @@ def bst_delete(root,name):
if root['left'] is None: if root['left'] is None:
return root['right'] return root['right']
if root['right'] is None: if root['right'] is None:
return root['right'] return root['left']
min_node = find_min(root['right']) min_node = find_min(root['right'])
root['name'] = min_node['name'] root['name'] = min_node['name']
@ -324,3 +326,132 @@ print("====== END TEST ======")
# ======================== EXPEREMENT CHAPTER ========================
import random
import time
import csv
import sys
sys.setrecursionlimit(20000)
def generate_records(n, seed=42):
random.seed(seed)
records = []
for i in range(1, n+1):
name = f"User_{i:05d}"
phone = f"{random.randint(100,999)}-{random.randint(1000,9999)}"
records.append((name, phone))
return records
def prepare_datasets(base_records):
shuffled = base_records.copy()
random.shuffle(shuffled)
sorted_records = sorted(base_records, key=lambda x: x[0])
return shuffled, sorted_records
def run_experiment(struct_funcs, records, mode_name, repeats=5):
results = []
for rep in range(repeats):
struct = struct_funcs['create']()
# enter all records
start = time.perf_counter()
for name, phone in records:
struct = struct_funcs['insert'](struct, name, phone)
end = time.perf_counter()
insert_time = end - start
# search for 110 records (100 real + 10 None)
existing_names = [name for name, _ in records]
sample_existing = random.sample(existing_names, 100)
nonexistent = [f"None_{i}" for i in range(10)]
search_names = sample_existing + nonexistent
random.shuffle(search_names)
start = time.perf_counter()
for name in search_names:
_ = struct_funcs['find'](struct, name)
end = time.perf_counter()
find_time = end - start
# delete 10 random records
to_delete = random.sample(existing_names, 10)
start = time.perf_counter()
for name in to_delete:
struct = struct_funcs['delete'](struct, name)
end = time.perf_counter()
delete_time = end - start
results.append({
'structure': struct_funcs['name'],
'mode': mode_name,
'repetition': rep+1,
'insert_time': insert_time,
'find_time': find_time,
'delete_time': delete_time
})
return results
def main():
N = 1000
base_records = generate_records(N)
shuffled, sorted_records = prepare_datasets(base_records)
structures = {
'LinkedList': {
'name': 'LinkedList',
'create': lambda: None,
'insert': ll_insert,
'find': ll_find,
'delete': ll_delete,
'list_all': ll_list_all
},
'HashTable': {
'name': 'HashTable',
'create': lambda: [None] * 10,
'insert': ht_insert,
'find': ht_find,
'delete': ht_delete,
'list_all': ht_list_all
},
'BST': {
'name': 'BST',
'create': lambda: None,
'insert': bst_insert,
'find': bst_find,
'delete': bst_delete,
'list_all': bst_list_all
}
}
all_results = []
repeats = 5
for struct_name, funcs in structures.items():
print(f"Testing {struct_name} on random order...")
res = run_experiment(funcs, shuffled, 'random', repeats)
all_results.extend(res)
print(f"Testing {struct_name} in sorted order...")
res = run_experiment(funcs, sorted_records, 'sorted', repeats)
all_results.extend(res)
with open('experiment_results.csv', 'w', newline='', encoding='utf-8') as f:
writer = csv.writer(f)
writer.writerow(['Structure', 'Mode', 'Repeat', 'Insert (sec)', 'Search (sec)', 'Delete (sec)'])
for r in all_results:
writer.writerow([
r['structure'],
r['mode'],
r['repetition'],
f"{r['insert_time']:.6f}",
f"{r['find_time']:.6f}",
f"{r['delete_time']:.6f}"
])
print("The experiment is complete. The results are saved in experiment_results.csv.")
if __name__ == '__main__':
main()

View File

@ -0,0 +1,31 @@
Structure,Mode,Repeat,Insert (sec),Search (sec),Delete (sec)
LinkedList,random,1,0.110077,0.006972,0.000836
LinkedList,random,2,0.105159,0.008245,0.000690
LinkedList,random,3,0.111559,0.008142,0.000632
LinkedList,random,4,0.116443,0.007497,0.000519
LinkedList,random,5,0.128044,0.007971,0.000584
LinkedList,sorted,1,0.098338,0.007254,0.000515
LinkedList,sorted,2,0.101167,0.004475,0.000742
LinkedList,sorted,3,0.128987,0.007993,0.000649
LinkedList,sorted,4,0.104280,0.006707,0.000676
LinkedList,sorted,5,0.129192,0.007550,0.000669
HashTable,random,1,0.014989,0.001023,0.000067
HashTable,random,2,0.010627,0.000918,0.000083
HashTable,random,3,0.012970,0.001416,0.000090
HashTable,random,4,0.013139,0.001172,0.000092
HashTable,random,5,0.013617,0.000928,0.000091
HashTable,sorted,1,0.016275,0.000988,0.000099
HashTable,sorted,2,0.017436,0.000888,0.000094
HashTable,sorted,3,0.017177,0.000977,0.000080
HashTable,sorted,4,0.013340,0.001643,0.000248
HashTable,sorted,5,0.013750,0.001013,0.000176
BST,random,1,0.006446,0.000436,0.000073
BST,random,2,0.005296,0.000442,0.000070
BST,random,3,0.006276,0.000486,0.000055
BST,random,4,0.004277,0.000230,0.000033
BST,random,5,0.004287,0.000230,0.000034
BST,sorted,1,0.286207,0.021908,0.002439
BST,sorted,2,0.275166,0.024806,0.003805
BST,sorted,3,0.334667,0.020466,0.002237
BST,sorted,4,0.319684,0.019011,0.002984
BST,sorted,5,0.299339,0.028599,0.001916
1 Structure Mode Repeat Insert (sec) Search (sec) Delete (sec)
2 LinkedList random 1 0.110077 0.006972 0.000836
3 LinkedList random 2 0.105159 0.008245 0.000690
4 LinkedList random 3 0.111559 0.008142 0.000632
5 LinkedList random 4 0.116443 0.007497 0.000519
6 LinkedList random 5 0.128044 0.007971 0.000584
7 LinkedList sorted 1 0.098338 0.007254 0.000515
8 LinkedList sorted 2 0.101167 0.004475 0.000742
9 LinkedList sorted 3 0.128987 0.007993 0.000649
10 LinkedList sorted 4 0.104280 0.006707 0.000676
11 LinkedList sorted 5 0.129192 0.007550 0.000669
12 HashTable random 1 0.014989 0.001023 0.000067
13 HashTable random 2 0.010627 0.000918 0.000083
14 HashTable random 3 0.012970 0.001416 0.000090
15 HashTable random 4 0.013139 0.001172 0.000092
16 HashTable random 5 0.013617 0.000928 0.000091
17 HashTable sorted 1 0.016275 0.000988 0.000099
18 HashTable sorted 2 0.017436 0.000888 0.000094
19 HashTable sorted 3 0.017177 0.000977 0.000080
20 HashTable sorted 4 0.013340 0.001643 0.000248
21 HashTable sorted 5 0.013750 0.001013 0.000176
22 BST random 1 0.006446 0.000436 0.000073
23 BST random 2 0.005296 0.000442 0.000070
24 BST random 3 0.006276 0.000486 0.000055
25 BST random 4 0.004277 0.000230 0.000033
26 BST random 5 0.004287 0.000230 0.000034
27 BST sorted 1 0.286207 0.021908 0.002439
28 BST sorted 2 0.275166 0.024806 0.003805
29 BST sorted 3 0.334667 0.020466 0.002237
30 BST sorted 4 0.319684 0.019011 0.002984
31 BST sorted 5 0.299339 0.028599 0.001916