Add files
Date modified: insertion.py: 2017-09-10 13:07 merge.py: 2017-09-10 13:54 numgen.py: 2017-09-10 13:15 sorting.py: 2018-08-26 16:08
This commit is contained in:
parent
f4915b4f07
commit
88bb9f38f5
53
insertion.py
Executable file
53
insertion.py
Executable file
@ -0,0 +1,53 @@
|
||||
"""
|
||||
Insertion Sort
|
||||
|
||||
by BBaoVanC
|
||||
|
||||
This import uses the insertion method of sorting.
|
||||
Pros:
|
||||
Fast for small lists
|
||||
Cons:
|
||||
Gets increasingly slower once you get past a certain amount of numbers
|
||||
"""
|
||||
|
||||
|
||||
def sort(A):
|
||||
for j in range(1, len(A)):
|
||||
# print(j)
|
||||
key = A[j]
|
||||
i = j - 1
|
||||
while i >= 0 and A[i] > key:
|
||||
A[i + 1] = A[i]
|
||||
i = i - 1
|
||||
A[i + 1] = key
|
||||
# print(str(A))
|
||||
return A
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
from timeit import default_timer as timer
|
||||
|
||||
with open("nums.txt") as f:
|
||||
A = f.readlines()
|
||||
A = [x.strip() for x in A]
|
||||
A = [int(x) for x in A]
|
||||
start = timer()
|
||||
|
||||
A = sort(A)
|
||||
|
||||
elapsed_time = timer() - start
|
||||
print("TIME: %.16f" % elapsed_time)
|
||||
|
||||
# print("Opening file")
|
||||
f = open("sort_insertion.txt", "w+")
|
||||
nums2 = list()
|
||||
for item in A:
|
||||
item = str(item) + "\n" # add newline character to each item in the nums2 list...
|
||||
nums2.append(item) # ...
|
||||
nums2[-1] = nums2[-1].strip() # remove newline character from last item in list
|
||||
for item in nums2:
|
||||
# print("Writing number: " + item)
|
||||
f.write(item) # write each number to the file
|
||||
# print("Closing file")
|
||||
f.close()
|
||||
# print("FINISHED")
|
86
merge.py
Executable file
86
merge.py
Executable file
@ -0,0 +1,86 @@
|
||||
"""
|
||||
Merge Sort
|
||||
|
||||
by BBaoVanC
|
||||
|
||||
This import uses the merge method of sorting.
|
||||
Pros:
|
||||
Time doesn't increase as fast as insertion when you add more numbers.
|
||||
Cons:
|
||||
Not the most efficient for small lists
|
||||
"""
|
||||
|
||||
|
||||
def sort(A):
|
||||
def merge(A, p, q, r):
|
||||
# print(A)
|
||||
n1 = q - p
|
||||
# print("n1: " + str(n1))
|
||||
n2 = r - q - 1
|
||||
# print("n2: " + str(n2))
|
||||
L = []
|
||||
R = []
|
||||
for num in range(0, n1 + 2):
|
||||
L.append(0)
|
||||
for num in range(0, n2 + 2):
|
||||
R.append(0)
|
||||
for i in range(0, n1 + 1):
|
||||
# print("i: " + str(i))
|
||||
# print("p + i: " + str(p + i))
|
||||
L[i] = A[p + i]
|
||||
for j in range(0, n2 + 1):
|
||||
R[j] = A[q + j + 1]
|
||||
# print("n1: " + str(n1))
|
||||
# print("n2: " + str(n2))
|
||||
L[n1 + 1] = float('inf')
|
||||
R[n2 + 1] = float('inf')
|
||||
i = 0
|
||||
j = 0
|
||||
for k in range(p, r + 1):
|
||||
if L[i] <= R[j]:
|
||||
A[k] = L[i]
|
||||
i += 1
|
||||
else:
|
||||
A[k] = R[j]
|
||||
j += 1
|
||||
|
||||
return A
|
||||
|
||||
def merge_sort(A, p, r):
|
||||
if p < r:
|
||||
q = int(((p + r + 1) / 2) - 1)
|
||||
A = merge_sort(A, p, q)
|
||||
A = merge_sort(A, q + 1, r)
|
||||
A = merge(A, p, q, r)
|
||||
return A
|
||||
|
||||
A = merge_sort(A, 0, len(A) - 1)
|
||||
return A
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
from timeit import default_timer as timer
|
||||
|
||||
with open("nums.txt") as f:
|
||||
A = f.readlines()
|
||||
A = [x.strip() for x in A]
|
||||
A = [int(x) for x in A]
|
||||
|
||||
start = timer()
|
||||
sorted_a = sort(A)
|
||||
elapsed_time = timer() - start
|
||||
print("TIME: %.16f" % elapsed_time)
|
||||
|
||||
# print("Opening file")
|
||||
f = open("sort_merge.txt", "w+")
|
||||
nums2 = list()
|
||||
for item in sorted_a:
|
||||
item = str(item) + "\n" # add newline character to each item in the nums2 list...
|
||||
nums2.append(item) # ...
|
||||
nums2[-1] = nums2[-1].strip() # remove newline character from last item in list
|
||||
for item in nums2:
|
||||
# print("Writing number: " + item)
|
||||
f.write(item) # write each number to the file
|
||||
# print("Closing file")
|
||||
f.close()
|
||||
# print("FINISHED")
|
48
numgen.py
Executable file
48
numgen.py
Executable file
@ -0,0 +1,48 @@
|
||||
"""
|
||||
Random Number Generator
|
||||
|
||||
by BBaoVanC
|
||||
|
||||
Generates a list of random numbers based on how many you want
|
||||
"""
|
||||
import random
|
||||
|
||||
DO_DEBUG = False
|
||||
|
||||
|
||||
def generate(count, digits, debug):
|
||||
numbers = list() # create a new blank list for storing our numbers
|
||||
count = int(count)
|
||||
digits = int(digits)
|
||||
debug = bool(debug)
|
||||
n = 0
|
||||
while n < count:
|
||||
maxnum = "9" * digits
|
||||
maxnum_int = int(maxnum)
|
||||
number = random.randint(0, maxnum_int)
|
||||
if debug:
|
||||
print("Generated number: " + str(number))
|
||||
numbers.append(number) # add a random number from 0 to 999 to the list
|
||||
n = n + 1 # increment our counter
|
||||
return numbers # return our list of numbers
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
amount = input("Amount: ")
|
||||
length = input("Length: ")
|
||||
nums = generate(int(amount), int(length), DO_DEBUG)
|
||||
|
||||
f = open("nums.txt", "w+")
|
||||
nums2 = list()
|
||||
for item in nums:
|
||||
item = str(item) + "\n" # add newline character to each item in the nums2 list...
|
||||
nums2.append(item) # ...
|
||||
nums2[-1] = nums2[-1].strip() # remove newline character from last item in list
|
||||
for item in nums2:
|
||||
if DO_DEBUG:
|
||||
print("Writing number: " + item)
|
||||
|
||||
f.write(item) # write each number to the file
|
||||
if DO_DEBUG:
|
||||
print("Closing file")
|
||||
f.close()
|
40
sorting.py
Executable file
40
sorting.py
Executable file
@ -0,0 +1,40 @@
|
||||
"""
|
||||
Sorting
|
||||
|
||||
by BBaoVanC
|
||||
|
||||
Compares times between sorting systems.
|
||||
"""
|
||||
|
||||
|
||||
from timeit import default_timer as timer
|
||||
import numgen
|
||||
import merge
|
||||
import insertion
|
||||
|
||||
DO_DEBUG = False
|
||||
|
||||
f = open("compare_sort.csv", "w+") # format: Amount,Insertion,Merge
|
||||
|
||||
n = 0
|
||||
while n <= 1000:
|
||||
A = numgen.generate(n, 8, DO_DEBUG)
|
||||
print("Generated list with length: %i" % n)
|
||||
start1 = timer()
|
||||
sort1 = insertion.sort(A)
|
||||
elapsed_time1 = timer() - start1
|
||||
print("Insertion test finished")
|
||||
|
||||
start2 = timer()
|
||||
sort2 = merge.sort(A)
|
||||
elapsed_time2 = timer() - start2
|
||||
print("Merge test finished")
|
||||
|
||||
f.write("%i,%.8f,%.8f\n" % (len(A), elapsed_time1, elapsed_time2))
|
||||
# print("Amount: %i" % len(A))
|
||||
# print("Insertion: %.5f" % elapsed_time1)
|
||||
# print("Merge: %.5f" % elapsed_time2)
|
||||
# print("--------------------------------")
|
||||
|
||||
n += 1
|
||||
f.close()
|
Loading…
Reference in New Issue
Block a user