常用的内部排序算法
常用的内部排序算法
简单选择排序、直接插入排序和冒泡排序、折半插入排序、希尔排序算法、快速排序算法(递归和非递归)、堆排序
(图片来源网络,侵删)
运行结果:
python
输入数据15,5,6,7,8,9,10
[外链图片转存中…(img-60STknHj-1720750359076)]
[外链图片转存中…(img-QWNWapS5-1720750359078)]
[外链图片转存中…(img-fVhvkUVx-1720750359079)]
C语言
输入数据8 6 80 50 40
[外链图片转存中…(img-yKdnpElL-1720750359079)]
[外链图片转存中…(img-MlEA4ULs-1720750359080)]
[外链图片转存中…(img-D81R9SPg-1720750359080)]
代码
Python代码
import tkinter as tk from tkinter import ttk class SortAlgorithms: def __init__(self): self.comparisons = 0 self.moves = 0 def reset_counters(self): self.comparisons = 0 self.moves = 0 def bubble_sort(self, arr): self.reset_counters() n = len(arr) for i in range(n): for j in range(0, n - i - 1): self.comparisons += 1 if arr[j] > arr[j + 1]: arr[j], arr[j + 1] = arr[j + 1], arr[j] self.moves += 1 yield arr[:] def selection_sort(self, arr): self.reset_counters() n = len(arr) for i in range(n): min_idx = i for j in range(i + 1, n): self.comparisons += 1 if arr[j] = 0 and key 0: for i in range(gap, n): temp = arr[i] j = i while j >= gap and arr[j - gap] > temp: self.comparisons += 1 arr[j] = arr[j - gap] self.moves += 1 j -= gap yield arr[:] arr[j] = temp self.moves += 1 yield arr[:] gap //= 2 def quick_sort(self, arr): self.reset_counters() def _quick_sort(items, low, high): if low pivot: right -= 1 self.comparisons += 1 if left >= right: return right items[left], items[right] = items[right], items[left] self.moves += 1 def heap_sort(self, arr): self.reset_counters() n = len(arr) def heapify(arr, n, i): largest = i l = 2 * i + 1 r = 2 * i + 2 if lC语言代码
#include #include int comparisons = 0; int moves = 0; void reset_counters() { comparisons = 0; moves = 0; } void print_array(int arr[], int size) { for (int i = 0; i arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; moves++; print_array(arr, size); } } } } void selection_sort(int arr[], int size) { reset_counters(); for (int i = 0; i = 0 && arr[j] > key) { comparisons++; arr[j + 1] = arr[j]; moves++; j--; print_array(arr, size); } arr[j + 1] = key; moves++; print_array(arr, size); } } void shell_sort(int arr[], int size) { reset_counters(); for (int gap = size / 2; gap > 0; gap /= 2) { for (int i = gap; i = gap && arr[j - gap] > temp; j -= gap) { comparisons++; arr[j] = arr[j - gap]; moves++; print_array(arr, size); } arr[j] = temp; moves++; print_array(arr, size); } } } void quick_sort(int arr[], int left, int right) { if (left = pivot) { comparisons++; j--; } if (i arr[largest]) { comparisons++; largest = r; } if (largest != i) { int temp = arr[i]; arr[i] = arr[largest]; arr[largest] = temp; moves++; print_array(arr, size); heapify(arr, size, largest); } } void heap_sort(int arr[], int size) { reset_counters(); for (int i = size / 2 - 1; i >= 0; i--) { heapify(arr, size, i); } for (int i = size - 1; i > 0; i--) { int temp = arr[0]; arr[0] = arr[i]; arr[i] = temp; moves++; print_array(arr, size); heapify(arr, i, 0); } } int main() { int choice, size; printf("请输入要排序的数组大小: "); scanf("%d", &size); int arr[size]; printf("请输入数组元素 (用空格隔开): "); for (int i = 0; i
文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。