插入排序法(InsertSort)c++实现
来源:互联网 发布:儿童涂鸦画画软件 编辑:程序博客网 时间:2024/05/19 14:02
插入排序法就是把无序区的数据一个一个在有序区里找到自己的位置。
这就好比军训时,教官首先让最左边的人站着不动,然后从左边第二个人开始,如果他比第一个人高就不动,否则就排在第一个人的左边。接着看第三个人,把他与前两个人比,然后找到自己的位置,依次下去,直到最后一个人找到自己的位置。
c++实现:
InsetSort.h文件:
/**************************************************************/
class InsertSort
{
const int arraySize;
float *array;
public:
InsertSort(int arraySize);
~InsertSort();
void Initial();
void Sort();
void Show();
};
/***************************************************************/
InsertSort.cpp文件:
/***************************************************************/
#include <iostream>
#include "InsertSort.h"
using namespace std;
InsertSort::InsertSort(int size):arraySize(size)
{
}
void InsertSort::Initial()
{
array= new float[arraySize];
cout<<"please enter "<<arraySize<<" elements!"<<endl;
for(int i=0;i<arraySize;i++)
{
cin>>array[i];
}
cout<<"entered!"<<endl;
}
void InsertSort::Sort()
{
for(int j=1;j<arraySize;j++)
{
float temp=array[j];
int i=j-1;
while(temp<array[i])
{
array[i+1]=array[i];
i--;
}
array[i+1]=temp;
}
}
void InsertSort::Show()
{
for(int i=0;i<arraySize;i++)
{
cout<<array[i]<<" ";
}
}
InsertSort::~InsertSort()
{
delete [] array;
}
/**********************************************************/
main函数文件:
/*********************************************************/
#include <iostream>
#include "InsertSort.h"
using namespace std;
int main()
{
cout<<"enter the size of array!"<<endl;
int size;
cin>>size;
InsertSort test(size);
test.Initial();
test.Sort();
test.Show();
return 0;
}
/***********************************************************/
- 插入排序法(InsertSort)c++实现
- 插入排序法(InsertSort)c++实现 ZT
- 插入排序(InsertSort)
- 插入排序(InsertSort)
- 插入排序(InsertSort)
- 插入排序算法(InsertSort)
- 插入排序(insertSort.cpp)
- 数据结构--插入排序(InsertSort)思想与实现
- 插入排序之直接插入(InsertSort)
- 插入排序(Insertsort)之Java实现
- 插入排序(Insertsort)之Java实现
- 内部排序之插入排序(InsertSort)
- 插入排序InsertSort
- insertSort - 插入排序
- 插入排序 InsertSort
- 插入排序 InsertSort
- 插入排序InsertSort
- 插入排序-InsertSort
- sql 执行顺序
- 二级域名隐藏转向
- patsubst notdir wildcard 详解
- 学习php+wap2.0
- foreach 能够做for不能做的事情
- 插入排序法(InsertSort)c++实现
- 用php开发wap2.0的基本格式是什么?
- 关于"回文数"的编程实验(三)
- 1℃ love (1℃的爱情)
- Oracle 建立表空间和用户权限
- php显示时间代码
- 不能读取 AppletViewer 的属性文件解决
- Linux服务器SYN攻击防御方法概述
- 找不到网络连接(本地连接)的解决方法