一个成功修改的代码:(c代码)参考一个很重要的web:http://www.diybl.com/course/3_program/c++/cppjs/20090403/164014.html
核心问题在于用new分配结构体数组
http://it.huij.net/it/kaifayuyan/C_C__/19990715/12220.html
#include<stdio.h>
#include<iostream.h>
#include<iomanip.h>
#include<stdlib.h>
#include<time.h>
#include<malloc.h>
#define MAXI 11
typedef int KeyType;
typedef int ElemType;
typedef struct {
KeyType key;
ElemType data;
}RecordNode;
typedef struct {
int n;
RecordNode *record;
}SortObject;
void insertsort (SortObject *pvector)
{
int i,j;
RecordNode temp;
for(i=1;i<pvector->n;i++)
{
temp=pvector->record[i];j=i-1;
while ((temp.key<pvector->record[j].key)&&(j>=0))
{
pvector->record[j+1]=pvector->record[j];j--;
}
if(j!=(i-1)) pvector->record[j+1]=temp;
}
}
insertsort算法的深入分析:
int main()
{
SortObject a;
a.n=MAXI;
int i,j,k;
srand(time(0));
a.record= new RecordNode[MAXI]; //最关键就在这里,一开始是出现warning,运行程式出错,后来发现是没有申请内存,但任然搞不清为谁申请,设法给a,用malloc申请,SortObject a=()malloc(sizeof()); 后来,发觉得为a.record申请,于是就修改typedef struct {
KeyType key;
ElemType data;
}RecordNode,*pu; 添加指向RecordNode的指针*pu,但仍有问题.
for( i=1;i<a.n;i++)
{ //曾经在这里添加代码a.record=()malloc(sizeof()),程式运行后,出现一些奇观的情况
a.record[i].key=rand()%80;
a.record[i].data=rand()%100;
}
cout<<"排序前数组:\n";
for( j=1;j<a.n;j++)
{cout<<setw(6)<<a.record[j].key;}
cout<<"\n正在调用insertsort\n"<<endl;
insertsort(&a);
cout<<"运行insertsort的结果如下:\n"<<endl;
for( k = 1; k < a.n; k++)
printf("%d ", a.record[k].key);
getchar();
free( a.record); //事实上,c++不是用free,而是用
return 0;
}
做了一个小调整,出现错误:
LNK1168: cannot open Debug/insertsort1.exe for writing
Error executing link.exe.
就是上面那个窗口没有关闭,添加一段代码,消除这个问题.
/* Note:Your choice is C IDE */
//插入排序法charufa3.cpp
#include<iostream.h>
#include<iomanip.h>
#include<stdlib.h>
#include<time.h>
#define MAXI 11
typedef int KeyType;
typedef int ElemType;
struct rec {
KeyType key;
ElemType data;};
typedef rec sqlist[MAXI];
void insertsort(sqlist b,int n)
{int i,j,k;
for(i=2;i<n;i++)
{b[0]=b[i];
j=i-1;
while(b[0].key<b[j].key)
{b[j+1]=b[j];j--;}
b[j+1]=b[0];
for(k=1;k<n;k++)
cout<<setw(4)<<b[k].key;
cout<<endl;}}
insertsort算法的深入分析:
如果恰好是a[i-2]=a[i]的话,
那么,while(temp.key<a[j])不成立了.
于是,还是有考虑到这个问题的.
void main()
{cout<<"charufa3.cpp运行结果:\n";
sqlist a;int i,n=MAXI;
srand(time(0));
for(i=1;i<n;i++)
{a[i].key=rand()%80;
a[i].data=rand()%100;}
cout<<"排序前数组:\n";
for(i=1;i<n;i++)
cout<<setw(4)<<a[i].key;
cout<<endl;
cout<<"数组排序过程演示:\n";
insertsort(a,n);
cout<<"排序后数组:\n";
for(i=1;i<n;i++)
cout<<setw(4)<<a[i].key;
cout<<endl;cin.get();}
结合两道程式的综合程式:
#include<stdio.h>
#include<iostream.h>
#include<iomanip.h>
#include<stdlib.h>
#include<time.h>
#include<malloc.h>
#define MAXI 11
typedef int KeyType;
typedef int ElemType;
typedef struct {
KeyType key;
ElemType data;
}RecordNode;
typedef struct {
int n;
RecordNode *record;
}SortObject;
void insertsort (SortObject *pvector,int n)
{
int i,j;
cout<<"\n正在调用insertsort\n"<<endl;
RecordNode temp;
for(i=1;i<pvector->n;i++)
{
temp=pvector->record[i];j=i-1;
while ((temp.key<pvector->record[j].key)&&(i>=0))
{
pvector->record[j+1]=pvector->record[j];j--;
}
if(j!=(i-1)) pvector->record[j+1]=temp;
for(int k=1;k<n;k++)
cout<<setw(4)<<pvector->record[k].key;
cout<<endl;
}
}
int main()
{
SortObject a;
a.n=MAXI;
int i,j,k;
srand(time(0));
a.record= new RecordNode[MAXI];
for( i=1;i<a.n;i++)
{//a.record[i] = (pu)malloc( sizeof(RecordNode));
a.record[i].key=rand()%80;
a.record[i].data=rand()%100;
}
cout<<"排序前数组:\n";
for( j=1;j<a.n;j++)
{cout<<setw(6)<<a.record[j].key;}
insertsort(&a,a.n);
cout<<"运行insertsort的结果如下:\n"<<endl;
for( k = 1; k < a.n; k++)
printf("%d ", a.record[k].key);
//getchar();
//free( a.record);
delete (a.record);
cout<<"\n程式执行完毕\n"<<endl;
return 0;
}