常用的排序算法:插入排序,希尔排序,冒泡排序,选择排序,快速排序,归并排序
2013-04-23 16:14
811 查看
昨晚准备今天的腾讯实习生的面试,就把常用的排序算法又写了一遍,放这儿吧,以后没事看看。
Sort.h
InsertSort.cpp
ShellSort.cpp
BubbleSort.cpp
SelectSort.cpp
QuickSort.cpp
MergeSort.cpp
main.cpp
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; }
相关文章推荐
- 【Java】八个常用的排序算法:插入排序、冒泡排序、选择排序、希尔排序 、快速排序、归并排序、堆排序和LST基数排序
- 常用的排序算法(快速排序、插入排序、希尔排序、堆排序、冒泡排序、选择排序、归并排序)
- 排序算法: 冒泡排序, 快速排序,希尔排序,直接插入排序 ,直接选择排序,归并排序,堆排序
- python排序算法-冒泡排序,选择排序,直接插入排序,希尔排序,归并排序,快速排序,堆排序
- 算法分析中最常用的几种排序算法(插入排序、希尔排序、冒泡排序、选择排序、快速排序,归并排序)C 语言版
- 几种常用的排序算法的分析及java实现(希尔排序,堆排序,归并排序,快速排序,选择排序,插入排序,冒泡排序)
- 元素排序几种常用的排序算法的分析及java实现(希尔排序,堆排序,归并排序,快速排序,选择排序,插入排序,冒泡排序)
- 几种常用的排序算法的分析及java实现(希尔排序,堆排序,归并排序,快速排序,选择排序,插入排序,冒泡排序)
- 各种排序算法总结----基数排序、归并排序、插入排序、冒泡排序、选择排序、快速排序、堆排序、希尔排序
- 插入排序、冒泡排序、选择排序与希尔排序、快速排序、归并排序
- 冒泡排序 快速排序 选择排序 堆排序 直接插入排序 希尔排序 归并排序
- C++实现八个常用的排序算法:插入排序、冒泡排序、选择排序、希尔排序等
- 排序算法汇总(选择排序 ,直接插入排序,冒泡排序,希尔排序,快速排序,堆排序)
- 几种常见的排序算法,选择排序,冒泡排序,希尔排序,堆排序,快速排序,归并排序,基数排序的比较
- 常见排序集合(冒泡排序,选择排序,直接插入排序,二分插入排序,快速排序,希尔排序,归并排序)
- Java实现八个常用的排序算法:插入排序、冒泡排序、选择排序、希尔排序等
- 数据结构与算法:七种排序算法总结(冒泡排序、选择排序、直接插入排序、希尔排序、堆排序、归并排序、快速排序)
- 比较排序总结——直接插入排序,希尔排序,选择排序,堆排序,冒泡排序,快速排序,归并排序
- 冒泡排序、插入排序、选择排序、希尔排序、堆排序、归并排序等常用排序算法的比较
- 十二.C语言8种排序算法及其实现 1.希尔排序 2.二分插入法 3.直接插入法 4.带哨兵的直接排序法 5.冒泡排序 6.选择排序 7.快速排序 8.堆排序