#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <limits.h>
#include <string.h>
#define MAX_CHARACTERS 100
void InsertionSorting(int *ipArray, int iCount)
{
for(int i = 1; i < iCount; i++)
{
int iTemp = *(ipArray + i);
for(int j = i - 1; j >= 0; j--)
{
if(*(ipArray + j) > iTemp)
{
*(ipArray + j + 1) = *(ipArray + j);
}
else
{
break;
}
}
*(ipArray + j + 1) = iTemp;
}
}
int main()
{
unsigned int iDemension;
char *cpRawString = (char *)malloc(sizeof(char)*MAX_CHARACTERS);
memset(cpRawString, '\0', MAX_CHARACTERS);
printf("Please input the demension of the array:");
scanf("%d", &iDemension);
//gets(cpRawString);
//iDemension = atoi(cpRawString);
while(iDemension > UINT_MAX || iDemension <= 0)
{
printf("Sorry, input error! Please try again.\n");
printf("Please input the demension of the array:");
scanf("%d", &iDemension);
//gets(cpRawString);
//iDemension = atoi(cpRawString);
}
int *ipArray = (int *)malloc(sizeof(int)*iDemension);
srand((unsigned)time( NULL ));
for(int i =0; i < iDemension; i++)
{
ipArray[i] = rand()%1000;
}
//Output the raw data.
for(i =0; i < iDemension; i++)
{
printf("%4d\n", ipArray[i]);
}
InsertionSorting(ipArray, iDemension);
printf("After insertion sorting:\n");
for(i = 0; i < iDemension; i++)
{
printf("%d ", ipArray[i]);
}
printf("\n");
free(ipArray);
free(cpRawString);
system("pause");
return 0;
}
Insertion Sorting
最新推荐文章于 2024-02-17 21:16:53 发布