mirror of
https://litchi.icu/ngc2207/algorithm.git
synced 2025-07-04 09:10:55 +00:00
first commit
This commit is contained in:
commit
a4fc2fa2bd
61
merge_sort.py
Normal file
61
merge_sort.py
Normal file
@ -0,0 +1,61 @@
|
||||
import argparse
|
||||
import random
|
||||
import time
|
||||
|
||||
def merge_sort(arr):
|
||||
if len(arr) <= 1:
|
||||
return arr
|
||||
|
||||
mid = len(arr) // 2
|
||||
left_half = merge_sort(arr[:mid])
|
||||
right_half = merge_sort(arr[mid:])
|
||||
|
||||
return merge(left_half, right_half)
|
||||
|
||||
def merge(left, right):
|
||||
sorted_array = []
|
||||
left_index, right_index = 0, 0
|
||||
|
||||
while left_index < len(left) and right_index < len(right):
|
||||
if left[left_index] < right[right_index]:
|
||||
sorted_array.append(left[left_index])
|
||||
left_index += 1
|
||||
else:
|
||||
sorted_array.append(right[right_index])
|
||||
right_index += 1
|
||||
|
||||
sorted_array.extend(left[left_index:])
|
||||
sorted_array.extend(right[right_index:])
|
||||
|
||||
return sorted_array
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser(description='Merge Sort with random array generation.')
|
||||
parser.add_argument('--length', type=int, default=10, help='Length of the random array to generate (default: 10)')
|
||||
parser.add_argument('--min', type=int, default=1, help='Minimum value for random numbers (default: 1)')
|
||||
parser.add_argument('--max', type=int, default=100, help='Maximum value for random numbers (default: 100)')
|
||||
args = parser.parse_args()
|
||||
|
||||
start_time = time.time()
|
||||
|
||||
generate_start_time = time.time()
|
||||
arr = [random.randint(args.min, args.max) for _ in range(args.length)]
|
||||
generate_end_time = time.time()
|
||||
|
||||
print(f"Original array: {arr}")
|
||||
|
||||
sort_start_time = time.time()
|
||||
sorted_arr = merge_sort(arr)
|
||||
sort_end_time = time.time()
|
||||
|
||||
print(f"Sorted array: {sorted_arr}")
|
||||
|
||||
end_time = time.time()
|
||||
|
||||
generate_time = generate_end_time - generate_start_time
|
||||
sort_time = sort_end_time - sort_start_time
|
||||
total_time = end_time - start_time
|
||||
|
||||
print(f"Time taken to generate data: {generate_time:.6f} seconds")
|
||||
print(f"Time taken to sort data: {sort_time:.6f} seconds")
|
||||
print(f"Total time taken: {total_time:.6f} seconds")
|
17
quick_sort.py
Normal file
17
quick_sort.py
Normal file
@ -0,0 +1,17 @@
|
||||
def quick_sort(arr):
|
||||
if len(arr) <= 1:
|
||||
return arr
|
||||
|
||||
pivot = arr[len(arr) // 2]
|
||||
left = [x for x in arr if x < pivot]
|
||||
middle = [x for x in arr if x == pivot]
|
||||
right = [x for x in arr if x > pivot]
|
||||
|
||||
print(f"Left: {left}, Middle: {middle}, Right: {right}")
|
||||
|
||||
return quick_sort(left) + middle + quick_sort(right)
|
||||
|
||||
if __name__ == '__main__':
|
||||
arr = [12, 11, 13, 5, 6, 7]
|
||||
print(f"Original array: {arr}")
|
||||
print(f"Sorted array: {quick_sort(arr)}")
|
117
tool.py
Normal file
117
tool.py
Normal file
@ -0,0 +1,117 @@
|
||||
import argparse
|
||||
import random
|
||||
import time
|
||||
|
||||
def merge_sort(arr):
|
||||
"""
|
||||
Perform merge sort on the given array.
|
||||
|
||||
:param arr: List of integers to be sorted.
|
||||
:return: Sorted list of integers.
|
||||
"""
|
||||
if len(arr) <= 1:
|
||||
return arr
|
||||
|
||||
mid = len(arr) // 2
|
||||
left_half = merge_sort(arr[:mid])
|
||||
right_half = merge_sort(arr[mid:])
|
||||
|
||||
return merge(left_half, right_half)
|
||||
|
||||
def merge(left, right):
|
||||
"""
|
||||
Merge two sorted lists into a single sorted list.
|
||||
|
||||
:param left: Sorted list of integers.
|
||||
:param right: Sorted list of integers.
|
||||
:return: Merged sorted list of integers.
|
||||
"""
|
||||
sorted_array = []
|
||||
left_index, right_index = 0, 0
|
||||
|
||||
while left_index < len(left) and right_index < len(right):
|
||||
if left[left_index] < right[right_index]:
|
||||
sorted_array.append(left[left_index])
|
||||
left_index += 1
|
||||
else:
|
||||
sorted_array.append(right[right_index])
|
||||
right_index += 1
|
||||
|
||||
sorted_array.extend(left[left_index:])
|
||||
sorted_array.extend(right[right_index:])
|
||||
|
||||
return sorted_array
|
||||
|
||||
def quick_sort(arr):
|
||||
"""
|
||||
Perform quick sort on the given array.
|
||||
|
||||
:param arr: List of integers to be sorted.
|
||||
:return: Sorted list of integers.
|
||||
"""
|
||||
if len(arr) <= 1:
|
||||
return arr
|
||||
|
||||
pivot = arr[len(arr) // 2]
|
||||
left = [x for x in arr if x < pivot]
|
||||
middle = [x for x in arr if x == pivot]
|
||||
right = [x for x in arr if x > pivot]
|
||||
|
||||
return quick_sort(left) + middle + quick_sort(right)
|
||||
|
||||
def generate_random_array(length, min_value, max_value):
|
||||
"""
|
||||
Generate a random array of integers.
|
||||
|
||||
:param length: Length of the array.
|
||||
:param min_value: Minimum value for random numbers.
|
||||
:param max_value: Maximum value for random numbers.
|
||||
:return: List of random integers.
|
||||
"""
|
||||
return [random.randint(min_value, max_value) for _ in range(length)]
|
||||
|
||||
def measure_time(func, *args, **kwargs):
|
||||
"""
|
||||
Measure the execution time of a function.
|
||||
|
||||
:param func: Function to be measured.
|
||||
:param args: Positional arguments for the function.
|
||||
:param kwargs: Keyword arguments for the function.
|
||||
:return: Tuple containing the result of the function and the execution time.
|
||||
"""
|
||||
start_time = time.time()
|
||||
result = func(*args, **kwargs)
|
||||
end_time = time.time()
|
||||
return result, end_time - start_time
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser(description='Sorting with random array generation.')
|
||||
parser.add_argument('--length', type=int, default=10, help='Length of the random array to generate (default: 10)')
|
||||
parser.add_argument('--min', type=int, default=1, help='Minimum value for random numbers (default: 1)')
|
||||
parser.add_argument('--max', type=int, default=100, help='Maximum value for random numbers (default: 100)')
|
||||
parser.add_argument('--sort', type=str, choices=['merge', 'quick'], default='merge', help='Sorting algorithm to use (default: merge)')
|
||||
args = parser.parse_args()
|
||||
|
||||
# Record the start time of the program
|
||||
program_start_time = time.time()
|
||||
|
||||
# Generate a random array
|
||||
arr, generate_time = measure_time(generate_random_array, args.length, args.min, args.max)
|
||||
print(f"Original array: {arr}")
|
||||
|
||||
# Sort the array
|
||||
if args.sort == 'merge':
|
||||
sorted_arr, sort_time = measure_time(merge_sort, arr)
|
||||
elif args.sort == 'quick':
|
||||
sorted_arr, sort_time = measure_time(quick_sort, arr)
|
||||
|
||||
print(f"Sorted array: {sorted_arr}")
|
||||
|
||||
program_end_time = time.time()
|
||||
|
||||
# Calculate and print the times
|
||||
total_time = program_end_time - program_start_time
|
||||
|
||||
print(f"Time taken to generate data: {generate_time:.6f} seconds")
|
||||
print(f"Time taken to sort data: {sort_time:.6f} seconds")
|
||||
print(f"Total time taken: {total_time:.6f} seconds")
|
Loading…
Reference in New Issue
Block a user