130 lines
3.5 KiB
Markdown
130 lines
3.5 KiB
Markdown
|
以下是实验报告完成的详细指导和代码示例。
|
|||
|
|
|||
|
---
|
|||
|
|
|||
|
## 实验报告
|
|||
|
|
|||
|
### 一、归并排序和快速排序的基本思想
|
|||
|
|
|||
|
1. **归并排序**
|
|||
|
- **基本思想**:归并排序基于分治思想。将数组递归地分成两个子数组,对每个子数组进行排序后再将其合并。合并时通过比较大小有序地将元素放入结果数组中。
|
|||
|
- **优点**:稳定排序,时间复杂度为 \(O(n \log n)\)。
|
|||
|
- **适用场景**:数据量大且需要稳定排序。
|
|||
|
|
|||
|
2. **快速排序**
|
|||
|
- **基本思想**:快速排序通过选择一个基准元素(通常是数组的第一个或最后一个元素),将数组划分为比基准小和比基准大的两个子数组,然后递归地对这两个子数组进行排序。
|
|||
|
- **优点**:平均时间复杂度为 \(O(n \log n)\),空间复杂度低于归并排序。
|
|||
|
- **适用场景**:数据量大且对稳定性要求不高。
|
|||
|
|
|||
|
---
|
|||
|
|
|||
|
### 二、实验环境
|
|||
|
1. **操作系统**:Windows/Linux。
|
|||
|
2. **开发工具**:Python 3.x,Jupyter Notebook。
|
|||
|
|
|||
|
---
|
|||
|
|
|||
|
### 三、源码实现
|
|||
|
|
|||
|
#### 1. 归并排序
|
|||
|
|
|||
|
```python
|
|||
|
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):
|
|||
|
result = []
|
|||
|
i = j = 0
|
|||
|
|
|||
|
# 合并两个子数组
|
|||
|
while i < len(left) and j < len(right):
|
|||
|
if left[i] <= right[j]:
|
|||
|
result.append(left[i])
|
|||
|
i += 1
|
|||
|
else:
|
|||
|
result.append(right[j])
|
|||
|
j += 1
|
|||
|
|
|||
|
# 加入剩余的元素
|
|||
|
result.extend(left[i:])
|
|||
|
result.extend(right[j:])
|
|||
|
return result
|
|||
|
```
|
|||
|
|
|||
|
#### 2. 快速排序
|
|||
|
|
|||
|
```python
|
|||
|
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]
|
|||
|
|
|||
|
# 递归排序
|
|||
|
return quick_sort(left) + middle + quick_sort(right)
|
|||
|
```
|
|||
|
|
|||
|
---
|
|||
|
|
|||
|
### 四、算法分析
|
|||
|
|
|||
|
#### 1. 归并排序
|
|||
|
- **时间复杂度**:分割数组和合并操作均为 \(O(n \log n)\)。
|
|||
|
- 最好、最坏和平均时间复杂度:\(O(n \log n)\)。
|
|||
|
- **空间复杂度**:需要额外存储中间数组,空间复杂度为 \(O(n)\)。
|
|||
|
|
|||
|
#### 2. 快速排序
|
|||
|
- **时间复杂度**:
|
|||
|
- 最好和平均时间复杂度:\(O(n \log n)\)。
|
|||
|
- 最坏时间复杂度:当每次分割极不均匀时为 \(O(n^2)\)。
|
|||
|
- **空间复杂度**:原地排序,平均空间复杂度为 \(O(\log n)\)。
|
|||
|
|
|||
|
---
|
|||
|
|
|||
|
### 五、实验运行结果
|
|||
|
|
|||
|
#### 测试代码
|
|||
|
|
|||
|
```python
|
|||
|
if __name__ == "__main__":
|
|||
|
import random
|
|||
|
|
|||
|
# 测试数组
|
|||
|
test_array = [random.randint(0, 100) for _ in range(10)]
|
|||
|
print("原始数组:", test_array)
|
|||
|
|
|||
|
# 测试归并排序
|
|||
|
sorted_array_merge = merge_sort(test_array)
|
|||
|
print("归并排序结果:", sorted_array_merge)
|
|||
|
|
|||
|
# 测试快速排序
|
|||
|
sorted_array_quick = quick_sort(test_array)
|
|||
|
print("快速排序结果:", sorted_array_quick)
|
|||
|
```
|
|||
|
|
|||
|
#### 运行结果截图
|
|||
|
运行以上代码后,实验结果显示为随机数组的排序前后对比,例如:
|
|||
|
```
|
|||
|
原始数组: [35, 20, 7, 85, 50, 13, 99, 42, 15, 68]
|
|||
|
归并排序结果: [7, 13, 15, 20, 35, 42, 50, 68, 85, 99]
|
|||
|
快速排序结果: [7, 13, 15, 20, 35, 42, 50, 68, 85, 99]
|
|||
|
```
|
|||
|
|
|||
|
截图保存实验运行结果以提交报告。
|
|||
|
|
|||
|
---
|
|||
|
|
|||
|
希望这份指导能够帮助您完成实验报告!需要进一步协助时,请随时提问。
|