《数据结构与算法分析(c描述》—— 快速排序

news/2024/5/19 23:51:36 标签: 排序算法, 快速排序, 算法

快速排序">1. 快速排序

快速排序是最快的已知算法>排序算法,平均运行时间为 O(NlogN) ,最坏情况的性能为 O(N^2)。

将数组 S 快速排序由下列简单的四步组成:

  1. 如果 S 中元素个素是0或1,则返回
  2. 取 S 中任一元素作为枢纽元
  3. 将 S - {v} (S 中其余元素)分成两个不相交的集合,S1 中元素小于 v,S2 中元素大于 v
  4. 对 S1、S2递归调用

c 代码实现如下:

void Qsort(int a[], int low, int high)
{
    if(low >= high)
    {
        return;
    }
    int first = low;
    int last = high;
    int key = a[first];/*用字表的第一个记录作为枢轴*/

    while(first < last)
    {
        while(first < last && a[last] >= key)
        {
            --last;
        }

        a[first] = a[last];/*将比第一个小的移到低端*/

        while(first < last && a[first] <= key)
        {
            ++first;
        }

        a[last] = a[first];    
        /*将比第一个大的移到高端*/
    }
    a[first] = key;/*枢轴记录到位*/
    Qsort(a, low, first-1);
    Qsort(a, first+1, high);
}

2. 快速选择

快速排序一个典型的变形应用是用在选择问题(select problem)上,即从一个数组中选择第 k 小元素。

快速算法选择的步骤如下:

  1. 如果 S 中元素个素是0或1,则返回
  2. 取 S 中任一元素作为枢纽元
  3. 将 S - {v} (S 中其余元素)分成两个不相交的集合,S1 中元素小于 v,S2 中元素大于 v
  4. 设|S1|的值为 cur,如果 k <= cur, 递归调用 qSelect(a, cur - k, low, k - 1)
  5. 否则递归调用 qSelect(a, k - cur, k + 1, high)

c 代码实现:

void qSelect(int a[], int k, int low, int high) {
    if (low >= high)
        return;
    int first = low;
    int last = high;
    int key = a[low];
    while(first < last) {
        while(first < last && a[last] >= key)
            last--;
        a[first] = a[last];
        while(first < last && a[first] <= key)
            first++;
        a[last] = a[first];
    }
    a[first] = key;
    if (k <= first)
        qSelect(a, first - k, low, first - 1);
    else if(k > first)
        qSelect(a, k - first, first + 1, high);
}

int main() {    
    int a[10] = {9, 2, 8, 3, 4, 1, 5, 18, 11, 14};
    qSelect(a, N, 0, 9);
    printf("qselect : %d\n", a[N - 1]);
    for (int i = 0; i < 10 ; i++)
        printf("%d#", a[i]);
    printf("\n");
    return 0;
}

“`


http://www.niftyadmin.cn/n/941463.html

相关文章

[leetcode Q20] Valid Parentheses

题目要求检查括号字符串序列是否匹配 简单的算法是使用一个栈&#xff1a; 做一个空栈&#xff0c;顺序扫描字符串直到末尾如果读到开放符号 “([{” 则入栈否则读到 “)]}”&#xff0c;检查栈是否为空&#xff0c;空则报错栈不空则检查栈顶元素是否与字符匹配扫描结束&…

[leetcode Q26] Remove Duplicates from Sorted Array

1. 题目要求 Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example, Given input arr…

[leetcode Q28] Implement strStr()

实现 c 函数 strstr() Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 返回子字符串在原字符串中第一次匹配的位置&#xff0c;否则放回 -1 实现还是比较简单的就是需要注意一些细节 思路…

[leetcode Q29] Divide Two Integers

1. 题目 Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. 要求不使用乘除法实现两个整数相除运算。 2. 思路 不使用乘法除法&#xff0c;要想快数得到结果就只能考虑位运算。本题主要考察位运算。 被除…

[leetcode Q33] Search in Rotated Sorted Array

1. 问题 Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no dupl…

[leetcode Q34Q35] Search for a RangeSearch Insert Position

Q34 和 Q35都是使用二分查找的题目&#xff0c;所以放在一起。 1. 问题 Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm’s runtime complexity must be in the order of O(log n). If the target is not…

C/C++研发实习生要求

岗位描述Job Description 如果你对基础技术感兴趣&#xff0c;你可以参与基础软件的设计、开发和维护&#xff0c;如分布式文件系统、缓存系统、Key/Value存储系统、数据库、Linux操作系统等&#xff1b; 如果你热衷于高性能分布式技术&#xff0c;你可以参与世界顶级规模的分…

[leetcode Q36] valid sudoku

1. 题目 检查 9x9 宫格中的行列&#xff0c;子宫格&#xff08;3x3&#xff09;是否有重复的数字出现。 标签为 hash table 2. 思路 简单的思路是使用三次循环&#xff0c;第一次检查每行是否有效&#xff0c;第二次检查每列是否有效&#xff0c;第三次检查每个子宫格是否有…