直接运行:
退出代码-1073740940 (0xC0000374)
调试出来:
退出代码0
这是什么原因?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
typedef struct SL
{
int *elem;
int TableLen;
}SSTable;
//随机数的生成
void ST_Init(SSTable &ST,int len)
{
ST.TableLen=len;
ST.elem= (int*)malloc(sizeof (SSTable));
int i;
srand(time(NULL));
for (i=0;i<ST.TableLen;i++)
{
ST.elem[i]=rand()%100;
}
}
void ST_print(SSTable ST)
{
for(int i=0;i<ST.TableLen;i++)
{
printf("%5d",ST.elem[i]);
}
printf("\n");
}
//两个数的交换
//交换时要取地址
void swap(int &a,int &b)
{
int temp;
temp=a;
a=b;
b=temp;
}
//大根堆
void LargeRootPile(int A[],int j,int len)
{
int dad=j;
int son=2*dad+1;
while (son<len)//!容易出错
{
if (son+1<len&&A[son]<A[son+1])
{
son++;
}
if (A[son]>A[dad])
{
swap(A[son],A[dad]);
dad=son;
son=2*dad+1;
}else
{
break;
}
}
}
//先将A变成大根堆
void HeapSort(int A[],int len)
{
int i;
//使得A成为大根堆
for(i=len/2-1;i>=0;i--)//i取0的时候还是有的
{
LargeRootPile(A,i,len);
}
//A成为大根堆后就要根头与叶尾进行交换
swap(A[0],A[len-1]);
//根头与叶尾进行交换后,继续进行变成大根堆,继续交换;
//
for(i=len-1;i>1;i--)
{
LargeRootPile(A,0,i);//大根堆长度减1了,不会影响后面交换数时,把交换过的又交换
//这时候是从头根进行大根堆,不是从最后一个父亲根进行大根堆;
swap(A[0],A[i-1]);//进行交换时,这里的要进行大根堆总数长度已经改变了
}
}
int main() {
SSTable ST;
ST_Init(ST,10);
//将A固定,还不是随机数去用
int A[10]={ 3, 87, 2, 93, 78, 56, 61, 38, 12, 40 };
memcpy(ST.elem,A,sizeof (A));//头文件是#include <string.h>
ST_print(ST);
HeapSort(ST.elem,10);
ST_print(ST);
return 0;
}