1, experimental purposes and tasks
Solve an array Arrangement with the law
Second, the experimental environment
c ++ or java
Third, the description of the problem
Input: An array
Output: an array of a large array of childhood to
Fourth, programming tasks
For an array, with the thought to divide and conquer arranged in ascending large.
Fifth, data input
1000 or more randomly generated data in the input document input.txt
Sixth, the resulting output
Such as arrays A = {3, 41, 52, 26, 38, 57, 9, 49}, {output 3,9,26,38,41,49.51,57}.
Seven experimental contents of the report
The main program codes
import math
import ioTool
#合并
def merge(A, B, s):
Aindex = 0 #A下标
Bindex = 0 #B下标
while Aindex + Bindex < len(s):
#如果
if Bindex == len(B) or (Aindex<len(A) and A[Aindex] > B[Bindex]):
s[Aindex+Bindex] = A[Aindex]
Aindex = Aindex + 1
else:
s[Aindex+Bindex] = B[Bindex]
Bindex = Bindex + 1
return s
#拆分
num = 0
def merge_soft(s):
global num
n = len(s)
#1、如果到了第1个了,或者没有就直接返回,不用排序了
if n == 1:
return
#2、拆分
mid = math.floor(n/2) #向上取整获取中间值 或者n//2可以拿到除数的整数
A = s[0:mid] #把0到mid的数据分到
B = s[mid:n] #后半部分
merge_soft(A)
merge_soft(B)
# print("第:",num,"次,拆分了A", A)
# print("第:", num, "次,拆分了B", B)
num = num + 1
# print("拆分了B", B)
#3、合并
arr = merge(A,B,s)
return arr
if __name__ == '__main__':
#随机生成数据并存放在input.txt文件中
# ioTool.randomData(1100000,-5000,182000,"input1.txt");
#读取数据
# s = ioTool.readLine("input1.txt");
s = [12,5,1,9,-11] #课本测试数据
arr = merge_soft(s)
ioTool.writeLine(arr,"output1.txt")
print("排序结果:",arr)
Test Results:
正在打开 output1.txt 文件
结果写入 output1.txt 文件完毕
排序结果: [12, 9, 5, 1, -11]
ioTool.py file
import random
#随机生成数
def randomData(n,x,y,addressURL):
# 打开input文件
file = open(addressURL, 'w', encoding='utf8');
print("正在随机生成数据,写入",addressURL,"文件")
# 产生10万个数据,从0到100000
i = 0;
while i < n:
file.write(str(random.randint(x, y))+"\n")
i = i + 1;
file.close()
print("写入",addressURL,"文件完毕")
#打开input.txt文件 读取文件 返回一个[]
def readLine(addressURL):
# 行数
count_line = 0;
# 定义存储的元组
arr1 = []
file = open(addressURL, 'r', encoding='utf8')
for line in file.read().splitlines():
arr1.append(int(line))
count_line = 1 + count_line # 计算行数
file.close()
return arr1
#写入文件
def writeLine(A,addressURL):
# 打开input文件
file = open(addressURL, 'w', encoding='utf8')
print("正在打开",addressURL,"文件")
i = 0
while i < len(A):
file.write(str(A[i]) + "\n")
i = i + 1
file.close()
print("结果写入",addressURL,"文件完毕")
Explanation
A method as defined in the Program:
# combined
Merge (A, B, S)
# split
merge_soft (s)
Program Entry #
IF name == ' main ':
# Randomly generated number
RandomData (n-, X, Y, addressURL):
# Open file returns a file read input.txt []
the readLine (addressURL):
# write to file
writeLine (A, addressURL):
Method 1) to read and write files and the definition
file reading method: readLine (addressURL)
parameter refers addressURL address to read the file
Method write file: writeLine (A, addressURL)
parameters A: sorted array
parameters addressURL: the array sort number as a write to that file address
defined in 2), and a method of generating random numbers achieve
randomly generated data: randomData ( n, x, y, addressURL)
parameter n: number of generation n
parameters x, y: n number generation range
parameters addressURL: after generating, for a save where
Method 3) defining and implementing the data file is read and the
open file returns a file read addressURL []: readLine (addressURL)
Parameter addressURL: file to read
Return Value: number array
4) resolution methods
# resolution mass over an array
merge_soft (S)
. 5) the combined method
# union, the a and B were combined, s location
merge (a, B, s)
Experimental results
Results 1: test data: [12,5,1,9, -11] Sort
Results: randomly generated numbers 1100000, from -5000 to 182,000, a file stored in the input1.txt