#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ARRAYNUM 10
int *SortarrayCreate(int num)
{
int *ptr=(int *)calloc(num,sizeof(int));
if(!ptr)
{
printf("calloc failed!\n");
exit(EXIT_FAILURE);
}
for(int i=0;i<num;i++)
{
srand(rand());
*(ptr+i)=rand();
}
return ptr;
}
void sort(int *ptr,int num)
{
int tmp=0;
for(int a=1;a<num;++a)
{
for(int b=num-1;b>=a;--b)
{
if(*(ptr+(b-1))>*(ptr+b))
{
tmp=*(ptr+(b-1));
*(ptr+(b-1))=*(ptr+b);
*(ptr+b)=tmp;
}
}
}
}
int main(int argc,char **argv)
{
int *ptr=NULL;
time_t start,end;
ptr=SortarrayCreate(ARRAYNUM);
printf("The rand array before sort as follows:\n");
for(int i=0;i<ARRAYNUM;i++)
{
printf("%d ",*(ptr+i));
}
printf("\n\n\nAfter sort the array element as follows:\n");
sort(ptr,ARRAYNUM);
for(int i=0;i<ARRAYNUM;i++)
{
printf("%d ",*(ptr+i));
}
free(ptr);
}
#include <stdlib.h>
#include <time.h>
#define ARRAYNUM 10
int *SortarrayCreate(int num)
{
int *ptr=(int *)calloc(num,sizeof(int));
if(!ptr)
{
printf("calloc failed!\n");
exit(EXIT_FAILURE);
}
for(int i=0;i<num;i++)
{
srand(rand());
*(ptr+i)=rand();
}
return ptr;
}
void sort(int *ptr,int num)
{
int tmp=0;
for(int a=1;a<num;++a)
{
for(int b=num-1;b>=a;--b)
{
if(*(ptr+(b-1))>*(ptr+b))
{
tmp=*(ptr+(b-1));
*(ptr+(b-1))=*(ptr+b);
*(ptr+b)=tmp;
}
}
}
}
int main(int argc,char **argv)
{
int *ptr=NULL;
time_t start,end;
ptr=SortarrayCreate(ARRAYNUM);
printf("The rand array before sort as follows:\n");
for(int i=0;i<ARRAYNUM;i++)
{
printf("%d ",*(ptr+i));
}
printf("\n\n\nAfter sort the array element as follows:\n");
sort(ptr,ARRAYNUM);
for(int i=0;i<ARRAYNUM;i++)
{
printf("%d ",*(ptr+i));
}
free(ptr);
}