【C++语言实现通用插入排序算法】

简介:

插入排序算法的C++通用版本,支持任何以迭代器(或普通数组首、尾指针)传入的数组的就地排序。

myutil.h

 

代码:
#pragma once
#ifndef _MY_UTIL_H
#define _MY_UTIL_H

#include<iterator>  //iterator_traits

//template function to display arrays with new line printed when finish.
template<class Iterator>
void displayArr(const Iterator &beg, const Iterator &end){
  typedef typename std::iterator_traits<Iterator>::value_type Ty;
  std::copy(beg, end, std::ostream_iterator<Ty>(std::cout, " "));
  std::cout<<std::endl;
}

#endif

 

insertion_sort.h

 

代码:

#pragma once
#ifndef _INSERT_SORT_H
#define _INSERT_SORT_H

#include<iterator>  //iterator_traits

template<class Iterator, class cmp>
void insertSort(const Iterator &beg, const Iterator &end){
  typedef typename std::iterator_traits<Iterator>::value_type val_t;
  Iterator step = beg;
  Iterator insertPos;
  Iterator beforeInsertPos;
  for(++step; step != end; ++step){
    val_t key = *step;
    insertPos = step;
    beforeInsertPos = insertPos;
    for(--beforeInsertPos; cmp()(*beforeInsertPos, key); --beforeInsertPos){
      *insertPos-- = *beforeInsertPos;
      if(beforeInsertPos == beg)
        break;
    }//end of for
    *insertPos = key;
  }// end of for
}// end of insertion sort

#endif

main.cpp

 

代码:
#include<iostream>
#include<string>
#include<vector>
#include<list>

#include"myutil.h"  //displayArr
#include"insertion_sort.h"  //insertSort

using std::cout;
using std::endl;
using std::vector;
using std::string;
using std::list;

int main(int argc, char **argv)
{
  int intArr[ ] = { 231, -547, -54, 54, 0, -1, 45, -54, 123, 9, -54 };
  int *intArrEnd = intArr + sizeof(intArr) / sizeof(*intArr);

  double doubleArr[ ] = { 0.0, -28.7, 465.2 };
  double *doubleArrEnd = doubleArr + sizeof(doubleArr) / sizeof(*doubleArr);

  string strArr[] = {"月夜幻影", "bill", "billhoo", "Alex", "alex", "Petter"};
  string *strArrEnd = strArr + sizeof(strArr) / sizeof(*strArr);

  vector<string> strVec(strArr, strArrEnd);
  list<string> strList(strArr, strArrEnd);

  cout<<"before insertion sort..."<<endl;
  displayArr<int*>(intArr, intArrEnd);
  displayArr<double*>(doubleArr, doubleArrEnd);
  displayArr<string*>(strArr, strArrEnd);
  displayArr(strVec.begin(), strVec.end());
  displayArr(strList.begin(), strList.end());

  //do insertion sort in ascending order
  insertSort<int*, std::greater<int>>(intArr, intArrEnd);
  insertSort<double*, std::greater<double>>(doubleArr, doubleArrEnd);
  insertSort<string*, std::greater<string>>(strArr, strArrEnd);
  insertSort<vector<string>::iterator, std::greater<string>>(strVec.begin(), strVec.end());
  insertSort<list<string>::iterator, std::greater<string>>(strList.begin(), strList.end());

  cout<<"\nascending order:"<<endl;
  displayArr<int*>(intArr, intArrEnd);
  displayArr<double*>(doubleArr, doubleArrEnd);
  displayArr<string*>(strArr, strArrEnd);
  displayArr(strVec.begin(), strVec.end());
  displayArr(strList.begin(), strList.end());

  //do insertion sort in descending order
  insertSort<int*, std::less<int>>(intArr, intArrEnd);
  insertSort<double*, std::less<double>>(doubleArr, doubleArrEnd);
  insertSort<string*, std::less<string>>(strArr, strArrEnd);
  insertSort<vector<string>::iterator, std::less<string>>(strVec.begin(), strVec.end());
  insertSort<list<string>::iterator, std::less<string>>(strList.begin(), strList.end());

  cout<<"\ndescending order:"<<endl;
  displayArr<int*>(intArr, intArrEnd);
  displayArr<double*>(doubleArr, doubleArrEnd);
  displayArr<string*>(strArr, strArrEnd);
  displayArr(strVec.begin(), strVec.end());
  displayArr(strList.begin(), strList.end());

  return EXIT_SUCCESS;
}

 

 



     本文转自Bill_Hoo 51CTO博客,原文链接:http://blog.51cto.com/billhoo/793581,如需转载请自行联系原作者








相关文章
|
8天前
|
存储 人工智能 算法
数据结构与算法细节篇之最短路径问题:Dijkstra和Floyd算法详细描述,java语言实现。
这篇文章详细介绍了Dijkstra和Floyd算法,这两种算法分别用于解决单源和多源最短路径问题,并且提供了Java语言的实现代码。
34 3
数据结构与算法细节篇之最短路径问题:Dijkstra和Floyd算法详细描述,java语言实现。
|
6天前
|
算法 C++
2022年第十三届蓝桥杯大赛C/C++语言B组省赛题解
2022年第十三届蓝桥杯大赛C/C++语言B组省赛题解
12 5
|
11天前
|
存储 算法 C++
高精度算法(加、减、乘、除,使用c++实现)
高精度算法(加、减、乘、除,使用c++实现)
145 0
高精度算法(加、减、乘、除,使用c++实现)
|
11天前
|
算法 搜索推荐
数据结构与算法学习十一:冒泡排序、选择排序、插入排序
本文介绍了冒泡排序、选择排序和插入排序三种基础排序算法的原理、实现代码和测试结果。
12 0
数据结构与算法学习十一:冒泡排序、选择排序、插入排序
|
8天前
|
算法 数据处理 C++
c++ STL划分算法;partition()、partition_copy()、stable_partition()、partition_point()详解
这些算法是C++ STL中处理和组织数据的强大工具,能够高效地实现复杂的数据处理逻辑。理解它们的差异和应用场景,将有助于编写更加高效和清晰的C++代码。
8 0
|
11天前
|
存储 编译器 C语言
深入计算机语言之C++:类与对象(上)
深入计算机语言之C++:类与对象(上)
|
11天前
|
存储 分布式计算 编译器
深入计算机语言之C++:C到C++的过度-2
深入计算机语言之C++:C到C++的过度-2
|
11天前
|
编译器 Linux C语言
深入计算机语言之C++:C到C++的过度-1
深入计算机语言之C++:C到C++的过度-1
|
15天前
|
搜索推荐 算法
【排序算法(一)】——插入排序,选择排序 —> 深层解析
【排序算法(一)】——插入排序,选择排序 —> 深层解析
|
16天前
|
算法 安全 Go
Python与Go语言中的哈希算法实现及对比分析
Python与Go语言中的哈希算法实现及对比分析
21 0