您的位置:首页 > 其它

常用的排序算法:插入排序,希尔排序,冒泡排序,选择排序,快速排序,归并排序

2013-04-23 16:14 811 查看
昨晚准备今天的腾讯实习生的面试,就把常用的排序算法又写了一遍,放这儿吧,以后没事看看。

Sort.h

#ifndef SORT_H
#define SORT_H

void insertSort(int x[], int n);
void shellSort(int x[], int n);

void bubbleSort(int x[], int n);

void selectSort(int x[], int n);

void quickSort(int x[], int low, int high);

void mergeSort(int x[], int low, int high);
#endif


InsertSort.cpp

#include "Sort.h"
#include <iostream>
/******************************************
/* <FUNC>
/* 函数名   : InsertSort
/* 功能     : 直接插入排序
/* 参数     : int x[],   待排序列
/*            int n,     序列中元素个数
/* 返回值   : -
/* </FUNC>
*******************************************/
void insertSort(int x[], int n)
{
for(int i = 1;i<n;++i)
{
int tmp = x[i];
int j = i-1;
while(j>=0 && tmp < x[j])
{
x[j+1] = x[j];
j--;
}
x[j+1]=tmp;

//output
std::cout<<"第"<<i+1<<"趟排序后的结果: ";
for(int k=0; k<n; k++)
std::cout<<x[k]<<" ";
std::cout<<std::endl;
}

}


ShellSort.cpp

#include "Sort.h"
#include <iostream>
void ShellSort(int x[], int n)
{
int hop = n/2;
while(hop>0)
{
for(int k = 0; k< hop; ++k)
{
for(int i = k + hop; i < n; i += hop)
{
int j = i - hop;
int tmp = x[i];
while(j>=0 && x[j]>tmp)
{
x[j+hop] = x[j];
j -= hop;
}
x[j+hop] = tmp;
}
}
hop /= 2;
}
}


BubbleSort.cpp

#include "Sort.h"
#include <iostream>

/*******************************************
/* <FUNC>
/* 函数名   : BubbleSort
/* </FUNC>
********************************************/
void bubbleSort(int x[], int n)
{
for(int i = 0; i< n-1; ++i)
{
bool IsExchanged =false;
for(int j = 0; j< n-1-i; ++j)// take care of j, very important!
{
if(x[j] > x[j+1])
{
int tmp = x[j+1];
x[j+1] = x[j];
x[j] = tmp;
IsExchanged = true;
}
}

//output
std::cout<<"第"<<i+1<<"趟排序后的结果: ";
for(int k=0; k<n; k++)
std::cout<<x[k]<<" ";
std::cout<<std::endl;

if(!IsExchanged)
break;
}
}


SelectSort.cpp

#include "Sort.h"
#include <iostream>

/****************************************
/* <FUNC>
/* 函数名   : SelectSort
/* </FUNC>
*****************************************/
int selectmin(int x[], int low, int high);
void selectSort(int x[], int n)
{
for(int i=0; i<n; ++i)
{
int pos = selectmin(x,i,n-1);
if(pos != i)
{
int tmp = x[i];
x[i] = x[pos];
x[pos] = tmp;
}

//output
std::cout<<"第"<<i+1<<"趟排序后的结果: ";
for(int k=0; k<n; k++)
std::cout<<x[k]<<" ";
std::cout<<std::endl;
}
}

int selectmin(int x[], int low, int high)
{
int pos = low;
for(int i =low; i<= high; ++i)
if(x[i]<x[pos])
pos =i;
return pos;
}


QuickSort.cpp

#include "Sort.h"
#include <iostream>

/**************************************
/* <FUNC>
/* 函数名   : QuickSort
/* </FUNC>
***************************************/
int Partition(int x[], int low, int high);
void quickSort(int x[], int low, int high)
{
int loc;
if(low < high)
{
loc = Partition(x,low,high);
quickSort(x,low,loc-1);
quickSort(x,loc+1,high);
}
}

int Partition(int x[], int low, int high)
{
int elem = x[low];
int begin = low;
int end = high;
while(begin < end)
{
while(x[end]>=elem && end > begin) --end;
x[begin]=x[end];

while(x[begin]<=elem && end > begin) ++begin;
x[end] = x[begin];
}
x[begin]= elem;
return begin;
}


MergeSort.cpp

#include "Sort.h"
#include <iostream>

/**************************************
/* <FUNC>
/* 函数名   : MergeSort
/* </FUNC>
***************************************/
void merge(int x[],int low, int mid, int high);
void mergeSort(int x[], int low, int high)
{
if(low < high)
{
int mid = (low+high)/2;
mergeSort(x,low,mid);
mergeSort(x,mid+1,high);
merge(x,low,mid,high);
}
}

void merge(int x[],int low, int mid, int high)
{
int begin1 = low;
int begin2 = mid+1;
int *tmp=new int[high-low+1];
int index=0;
while(begin1 <= mid && begin2 <= high)
{
if(x[begin1] <= x[begin2])
tmp[index++] = x[begin1++];
else
tmp[index++] = x[begin2++];
}
while(begin1 <= mid)
tmp[index++] = x[begin1++];
while(begin2 <= high)
tmp[index++] = x[begin2++];

for(int begin = low; begin <= high; ++begin)
{
x[begin] = tmp[begin-low];
}
delete []tmp;
}


main.cpp

#include <iostream>
#include "Sort.h"
using namespace std;

int main(int argc, char* argv[])
{
//InsertSort
int x1[10] = {10, 5, 17, 23, 17, 18, -1, 3, 12, 5};
printf("InsertSort: \n");
insertSort(x1, 10);

//BubbleSort
int x2[10] = {10, 5, 17, 23, 17, 18, -1, 3, 12, 5};
printf("\nBubbleSort: \n");
bubbleSort(x2, 10);

//SelectSort
int x3[10] = {10, 5, 17, 23, 17, 18, -1, 3, 12, 5};
printf("\nSelectSort: \n");
selectSort(x3, 10);

//QuickSort
int x4[10] = {10, 5, 17, 23, 17, 18, -1, 3, 12, 5};
printf("\nQuickSort: \n");
quickSort(x4, 0, 9);

for(int k=0; k<10; k++)
std::cout<<x4[k]<<" ";
cout<<std::endl;

//MergeSort
int x5[10] = {10, 5, 17, 23, 17, 18, -1, 3, 12, 5};
printf("\nMergeSort: \n");
mergeSort(x5, 0, 9);

for(int k=0; k<10; k++)
std::cout<<x5[k]<<" ";
cout<<std::endl;

int x8[12] = {-1, 10, 5, 17, 23, 17, 18, -1, 3, 12, 5, 88};
ShellSort(x8,12);
for(int k=0; k<12; k++)
std::cout<<x8[k]<<" ";
cout<<std::endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐