选择排序Linux下c 实现 (二)

2014-11-24 10:40:05 · 作者: · 浏览: 1
gth);
return 0;
}
void showArr(const int *pArr, const int length)
{
int i;
for(i=0; i {
printf("%d ", *(pArr+i));
}
printf("\n");
}
void initRandomArr(int *pArr, const int length)
{
int i;
srand(time(NULL));
for(i=0; i< length; i++)
{
*(pArr+i)=rand()%1000;
}
}
5、编译程序:


[cpp]
[root@localhost selectSort]$ gcc -c main.c
[root@localhost selectSort]$ gcc -c selectSort.c
[root@localhost selectSort]$ gcc -o main main.o selectSort.o

[root@localhost selectSort]$ gcc -c main.c
[root@localhost selectSort]$ gcc -c selectSort.c
[root@localhost selectSort]$ gcc -o main main.o selectSort.o
执行可执行文件main,大致如下:


[cpp]
[root@localhost selectSort]$ ./main
Input array length:
8
Get random array :
906 968 161 208 757 885 370 691
select sort result:
161 208 370 691 757 885 906 968

[root@localhost selectSort]$ ./main
Input array length:
8
Get random array :
906 968 161 208 757 885 370 691
select sort result:
161 208 370 691 757 885 906 968
选择排序时间复杂度О(n ), 交换次数O(n),已经有序的最好情况下,交换0次;逆序的最坏情况下,交换n-1次。 交换次数比冒泡排序少多了,由于交换所需CPU时间比比较所需的CPU时间多,n值较小时,选择排序比冒泡排序快。