Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c6b5981

Browse files
more files searching and sorting
1 parent c1b82ae commit c6b5981

11 files changed

+389
-0
lines changed

‎BubbleSort.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
def bubble_sort(a_list):
2+
exchanges = True
3+
pass_num = len(a_list)-1
4+
while pass_num > 0 and exchanges:
5+
exchanges = False
6+
for i in range(pass_num):
7+
if a_list[i] > a_list[i+1]:
8+
exchanges = True
9+
#swapping
10+
a_list[i],a_list[i+1] = a_list[i+1],a_list[i]
11+
pass_num -= 1
12+
a_list = [54,26,93,17,77,31,44,55,20,3]
13+
bubble_sort(a_list)
14+
print(a_list)

‎InsertionSort.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
def insertion_sort(a_list):
2+
for index in range(1,len(a_list)):
3+
current_value = a_list[index]
4+
position = index
5+
6+
while position > 0 and current_value < a_list[position - 1]:
7+
a_list[position] = a_list[position - 1]
8+
position -= 1
9+
a_list[position] = current_value
10+
11+
a_list = [54, 26, 93, 17, 77, 31, 44, 55, 20]
12+
insertion_sort(a_list)
13+
print(a_list)
14+

‎Map.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
class HashTable:
2+
def __init__(self):
3+
self.size = 11
4+
self.slots = [None] * self.size
5+
self.data = [None] * self.size
6+
7+
def put(self, key, data):
8+
hash_value = self.hash_function(key, len(self.slots))
9+
10+
if self.slots[hash_value] == None:
11+
self.slots[hash_value] = key
12+
self.data[hash_value] = data
13+
else:
14+
if self.slots[hash_value] == key:
15+
self.data[hash_value] = data
16+
else:
17+
next_slot = self.rehash(hash_value, len(self.slots))
18+
19+
while self.slots[next_slot] != None and self.slots[next_slot] != key:
20+
print(next_slot)
21+
next_slot = self.rehash(next_slot, len(self.slots))
22+
23+
if self.slots[next_slot] == None:
24+
self.slots[next_slot] = key
25+
self.data[next_slot] = data
26+
else:
27+
self.data[next_slot] = data
28+
29+
def get(self, key):
30+
start_slot = self.hash_function(key, len(self.slots))
31+
32+
position = start_slot
33+
found = False
34+
stop = False
35+
data = None
36+
37+
while self.slots[position] != None and not found and not stop:
38+
if self.slots[position] == key:
39+
found = True
40+
data = self.data[position]
41+
else:
42+
position = self.rehash(position, len(self.slots))
43+
if position == start_slot:
44+
stop = True
45+
return data
46+
47+
def __getitem__(self, key):
48+
return self.get(key)
49+
50+
def __setitem__(self,key,data):
51+
self.put(key,data)
52+
53+
def hash_function(self, key, size):
54+
return key % size
55+
56+
def rehash(self, old_hash, size):
57+
return (old_hash+1) % size
58+
59+
60+
61+
print("hi")
62+
h=HashTable()
63+
h[54]="cat"
64+
h[20]="dog"
65+
h[93]="lion"
66+
h[17]="tiger"
67+
h[77]="bird"
68+
h[31]="cow"
69+
h[44]="goat"
70+
print("hi8")
71+
h[55]="pig"
72+
#print("hi9")
73+
h[26]="chicken"
74+
#print("hi10")
75+
print(h.slots)
76+
print(h.data)
77+
print(h[31])
78+
print(h[93])
79+
print(h[20])
80+
print(h[32])

‎MergeSort.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
def merge_sort(a_list):
2+
if len(a_list) > 1:
3+
mid = len(a_list) // 2
4+
5+
left_list = a_list[ :mid]
6+
right_list = a_list[mid: ]
7+
8+
merge_sort(left_list)
9+
merge_sort(right_list)
10+
11+
i = 0
12+
j = 0
13+
k = 0
14+
15+
while i < len(left_list) and j < len(right_list):
16+
if left_list[i] < right_list[j]:
17+
a_list[k] = left_list[i]
18+
i += 1
19+
else:
20+
a_list[k] = right_list[j]
21+
j += 1
22+
k += 1
23+
24+
while i < len(left_list):
25+
a_list[k] = left_list[i]
26+
i += 1
27+
k += 1
28+
29+
while j < len(right_list):
30+
a_list[k] = right_list[j]
31+
j += 1
32+
k += 1
33+
print("Merging ", a_list)
34+
35+
a_list = [54, 26, 93, 17, 77, 31, 44, 55, 20]
36+
merge_sort(a_list)
37+
print(a_list)
38+

‎QuickSort.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
def quick_sort(a_list):
2+
quick_sort_helper(a_list,0,len(a_list)-1)
3+
4+
def quick_sort_helper(a_list, first, last):
5+
if first < last:
6+
split_point = partition(a_list, first, last)
7+
8+
quick_sort_helper(a_list, first, split_point-1)
9+
quick_sort_helper(a_list, split_point + 1, last)
10+
11+
def partition(a_list, first, last):
12+
pivot_value = a_list[first]
13+
14+
left_mark = first + 1
15+
right_mark = last
16+
17+
done = False
18+
while not done:
19+
while left_mark <= right_mark and \
20+
a_list[left_mark] <= pivot_value:
21+
left_mark += 1
22+
while a_list[right_mark] >= pivot_value and \
23+
right_mark >= left_mark:
24+
right_mark -= 1
25+
26+
if right_mark < left_mark:
27+
done = True
28+
else:
29+
temp = a_list[left_mark]
30+
a_list[left_mark] = a_list[right_mark]
31+
a_list[right_mark] = temp
32+
33+
temp = a_list[first]
34+
a_list[first] = a_list[right_mark]
35+
a_list[right_mark] = temp
36+
37+
return right_mark
38+
39+
a_list = [54, 26, 93, 17, 77, 31, 44, 55, 20]
40+
quick_sort(a_list)
41+
print(a_list)
42+
43+

‎Searching_binarySearch.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
def binary_search(a_list, item):
2+
first = 0
3+
last = len(a_list) - 1
4+
found = False
5+
6+
while first <= last and not found:
7+
mid = (first + last) / 2
8+
if a_list[mid] == item:
9+
found = True
10+
elif a_list[mid] < item:
11+
first = mid +1
12+
else:
13+
last = mid - 1
14+
15+
return found
16+
17+
test_list = [0, 1, 2, 8, 13, 17, 19, 32, 42,]
18+
print(binary_search(test_list, 3))
19+
print(binary_search(test_list, 13))

‎Searching_sequential.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
def sequential_search(a_list, item):
2+
pointer = 0
3+
found = False
4+
5+
while not found and pointer < len(a_list):
6+
if item == a_list[pointer]:
7+
found = True
8+
else:
9+
pointer += 1
10+
11+
return found
12+
13+
def ordered_sequential_search(a_list, item):
14+
pointer = 0
15+
found = False
16+
stop = False
17+
while not found and pointer < len(a_list) and not stop:
18+
if item == a_list[pointer]:
19+
found = True
20+
elif item < a_list[pointer]:
21+
stop = True
22+
else:
23+
pointer += 1
24+
25+
return found
26+
27+
test_list = [1, 2, 32, 8, 17, 19, 42, 13, 0]
28+
print(sequential_search(test_list, 3))
29+
print(sequential_search(test_list, 13))
30+
31+
test_list = [0, 1, 2, 8, 13, 17, 19, 32, 42,]
32+
print(ordered_sequential_search(test_list, 3))
33+
print(ordered_sequential_search(test_list, 13))

‎SelectionSort.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def selection_sort(a_list):
2+
for fill_slot in range(len(a_list) - 1, 0, -1):
3+
pos_of_max = 0
4+
for location in range(1, fill_slot+1):
5+
if a_list[location] > a_list[pos_of_max]:
6+
pos_of_max = location
7+
a_list[pos_of_max], a_list[fill_slot] = a_list[fill_slot], a_list[pos_of_max]
8+
9+
a_list = [54, 26, 93, 17, 77, 31, 44, 55, 20,3]
10+
selection_sort(a_list)
11+
print(a_list)

‎ShellSort.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def shell_sort(a_sort):
2+
sublist_count = len(a_list) // 2
3+
while sublist_count > 0:
4+
for start_position in range(sublist_count):
5+
gap_insertion_sort(a_list, start_position, sublist_count)
6+
7+
print("After increments of size ", sublist_count, "The list is ", a_list)
8+
9+
sublist_count = sublist_count // 2
10+
def gap_insertion_sort(a_list, start, gap):
11+
for i in range(start+gap, len(a_list), gap):
12+
current_value = a_list[i]
13+
position = i
14+
while position >= gap and a_list[position-gap]>current_value:
15+
a_list[position] = a_list[position-gap]
16+
position = position - gap
17+
a_list[position] = current_value
18+
19+
a_list = [54, 26, 93, 17, 77, 31, 44, 55, 20]
20+
shell_sort(a_list)
21+
print(a_list)

‎Tree_UsingListofLists.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
def binary_tree(n):
2+
return [n,[],[]]
3+
4+
def insert_left(root,new_value):
5+
t = root.pop(1)
6+
if len(t) > 1:
7+
root.insert(1,[new_value,t,[]])
8+
else:
9+
root.insert(1,[new_value,[],[]])
10+
return root
11+
12+
def insert_right(root,new_value):
13+
t = root.pop(2)
14+
if len(t) > 1:
15+
root.insert(2,[new_value,[],t])
16+
else:
17+
root.insert(2,[new_value,[],[]])
18+
return root
19+
20+
def get_root_val(root):
21+
return root[0]
22+
23+
def set_root_val(root, new_val):
24+
root[0] = new_val
25+
26+
def get_left_child(root):
27+
return root[1]
28+
29+
def get_right_child(root):
30+
return root[2]
31+
32+
def build_tree():
33+
x = binary_tree('a')
34+
insert_left(x,'b')
35+
insert_right(x,'c')
36+
insert_right(get_left_child(x),'d')
37+
insert_left(get_right_child(x),'e')
38+
insert_right(get_right_child(x),'f')
39+
return x
40+
41+
print(build_tree())
42+
"""
43+
x = binary_tree('a')
44+
insert_left(x,'b')
45+
insert_right(x,'c')
46+
print x
47+
insert_right(get_right_child(x), 'd')
48+
print x
49+
insert_left(get_right_child(get_right_child(x)), 'e')
50+
print x
51+
52+
# another tree
53+
r = binary_tree(3)
54+
print(r)
55+
insert_left(r, 4)
56+
insert_left(r, 5)
57+
insert_right(r, 6)
58+
insert_right(r, 7)
59+
print(r)
60+
l = get_left_child(r)
61+
print(l)
62+
set_root_val(l, 9)
63+
print(r)
64+
insert_left(l, 11)
65+
print(r)
66+
print(get_right_child(get_right_child(r)))"""

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /