快速排序算法
#include<stdio.h>
void qSort(int *begin,int *end)
{
if(begin >= end)
{
return;
}
int t = *begin;
int *p =begin;
int *q =end;
while(p<q)
{
while(p<q && *q >=t)
{
--q;
}
while(p<q && *p <=t)
{
++p;
}
swap(p,q);
}
swap(begin,p);
qSort(begin,p-1);
qSort(p+1,end);
}
迭代器实现数组排序
#include<stdio.h>
void choicesort(int *begin,int *end)
{
int *p;
for(;begin < end;++begin)
{
for(p = begin+1 ; p<=end ;++p)
{
if(*begin>*p)
{
swap(begin,p);
}
}
}
}
动态内存分配:动态内存分配是指在程序运行时根据需要请求分配内存空间的过程。
malloc:用于分配指定大小的内存空间,并返回指向该空间的指针。如果分配失败,则返回NULL。
realloc:用于调整已分配内存的大小,可以增加或减少内存块的大小。
free:用于释放由malloc、calloc或realloc分配的内存空间。
动态内存分配的注意事项
使用动态内存分配时,需要注意以下几点:
1.避免内存泄漏:确保每次分配的内存在不再需要时都被释放。
2.防止野指针:释放内存后,应将指针设置为NULL,以防止野指针的出现。
3.注意内存越界:在操作动态分配的内存时,不得超出分配的范围。