做题前先设计测试案例
上下对应
-
input:
{3,2,1} {0,0,0,0}
-
output:
{1,2,3} {0,0,0,0}
搭建测试框架
#include <iostream>
#include <ctime>
void test(void(*sort)(int *arr),int *arr,int length){
clock_t startTime,endTime;
startTime=clock();
sort(arr);
endTime=clock();
std::cout<<(double)(endTime-startTime)/CLOCKS_PER_SEC<<std::endl;
}
void sort(int *arr,int length){//todo}
int main(){
int arr[]={3,2,1};
int length=sizeof(arr)/sizeof(*arr);
test(sort,arr,length);
return 0;
}
排序方法 | 时间复杂度(平均) | 时间复杂度(最坏) | 时间复杂度(最好) | 空间复杂度 | 稳定性 |
---|---|---|---|---|---|
快速排序 | |||||
冒泡排序 | |||||
插入排序 | |||||
希尔排序 | |||||
堆排序 | |||||
归并排序 | |||||
计数排序 | |||||
桶排序 |
快速排序
int partition(int *arr,int l,int r)
{
int mid = rand()%(r-l+1)+l;
swap(arr, mid,l);
int i = l + 1;
while(i<=r)
if (arr[i] > arr[l])swap(arr, i, r--);
else i++;
swap(arr, l, r);
return r;
}
// in: arr[l,r]
void quick_sort(int* arr,int l,int r)
{
if (l >= r)return;
int mid = partition(arr, l, r);
quick_sort(arr, l, mid - 1);
quick_sort(arr, mid + 1,r);
}
冒泡排序
void bubble_sort(int* arr,int l,int r)
{
for (int i = r;i >= l;--i)
for (int j = l;j < i;++j)
if (arr[j] > arr[j+1])
swap(arr, j, j+1);
}
插入排序
void insert_sort(int* arr,int l,int r)
{
// 维护[l,i]的有序
for (int i = l + 1;i <= r;++i)
for (int j = i;j > l;--j)
if (arr[j] < arr[j - 1])
swap(arr, j, j - 1);
else
break;
}
希尔排序
选择排序
void select_sort(int* arr,int l,int r)
{
for (int i = l;i <= r;++i) {
int minIndex = i;
for (int j = i;j <= r;++j)
if (arr[j] < arr[minIndex])
minIndex = j;
swap(arr, i, minIndex);
}
}
堆排序
class MinHeap
{
public:
int capacity;
int *heap;
int count;
MinHeap(unsigned int capacity) :capacity(capacity) { heap = new int[capacity]; count = 0; }
~MinHeap() { delete[] heap; }
void push(int num)
{
if (count >= capacity)return;//todo throw exception;
heap[count++] = num;
shift_up();
}
int pop()
{
if (count == 0)return NULL;//todo throw exception;
swap(heap, 0, --count);
int res = heap[count];
shift_down();
return res;
}
private:
void shift_up()
{
int index = count - 1;
while(index!=0)
{
int parent=(index - 1) / 2;
if (heap[index] < heap[parent])
swap(heap, index, parent);
else
break;
index = parent;
}
}
void shift_down()
{
int index = 0;
while(index<count)
{
int l = index * 2 + 1;
int r = index * 2 + 2;
if (r <= count-1 &&heap[r]<heap[index]&&heap[l] > heap[r]) {
swap(heap, index, r);
index = r;
}else if(l<=count-1&&heap[l]<heap[index])
{
swap(heap, index, l);
index = l;
}
else
break;
}
}
};
void heap_sort(int* arr,int l,int r)
{
MinHeap *heap=new MinHeap(r-l+1);
for (int i = l;i <= r;++i)
heap->push(arr[i]);
for (int i = l;i <= r;++i)
arr[i] = heap->pop();
}
归并排序
int temp[10000];
void merge_sort(int * arr,int l,int r)
{
if (r - l <= 1)return;
int mid = l + (r - l) / 2;
merge_sort(arr, l, mid);
merge_sort(arr, mid + 1, r);
int index = 0,i=l,j=mid+1;
while(i<=mid||j<=r)
if ( j>r||arr[i] < arr[j + 1])
temp[index++] = arr[i++];
else
temp[index++] = arr[j++];
for (int i = l;i <= r;++i)
arr[i] = temp[r - l];
}
计数排序
void bucket_sort(int* arr,int l,int r)
{
int bucket[100]{0};//数组内的数据大小处于[0,100)
for (int i = l;i <= r;++i)
bucket[arr[i]]++;
for(int i=0;i<=100;++i)
while(bucket[i]!=0)
{
bucket[i]--;
arr[l++] = i;
}
}