c++排序系列之插入排序
来源:互联网 发布:淘宝免费推广 编辑:程序博客网 时间:2024/06/05 21:58
1.前言
自从大一学过一点点的c++之后,后面一直用java,未得机会去训练c++。
有时候觉得java不够带感,偶然想写一个简单的c/c++排序,都不会写!
尚未晚矣,从头开始。。。
准备先用c++把所有的排序实现一遍。
本节来看最简单粗暴的插入排序。
2.插入排序(insertion sort)
2.1简单介绍
插入排序,通过将所有的元素执行一遍插入之后即完成排序。在每一次的插入,前面插入的元素已经排好序,新来的元素则要跟已排好序的元素进行比较,找到它的位置。之后,将位置以及后面的元素后移一位,将新来的元素插入合适的位置。
2.2代码实现
原理简单,下面用c++实现:
.h:声明定义插入排序Insert_Sorting模板类
#ifndef INSERTSORTING_H#define INSERTSORTING_H//template <class Type> class Insert_Sorting{//template classprivate:Type *dataArray;//dynamic data arrayint maxNum;//max array sizeint current;//actual array size less than maxpublic:Insert_Sorting();Insert_Sorting(int num);~Insert_Sorting();//int compare(const Type element1, const Type element2);////模板只能用于定义数据类型时用,因此对于过程,模板不能代替。因此对于不同类型的文件的读取,分开写了。。。 void read_data_INT (const char * fileName);//read INT data from filevoid read_data_DOUBLE (const char * fileName);//DOUBLEvoid read_data_STR (const char * fileName);//STRINGvoid sort ();//insertion sortvoid display ();//display};#endif
.cpp:对模板类的成员函数的实现
#include "insertSorting.h"#include "stdlib.h"#include <iostream>#include <fstream>using namespace std;template <class Type> Insert_Sorting<Type>::Insert_Sorting(){maxNum = 100;current = 0;dataArray = NULL;}template <class Type> Insert_Sorting<Type>::Insert_Sorting(int num){if(num >0){maxNum = num;//dataArray = new Type[maxNum];//cout<<"successfully!"<<endl;}else{maxNum = 0;cerr<<"error!"<<endl;}}template <class Type> Insert_Sorting<Type>::~Insert_Sorting(){delete[] dataArray;}template <class Type> int Insert_Sorting<Type>::compare(const Type element1, const Type element2){if(element1 > element2)return 1;else if(element1 < element2)return -1;elsereturn 0;}//template <class Type> void Insert_Sorting<Type>::read_data_INT(const char * fileName){cout<<"initialize the input data:"<<endl;//read from txt fileifstream infile(fileName, ios::in); if(!infile){cerr<<"error: unable to open input file : "<<fileName<<endl;}else{cout<<"read file by line"<<endl;int in;const int LINE_LENGTH = 100;char inStr[LINE_LENGTH] = {0};while(infile.getline(inStr, LINE_LENGTH)){//cout<<inStr<<endl;//字符串转换为整型intin = atoi(inStr);//cout<<"input int : "<<in<<endl;if(current < maxNum){dataArray[current] = in;current++;}}}infile.clear();infile.close();}template <class Type> void Insert_Sorting<Type>::read_data_DOUBLE(const char * fileName){cout<<"initialize the input data:"<<endl;//read from txt fileifstream infile(fileName, ios::in); if(!infile){cerr<<"error: unable to open input file : "<<fileName<<endl;}else{cout<<"read file by line"<<endl;double in;const int LINE_LENGTH = 100;char inStr[LINE_LENGTH] = {0};while(infile.getline(inStr, LINE_LENGTH)){//cout<<inStr<<endl;//字符串转换为浮点数double in = atof(inStr);//cout<<"input double : "<<in<<endl;if(current < maxNum){dataArray[current] = in;current++;}}}}template <class Type> void Insert_Sorting<Type>::read_data_STR(const char * fileName){cout<<"initialize the input data:"<<endl;//read from txt fileifstream infile(fileName, ios::in); if(!infile){cerr<<"error: unable to open input file : "<<fileName<<endl;}else{cout<<"read file by line"<<endl;string in;const int LINE_LENGTH = 100;char inStr[LINE_LENGTH] = {0};while(infile.getline(inStr, LINE_LENGTH)){//cout<<inStr<<endl;//字符串转换为string in = inStr;//cout<<"input string : "<<in<<endl;if(current < maxNum){dataArray[current] = in;current++;}}}}template <class Type> void Insert_Sorting<Type>::sort(){cout<<"sort the input data:"<<endl;Type * tempData = new Type[current];//insert sortint tempSize = 0;for(int i=0; i<current; i++){Type temp = dataArray[i];if(tempSize == 0)tempData[0] = temp;else{int j=0;while(j<tempSize && compare(temp, tempData[j]) >0){j++;}if(j==tempSize)tempData[j] = temp;else{for(int k=tempSize; k>j; k--){tempData[k] = tempData[k-1];}tempData[j] = temp;}}tempSize++;for(int index=0; index<tempSize; index++)cout<<tempData[index]<<" ";cout<<endl;}//storefor(int i=0; i<current; i++){dataArray[i] = tempData[i];}}template <class Type> void Insert_Sorting<Type>::display(){cout<<"display the sorted data:"<<endl;for(int i=0; i<current; i++){cout<<dataArray[i]<<" ";}cout<<endl;}
main函数:测试插入排序
#include <iostream>#include <string>#include "insertSorting.h"#include "insertSorting.cpp"using namespace std;/* run this program using the console pauser or add your own getch, system("pause") or input loop */int main(int argc, char** argv) {//intInsert_Sorting<int> Insert_Sorting_INT(100);const char * filename1 = "E://int_data.txt";Insert_Sorting_INT.read_data_INT(filename1);Insert_Sorting_INT.display();Insert_Sorting_INT.sort();Insert_Sorting_INT.display();cout<<"-------------------------------------"<<endl;//doubleInsert_Sorting<double> Insert_Sorting_DOUBLE(100);const char * filename2 = "E://double_data.txt";Insert_Sorting_DOUBLE.read_data_DOUBLE(filename2);Insert_Sorting_DOUBLE.display();Insert_Sorting_DOUBLE.sort();Insert_Sorting_DOUBLE.display();cout<<"-------------------------------------"<<endl;//stringInsert_Sorting<string> Insert_Sorting_STR(100);const char * filename3 = "E://string_data.txt";Insert_Sorting_STR.read_data_STR(filename3);Insert_Sorting_STR.display();Insert_Sorting_STR.sort();Insert_Sorting_STR.display();cout<<"-------------------------------------"<<endl;return 0;}
2.3结果分析
时间复杂度:
best:O(N)
worst:O(N^2)
average:O(N^2)
左侧是原始数据,右侧是执行insertion sort过程:
int类型:
double类型:
string类型:
3.注意:
GNU C++下,模板类main编译时不仅要包含.h头文件,也要包含.cpp文件,否则会报错undefined reference to,GNU c++对模板类不能进行分离式编译。
4.总结:
虽然简单实现了整个任务,但是对其中的很多的问题还未理解清楚,诸如c/c++读取文件的不同方式,模板类、模板函数等。
只有反复coding,琢磨其中的原理。
路漫漫修远兮。
0 0
- C之插入排序
- 排序系列之直接插入排序
- 排序算法系列之插入排序
- 排序系列之--直接插入排序
- c++排序系列之插入排序
- 基本排序系列之直接插入排序
- 排序算法系列之插入排序
- C排序算法之插入排序
- 排序算法之插入排序 (C语言)
- C语言之排序算法---插入排序
- C--经典算法之排序---插入排序
- c之排序问题---插入排序详解
- 算法之排序算法:插入排序(C++)
- c语言排序算法之插入排序
- C语言::简单排序之插入排序
- C语言排序之插入排序篇
- C语言之插入排序
- C语言之插入排序
- java 加密解密
- 【Go web开发之revel+mgo】第9章 其它和??
- MainActivity 访问 fragment_main.xml 中的组件 空指针
- 点击不在这个区域部分,div消失,jquery代码 - hiding an element on click of anything else on the page
- 2014-04-26 求和为N+1(微软面试题)
- c++排序系列之插入排序
- 关于java中String的一点理解
- OpenCV实现Gabor滤波(2)
- codeigniter源代码分析之CI系统运行流程 CodeIgniter.php
- CC_6_智力题
- HDU 1559 最大子矩阵
- A20-AT070TNA2配置
- [计算机基础]ARM-Linux系统调用
- 背包问题