待排序数据表的类定义 ifndef datalist h #define datalist h Include <iostream.> const int Defaultsize=100; template <class Type> class datalist i template <class Type> class Element t private: Type keys ∥关键码 field otherdata;∥其它数据成员 public: Element(: keyO), otherdata( NULD
#ifndef DATALIST_H #define DATALIST_H #include <iostream.h> const int DefaultSize = 100; template <class Type> class datalist { template <class Type> class Element { private: Type key; //关键码 field otherdata; //其它数据成员 public: Element ( ) : key(0), otherdata(NULL) { }
Type getKey(){ return key;}∥提取关键码 void setKey( const Type x){key=x;}∥修改 Element<Type> operator ∥赋值 Element<Type> &x)i this=x;) int operator=( Type &x) /Athis=x freturn! this <x< this ) B int operator != Type &x) //this!=x f return this <xx< this; 3 int operator<=(Type&x)判this≤x freturn! this>x;3 int operator>=(Type&x)判this≥x freturn! this <x);) int operator <( Type &x) ∥.this<x f return this>x,)
Type getKey ( ) { return key; }//提取关键码 void setKey ( const Type x ) { key = x; } //修改 Element<Type> & operator = //赋值 ( Element<Type> & x ) { this = x; } int operator == ( Type & x ) //判this == x { return ! ( this < x || x < this ); } int operator != ( Type & x ) //判this != x { return this < x || x < this; } int operator <= ( Type & x ) //判this x { return ! ( this > x ); } int operator >= ( Type & x ) //判this x { return ! ( this < x ); } int operator < ( Type & x ) //判this < x { return this > x; }
template <class Type> class datalist i public datalist( int maxsz= Defaultsize):∥构造函数 Max Size( maxsz ) CurrentSize(0) i vector=new Element <Type> [MaxSz; 3 void swap( Element<Type>&x,∥对换 Element <Type> &y) f Element<Type> temp=x;x=y; y=temp; 3 private Element<Type>* ector;∥存储向量 int max size, Currentsize;/最大与当前个数
} template <class Type> class datalist { public: datalist ( int MaxSz = DefaultSize ) : //构造函数 MaxSize ( Maxsz ), CurrentSize (0) { Vector = new Element <Type> [MaxSz]; } void swap ( Element <Type> & x, //对换 Element <Type> & y ) { Element<Type> temp = x; x = y; y = temp; } private: Element <Type> * Vector; //存储向量 int MaxSize, CurrentSize; //最大与当前个数 }
插入排序( nsert Sorting) 插入排序的基本方法是:每步将一个待排序的对 象,按其关键码大小,插入到前面已经排好序的 组对象的适当位置上,直到对象全部插入为止。 直接插入排序( nsert Sort 直接插入排序的基本思想是:当插入第i(≥1) 个对象时,前面的0,V1,…,v1已经排好 序。这时,用v的关键码与v1,v2,…的 关键码顺序进行比较,找到插入位置即将叫插 入,原来位置上的对象向后顺移
2125/49 2345 果 i=1 49 21 2516 25 2345 temp i=2 2125 25116 49 08 012345 tem
各趟排序结果 0 1 2 3 4 5 0 1 2 3 4 5 temp 25 0 1 2 3 4 5 temp 49