面试各种常见排序

1--归并排序 

2--快速排序 

3--插入排序 

4--堆排序 

5--计数排序 

6--基数排序 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

1--归并排序

/*

算法类型:归并算法

算法思路:分为两个函数,merge使用递归将数组分解并最终合并,mergeSort函数具体完成一定区间数组的排序

时间复杂度:n*logn

注意事项: int *p = new int [5] int *p = new int 5)不同,后者创建一个指向整数5的指针

遇到的问题:函数采用数组传递时,比较难调试,因为无法知道下一个元素是什么,也就无法知道数组的实时变化情况

*/

 

#include "stdafx.h"

 

void  mergeSort (int a[],int low,int mid,int high);

void  merge(int a[],int low, int high);

 

int _tmain(int argc, _TCHAR* argv[])

{

int data[14] = {2,1,8,9,6,7,11,35,2,2,1,3,5,2};

merge(data,0,sizeof(data)/sizeof(int)-1);

return 0;

}

 

void merge(int a[],int low,int high){

 

int mid = (low+high)/2;

if (low < high){

merge(a,low,mid);

    merge(a,mid+1,high);

mergeSort(a,low,mid,high);

}

}

 

void mergeSort(int a[],int low,int mid, int high){

int i = low;

int j = mid+1;

int k = 0;

//generate a temporary array aTemp

int *aTemp = new int [high-low+1];

 

//compare array[low,mid] and array[mid+1,high]

while(i<=mid&&j<=high){

           

if (a[i] < a[j])

              aTemp[k++] = a[i++];

else

  aTemp[k++] = a[j++];

}

//put the rest elements into aTemp

while(i<=mid)

            aTemp[k++] = a[i++];

while(j<=high)

            aTemp[k++] = a[j++];

//copy a wiht aTemp

for(int i = 0;i<high - low +1;i++)

a[i+low] = aTemp[i];

delete [] aTemp;

}

2--快速排序

/*

算法名称:快速排序

主要思路:

1Partition函数将数组a分割成三部分部分,前一部分都小于等于主元,后一部分都大于主元;

   函数返回主元所在索引。

2QuickSort通过递归对整个数组进行排序

时间复杂度:最坏:n*n,期望:n*log(n)

稳定性:

 

*/

#include "stdafx.h"

#include <algorithm>

using namespace std;

 

int   Partition(int a[],int low, int high);

void  QuickSort(int a[],int low, int high);

 

 

int _tmain(int argc, _TCHAR* argv[])

{

int data[15] = {1,3,9,11,3,4,3,5,6,7,12,11,3,4,10};

QuickSort(data,0,14);

return 0;

}

 

 

//after sorted, elements in left part of a[i] is no greater than a[i]

//elements in right part of a[i] is  greater than a[i].The function

//return i

 

//there is a better solution...,waitting me to complete it

int   Partition(int a[],int low, int high){

 

  int key = a[high];

  int i = low - 1;

  int j = low;

  for (;j<high;j++){

  if (a[j] <= key)  

  swap(a[++i],a[j]);//i increase and switch a[i] with a[j]

  }

  swap(a[++i],a[high]);

  return i;

}

//better one

static int partition(int A[], int start, int end, int pivotIndex){

    int i = start, j = end, pivot = A[pivotIndex];

    swap<int>(A[end], A[pivotIndex]);

    while(i < j){

        while(i < j && A[i] <= pivot) ++i;

        while(i < j && A[j] >= pivot) --j;

        if(i < j) swap<int>(A[i], A[j]);

    }

    swap<int>(A[end], A[i]);

    return i;

}

 

 

void  QuickSort(int a[],int low, int high){

 

if (low<high){

int mid = Partition(a,low,high);

QuickSort(a,low,mid-1);

QuickSort(a,mid+1,high);

}

}

 

3--插入排序

 

/*

算法名称:插入排序

主要思路:using {2,3,1,9,10,2,4,7,5} as a example

         index0  1  2  3  4  5  6  7  8  

   element2  3  1  9  10 2  4  7  5

when dataI is 1

                 2  3  3  9  10 2  4  7  5

 2  2  3  9  10 2  4  7  5

 1  3  3  9  10 2  4  7  5

时间复杂度:n^2

*/

 

#include "stdafx.h"

 

void  InsertSort(int *data,int n);

 

int _tmain()

{

int data[9] ={2,3,1,9,10,2,4,7,5};

InsertSort(data,sizeof(data)/sizeof(int));

return 0;

}

 

void  InsertSort(int *data,int n){

for(int i = 1;i<n;i++){ //begin from the second element

   int dataI = data[i];

   int j = i-1;

           for(;j>=0&&data[j]>dataI;j--){

   data[j+1] = data[j]; //copy data[j] to the place j+1;

   }

   data[j+1] = dataI;//insert dataI to the place j+1;

}

}

 

 

 

4--堆排序

/*

算法名称:堆排序

算法思路:

1、用HeapBuild函数来建堆,建堆时对所有的非叶子节点进行调整,<=n/2的节点都是非叶子节点,

注意数组中的元素从第二个开始有效,目的是第一个元素索引从1开始,有利于计算

2、使用HeapAdjust函数对第i个非叶子节点进行调整,再调整过程中使用递归来保证调整后的节点

也是满足最大堆特性。

3、使用HeapSort函数来完成排序,由于建立的是最大堆,所以,第一个元素就是最大元素,调整到

数组最后面,从新对1n-1元素建堆,重复以上操作。

时间复杂度:n*log(n)

稳定性:不稳定

 

 

*/

#include "stdafx.h"

#include  <algorithm>

using namespace std;

void HeapBuild(int a[],int size);

void HeapAdjust(int a[],int i,int size);

void HeapSort(int a[],int size);

 

  

 

int _tmain(int argc, _TCHAR* argv[])

{

int data[16] = {0,3,8,7,5,10,17,11,2,1,2,9,12,3,8,20};//the elements needed to be sorted begin from a[1]

HeapSort(data,15);

return 0;

}

//create the heap

void HeapBuild(int a[],int size){

 

for(int i = size/2;i>=1;i--)

HeapAdjust(a,i,size);

}

 

//adjust the heap

void HeapAdjust(int a[],int i,int size){

 

int lChild = 2*i;

int rChild = 2*i+1;

int max =i;

 

if(i<=size/2){//leaf nodes needn't to be adjusted

if (lChild<=size && a[lChild]>a[max])

max = lChild;

if(rChild<=size && a[rChild]>a[max])

max = rChild;

        

 if (max != i){//readjust  

 swap(a[max],a[i]);

 HeapAdjust(a,max,size);

 }

}

}

 

 

void HeapSort(int a[],int size){

 

HeapBuild(a,size);//build a heap first time

for(int i = size;i>=2;i--){

swap(a[i],a[1]);//exchange the biggest element a[1] whih last element

HeapAdjust(a,1,i-1);//build a heap for a[1] to a[i]

}

}

 

5--计数排序

 

#include "stdafx.h"

 

/*

算法名称:计数排序

主要思路:

时间复杂度:n

稳定性:

条件:假定数据范围是0-100

*/

 

 

void  CountSort(int a[],int length);//The function will change the a itself

void  CountSort(int a[],int b[],int length);//The sorted array will be saved in b

int _tmain(int argc, _TCHAR* argv[])

{

int data[30] ={9,7,3,1,3,7,6,0,3,7,

               0,0,1,2,3,3,2,8,2,9,

               2,2,9,10,10,2,3,1,3,1};

//CountSort(data,30);

int b[30];

CountSort(data,b,30);

return 0;

}

 

void  CountSort(int a[],int length){

 

int  count[11]={0};

//Sanner the array a and count the times of repeation of each elements

for(int i = 0;i<length;i++)

count[a[i]]++;

//change content of a to complete sorting

int index = 0;

for (int i=0;i<11;i++)

while(count[i]-->0)

a[index++] = i;

}

 

void  CountSort(int a[],int b[],int length){

 

int  count[11]={0};

//Sanner the array a and count the times of repeation of each elements

for(int i = 0;i<length;i++)

count[a[i]]++;

//change content of a to complete sorting

int index = 0;

for (int i=0;i<11;i++)

while(count[i]-->0)

b[index++] = i;

}

 

 

 

6--基数排序

//参考网站:http://blog.csdn.net/cjf_iceking/article/details/7943609

/*

算法名称:基数排序

算法思路:分别对个位、十位、百位...排序

最后一次排序后,整个数组就完成了排序

算法复杂度:d*(n+radix)

其中d是关键码的个数,radix是关键码的范围

*/

#include "stdafx.h"

#include  <cstdlib>

using namespace std;

 

 int GetNumInPos(int num,int pos);//get the number of num in special position

 void RadixSort(int a[],int length);//complete the whole sort

 const int KEYNUM = 4;// numbers of key num code

 const int RADIX = 10;//up level of key num code

int _tmain(int argc, _TCHAR* argv[])

{

    

int data[20] = {1232,22,1243,1567,1240,3,8922,909,33,233,  

                1567,23, 9,  2342, 898,11,4545,4567,222,1239};

RadixSort(data,20);

return 0;

}

 

int GetNumInPos(int num,int pos){

int temp = 1;

for(int i = 0;i<pos-1;i++)

temp *= 10;

return num/temp%10;

}

void RadixSort(int a[],int length){

     

//allocata space for radixArray

int *radixArrays[RADIX];

for(int i = 0;i<RADIX;i++){

radixArrays[i] = (int *)malloc(sizeof(int) * (length + 1));  

radixArrays[i][0] = 0;//fisrt space is used to save numbers of this kind

}

 

for(int i = 1;i<=KEYNUM;i++){

//sanner the a by radix

 

for (int j = 0;j<length;j++){

int num = GetNumInPos(a[j],i);

radixArrays[num][++radixArrays[num][0]] = a[j];

}

//get elements from radixArrays

int index = 0;

for (int k = 0;k<RADIX;k++){

for(int p = 1;p<=radixArrays[k][0];p++)

a[index++] = radixArrays[k][p];                 

  

radixArrays[k][0] = 0;//reset

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值