数组
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 5
int main()
{
int array[N];
int i,j,min,temp,k;
srand((unsigned)time(NULL));
for(i = 0;i < N;i++)
{
array[i] = rand() % 100;
}
printf("The original array is :");
for(i = 0;i < N;i++)
{
printf("%5d",array[i]);
}
printf("\n");
for(i = 0;i < N-1;i++)
{
min = i;
for(j = i + 1;j < N;j++)
{
if(array[j] < array[min])
{
min = j;
}
}
if(min != i)
{
temp = array[i];
array[i] = array[min];
array[min] = temp;
}
printf(" The %dth order is :",i + 1);
for(k = 0;k < N;k++)
{
printf("%5d",array[k]);
}
printf("\n");
}
printf(" The final array is :");
for(i = 0;i < N;i++)
{
printf("%5d",array[i]);
}
printf("\n");
return 0;
}
The original array is : 74 67 60 81 14
The 1th order is : 14 67 60 81 74
The 2th order is : 14 60 67 81 74
The 3th order is : 14 60 67 81 74
The 4th order is : 14 60 67 74 81
The final array is : 14 60 67 74 81
指针
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 5
int main()
{
int a[N];
int *p = a,*q,*min;
int temp,i;
srand((unsigned)time(NULL));
for(i = 0;i < N;i++)
{
a[i] = rand() % 100;
}
for(p = a;p < a + N - 1;p++)
{
min = p;
for(q = p + 1;q < a + N;q++)
{
if(*q < *p)
{
min = q;
}
}
if(min != p)
{
temp = *min;
*min = *p;
*p = temp;
}
}
for(i = 0;i < N;i++)
{
printf("%5d",a[i]);
}
printf("\n");
return 0;
}