mirror of
https://github.com/huihut/interview.git
synced 2025-12-18 13:04:38 +03:00
修改排序算法代码
This commit is contained in:
@@ -5,6 +5,8 @@ using namespace std;
|
|||||||
|
|
||||||
/*****************
|
/*****************
|
||||||
|
|
||||||
|
桶排序:将值为i的元素放入i号桶,最后依次把桶里的元素倒出来。
|
||||||
|
|
||||||
桶排序序思路:
|
桶排序序思路:
|
||||||
1. 设置一个定量的数组当作空桶子。
|
1. 设置一个定量的数组当作空桶子。
|
||||||
2. 寻访序列,并且把项目一个一个放到对应的桶子去。
|
2. 寻访序列,并且把项目一个一个放到对应的桶子去。
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
/*****************
|
/*****************
|
||||||
|
|
||||||
|
计数排序:统计小于等于该元素值的元素的个数i,于是该元素就放在目标数组的索引i位(i≥0)。
|
||||||
|
|
||||||
计数排序基于一个假设,待排序数列的所有数均出现在(0,k)的区间之内,如果k过大则会引起较大的空间复杂度
|
计数排序基于一个假设,待排序数列的所有数均出现在(0,k)的区间之内,如果k过大则会引起较大的空间复杂度
|
||||||
计数排序并非是一种基于比较的排序方法,它直接统计出键值本应该出现的位置
|
计数排序并非是一种基于比较的排序方法,它直接统计出键值本应该出现的位置
|
||||||
时间复杂度为O(n),空间复杂度为O(n+k)
|
时间复杂度为O(n),空间复杂度为O(n+k)
|
||||||
|
|||||||
@@ -2,6 +2,8 @@
|
|||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
// 堆排序:(最大堆,有序区)。从堆顶把根卸出来放在有序区之前,再恢复堆。
|
||||||
|
|
||||||
void max_heapify(int arr[], int start, int end) {
|
void max_heapify(int arr[], int start, int end) {
|
||||||
//建立父節點指標和子節點指標
|
//建立父節點指標和子節點指標
|
||||||
int dad = start;
|
int dad = start;
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
|
||||||
|
// 归并排序:把数据分为两段,从两段中逐个选最小的元素移入新数据段的末尾。可从上到下或从下到上进行。
|
||||||
|
|
||||||
/*****************
|
/*****************
|
||||||
迭代版
|
迭代版
|
||||||
*****************/
|
*****************/
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
/*****************
|
// 基数排序:一种多关键字的排序算法,可用桶排序实现。
|
||||||
基数排序
|
|
||||||
*****************/
|
|
||||||
|
|
||||||
int maxbit(int data[], int n) //辅助函数,求数据的最大位数
|
int maxbit(int data[], int n) //辅助函数,求数据的最大位数
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
// 希尔排序:每一轮按照事先决定的间隔进行插入排序,间隔会依次缩小,最后一次一定要是1。
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void shell_sort(T array[], int length) {
|
void shell_sort(T array[], int length) {
|
||||||
int h = 1;
|
int h = 1;
|
||||||
|
|||||||
Reference in New Issue
Block a user