常见排序代码(C语言)
Posted huatian5
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了常见排序代码(C语言)相关的知识,希望对你有一定的参考价值。
选快希堆不稳(是不稳定的排序),
堆归选基不变(运行时间不发生变化,与初始状态无关)
插入排序
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* sortArray(int* nums, int numsSize, int* returnSize)
for (int i = 1, j; i < numsSize; i++)
int temp = nums[i];
for (j = i - 1; j >= 0 && temp < nums[j]; j--)
nums[j+1] = nums[j];
nums[j+1] = temp;
*returnSize = numsSize;
return nums;
选择排序
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* sortArray(int* nums, int numsSize, int* returnSize)
for (int i = 0, j; i < numsSize; i++)
int min_pos = i;
for (int j = i+1; j < numsSize; j++)
if (nums[j] < nums[min_pos])
min_pos = j;
int temp = nums[i];
nums[i] = nums[min_pos];
nums[min_pos] = temp;
*returnSize = numsSize;
return nums;
冒泡排序(优化可在第一层循环内检查是否发生交换,所以运行时间与初始状态有关)
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* sortArray(int* nums, int numsSize, int* returnSize)
for (int i = 0, j; i < numsSize; i++)
for (int j = 0; j < numsSize - 1 - i; j++)
if (nums[j] > nums[j+1])
int temp = nums[j];
nums[j] = nums[j+1];
nums[j+1] = temp;
*returnSize = numsSize;
return nums;
希尔排序
基数排序
快速排序
最理想时间复杂度O(nlogn),最坏时间复杂度O(n^2),实际应用中,快速排序的平均时间复杂度为O(nlogn)
#include<iostream>
#include<algorithm>
using namespace std;
int a[105] = 3,5,7,9,2,68,48,6,59,18;
void quicksort(int left,int right)
if(left > right)//递归终止条件
return;
int i = left;
int j = right;
while(i < j)
while(i < j && a[j] >= a[left])
j--;
while(i < j && a[i] <= a[left])
i++;
if(i < j)
swap(a[i],a[j]);
swap(a[left],a[i]);//基准归位
quicksort(left,i-1);
quicksort(i+1,right);
int main()
quicksort(0,9);
for(int i = 0;i < 10;i++)
cout<<a[i]<<endl;
寻找第k小元素
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <utility>
#include <vector>
using namespace std;
int a[105] = 3,5,7,9,2,68,48,6,59,18;
int findkth(int l,int r,int k)
if(l > r)
return -1;
int i = l, j = r;
while(i < j)
while(i < j && a[j] >= a[l])
j--;
while(i < j && a[i] <= a[l])
i++;
if(i < j)
swap(a[i],a[j]);
swap(a[i],a[l]);
if(i == k-1)
return a[i];
else if(i > k-1)
return findkth(l,i-1,k);
else
return findkth(i+1,r,k);
int main()
printf("%d\\n",findkth(0,6,3));
归并排序
时间复杂度O(nlogn)
#include<iostream>
#include<algorithm>
using namespace std;
int a[105] = 3,5,7,9,2,68,48,6,59,18,b[105];
void mergesort(int *a,int left,int right,int *T)
if(right - left > 1)
int mid = left + (right-left) / 2;
int i = left,j = mid,k = left;
mergesort(a,left,mid,T);
mergesort(a,mid,right,T);
while(i < mid || j < right)
if(j >= right || (i < mid && a[i] <= a[j]))
T[k++] = a[i++];
else
T[k++] = a[j++];
for(int i = left;i < right;i++)
a[i] = T[i];
int main()
mergesort(a,0,10,b);
for(int i = 0;i < 10;i++)
cout<<a[i]<<endl;
#include<iostream>
#include<algorithm>
using namespace std;
int a[105] = 3,5,7,9,2,68,48,6,59,18,b[105];
void mergesort(int *a,int left,int right,int *T)
if(left >= right)
return;
int mid = left + (right-left) / 2;
int i = left,j = mid+1,k = left;
mergesort(a,left,mid,T);
mergesort(a,mid+1,right,T);
while(i <= mid && j <= right)
if(a[i] <= a[j])
T[k++] = a[i++];
else
T[k++] = a[j++];
while(i <= mid) T[k++] = a[i++];
while(j <= right) T[k++] = a[j++];
for(int i = left;i <= right;i++)
a[i] = T[i];
int main()
mergesort(a,0,9,b);
for(int i = 0;i < 10;i++)
cout<<a[i]<<endl;
堆排序
#include<bits/stdc++.h>
using namespace std;
int a[105] = 3,5,7,9,2,68,48,6,59,18;
void adjust(int len,int index)
int left = index*2+1;
int right = index*2+2;
int maxn = index;//最大数的下标
if(left < len && a[left] > a[maxn])
maxn = left;
if(right < len && a[right] > a[maxn])
maxn = right;
if(maxn != index)//有更新
swap(a[maxn],a[index]);
adjust(len,maxn);
void heap_sort(int n)//最大堆
for(int i = n/2 - 1;i >= 0;i--)//对每一个非也叶节点进行堆调整
adjust(n,i);
for(int i = n-1;i >= 1;i--)
swap(a[0],a[i]);//将当前最大的放到数组末尾
adjust(i,0);//对未排序的部分继续堆排序
int main()
heap_sort(10);
for(int i = 0;i < 10;i++)
cout<<a[i]<<endl;
以上是关于常见排序代码(C语言)的主要内容,如果未能解决你的问题,请参考以下文章
8种面试经典!排序详解--选择,插入,希尔,冒泡,堆排,3种快排,快排非递归,归并,归并非递归,计数(图+C语言代码+时间复杂度)
8种面试经典排序详解--选择,插入,希尔,冒泡,堆排,3种快排及非递归,归并及非递归,计数(图+C语言代码+时间复杂度)