选择排序 堆排序

选择排序

选择排序一般指待排序的数中挑选最值,放数组末位,减小数组长度,再挑选待排序数中的最值。
选择排序一般有:简单选择排序堆排序

简单选择排序

也称为直接选择排序,在待排序数组中找到最值下标与已排序数组的新下标交换元素值。

简单选择排序-代码

void SelectSort(int A[], int n)
{
    for (int i = n - 1; i >= 0; i--)
    {
        int max = i;
        for (int j = i - 1; j >= 0; j--)
            if (A[max] < A[j])
                max = j;

        if (max != i)
            swap(A[max], A[i]);
    }
}

int main()
{
    int A[] = {8, 7, 5, 6, 1, 3, 2, 9, 10, 234, 121, 4, 2324, 98};
    SelectSort(A, 14);
    for (int i = 0; i < 14; i++)
        cout << A[i] << " ";
    return 0;
}

堆排序-思路

堆排序演示

使用数组存储堆,使用筛选法建立堆。

  • 堆的定义
    堆一般有两种,小顶堆和大顶堆,堆顶元素是组成堆所有元素中的最值,小顶堆堆顶元素是所有数中的最小值,以数组存储堆,一般A[0]表示堆顶元素。大顶堆堆顶元素是所有元素的最大值。
  • 从小到大排序
    建立大顶堆,不断交换堆顶元素与数组末位元素,重新调整为数组长度-1的大顶堆。大的都到了数组后面,所以结束排序后输出数组是从小到大排序。
  • 从大到小排序
    建立小顶堆,不断交换堆顶元素与数组末位元素,重新调整为数组长度-1的小顶堆,小的都到了数组后面,所以结束排序后输出数组是从大到小排序。

堆排序-代码

修改代码24行和27行可以变为从大到小排序

void Display(int A[], int n)
{
  for (int i = 0; i < n; i++)
  {
    if (i != 0)
      cout << " ";
    cout << A[i];
  }
  cout << endl;
}

void AdjustDown(int A[], int root, int n)
{
  int left = root * 2 + 1;
  int temp = A[root];

  while (left < n)
  {
    // left值变为子树的最大值
    if (left + 1 < n && A[left] < A[left + 1])
      left++;

    if (temp < A[left])
    {
      A[root] = A[left];
      root = left;
      left = root * 2 + 1;
    }
    else
      break;
  }
  A[root] = temp;
}

// root为最后一个非叶子结点
void GenerateHeap(int A[], int root, int n)
{
  if (root >= 0)
  {
    AdjustDown(A, root, n);
    GenerateHeap(A, root - 1, n);
  }
}

// n为A数组长度
void HeapSort(int A[], int n)
{
  // 建立堆
  GenerateHeap(A, n / 2 - 1, n);

  // 交换堆顶元素与末位元素
  for (int i = n - 1; i > 0; i--)
  {
    swap(A[i], A[0]);
    AdjustDown(A, 0, i);
  }
}

int main()
{
  int A[] = {7, 6, 8, 4, 5, 9, 1, 2, 10, 23, 15, 3, 65};
  HeapSort(A, 13);
  Display(A, 13);
  return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以给你一个使用 C++ 实现的测试代码,包括直接插入排序、希尔排序、冒泡排序、快速排序、直接选择排序堆排序,代码如下: ```cpp #include <iostream> #include <ctime> #include <cstdlib> using namespace std; // 直接插入排序 void insertSort(int arr[], int len) { for (int i = 1; i < len; i++) { int temp = arr[i]; int j = i - 1; while (j >= 0 && arr[j] > temp) { arr[j + 1] = arr[j]; j--; } arr[j + 1] = temp; } } // 希尔排序 void shellSort(int arr[], int len) { int gap = len / 2; while (gap > 0) { for (int i = gap; i < len; i++) { int temp = arr[i]; int j = i - gap; while (j >= 0 && arr[j] > temp) { arr[j + gap] = arr[j]; j -= gap; } arr[j + gap] = temp; } gap /= 2; } } // 冒泡排序 void bubbleSort(int arr[], int len) { for (int i = 0; i < len - 1; i++) { for (int j = 0; j < len - 1 - i; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } // 快速排序 int partition(int arr[], int left, int right) { int pivot = arr[left]; while (left < right) { while (left < right && arr[right] >= pivot) right--; arr[left] = arr[right]; while (left < right && arr[left] <= pivot) left++; arr[right] = arr[left]; } arr[left] = pivot; return left; } void quickSort(int arr[], int left, int right) { if (left < right) { int mid = partition(arr, left, right); quickSort(arr, left, mid - 1); quickSort(arr, mid + 1, right); } } // 直接选择排序 void selectSort(int arr[], int len) { for (int i = 0; i < len - 1; i++) { int minIndex = i; for (int j = i + 1; j < len; j++) { if (arr[j] < arr[minIndex]) { minIndex = j; } } int temp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = temp; } } // 堆排序 void maxHeapify(int arr[], int start, int end) { int dad = start; int son = dad * 2 + 1; while (son <= end) { if (son + 1 <= end && arr[son] < arr[son + 1]) { son++; } if (arr[dad] > arr[son]) { return; } else { int temp = arr[dad]; arr[dad] = arr[son]; arr[son] = temp; dad = son; son = dad * 2 + 1; } } } void heapSort(int arr[], int len) { for (int i = len / 2 - 1; i >= 0; i--) { maxHeapify(arr, i, len - 1); } for (int i = len - 1; i > 0; i--) { int temp = arr[0]; arr[0] = arr[i]; arr[i] = temp; maxHeapify(arr, 0, i - 1); } } int main() { srand(time(0)); // 用当前时间作为随机数种子 const int N = 10000; int arr[N]; for (int i = 0; i < N; i++) { arr[i] = rand(); // 生成随机数 } clock_t start, end; double duration; start = clock(); insertSort(arr, N); end = clock(); duration = (double)(end - start) / CLOCKS_PER_SEC; cout << "直接插入排序用时:" << duration << "秒" << endl; start = clock(); shellSort(arr, N); end = clock(); duration = (double)(end - start) / CLOCKS_PER_SEC; cout << "希尔排序用时:" << duration << "秒" << endl; start = clock(); bubbleSort(arr, N); end = clock(); duration = (double)(end - start) / CLOCKS_PER_SEC; cout << "冒泡排序用时:" << duration << "秒" << endl; start = clock(); quickSort(arr, 0, N - 1); end = clock(); duration = (double)(end - start) / CLOCKS_PER_SEC; cout << "快速排序用时:" << duration << "秒" << endl; start = clock(); selectSort(arr, N); end = clock(); duration = (double)(end - start) / CLOCKS_PER_SEC; cout << "直接选择排序用时:" << duration << "秒" << endl; start = clock(); heapSort(arr, N); end = clock(); duration = (double)(end - start) / CLOCKS_PER_SEC; cout << "堆排序用时:" << duration << "秒" << endl; return 0; } ``` 这个代码会生成一个长度为 10000 的随机数数组,并分别对其进行直接插入排序、希尔排序、冒泡排序、快速排序、直接选择排序堆排序,并输出每个排序算法所用的时间。你可以根据需要修改数据规模和排序算法

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值