- 论坛徽章:
- 0
|
挑战数组腾挪大法
samhoo的解答。
一)算法说明
left = 0; right = n - 1;
1)比较L[left]和X, 如果小于X, left加1, 重复本步骤; 如果等于X跳到步骤2; 如果大于X, 跳到步骤3.
2)比较L[left]以右的数, 如果小于X,交换之, left加1, 重复本步骤; 如果等于X, left加1, 重复本步骤; 如果大于X, 跳到步骤3.
3)做法与步骤1相似, right向左移动.
4)做法与步骤2相似, right向左移动.
5)交换L[left], L[right];跳到步骤1)
上述步骤都会对边界进行检查。
二)编译器/运行平台,机器类型
cc/sco OpenServer 5.05, intel 1.2GMhz
三)Partition函数代码
/*
* L:输入数组
* n:数组大小
* X:X元素
* 返回X在数组中的下标
*/
int Partition(long *L, int n, long X)
{
int left = 0, left_ep = 0, right = n -1, right_ep = n - 1;
/* 大数:大于X的数; 小数:小于X的数 */
while(left < right){
/* 找到左边用于交换的大数 */
while( (L[left] <= X) && (left < right) ){
if (L[left] < X){ /* 如果是小数,向右移动 */
left++;
left_ep++;
}
else{
while((L[left] == X) && (left <= right)) left++;
if (left >; right)
break;
SWAP(L, left, left_ep);
if (L[left] < X){
left++;
left_ep++;
}
else
left = left_ep;
}
}
/* 找到右边用于交换的小数 */
while( (L[right] >;= X) && (left < right) ){
if (L[right] >; X){ /* 如果是大数,向左移动 */
right--;
right_ep--;
}
else{
while((L[right] == X) && (left <= right)) right--;
if (left >; right) break;
SWAP(L, right, right_ep);
if (L[right] >; X){
right--;
right_ep--;
}
else
right = right_ep;
}
}
/* 交换 */
if (left < right){
SWAP(L, left, right);
left++;
left_ep++;
right--;
right_ep--;
}
}
return left_ep;
}
long *CreateList(int n)
/* Allocates memory for an array of n longs */
{
long *L;
L = (long *) malloc( n * sizeof(long) );
if( L == NULL) { /* Exit program if memory cannot be allocated */
printf("\nCannot allocate enough memory for list.\n" ;
exit(0);
}
return (L);
}
long *MakeRandomList(int *num)
{
long *L; /* pointer to List */
unsigned int seed; /* seed for random number generator */
int i; /* index of for loop */
printf("\nNumber of elements to sort=>;" ;
scanf("%d",num);
printf("Seed for random number generator (integer)=>;" ;
scanf("%d",&seed);
srand(seed);
/* Allocate memory for # of elements. If memory cannot be allocated,
display message and terminate program. Read in the elements. */
L = CreateList(*num);
if (L == NULL) {
printf("\nCannot allocate enough memory for number list.\n" ;
exit(0);
}
for (i = 0; i < *num; ++i)
L = rand();
return(L); /* Return the List */
}
int Check(long *L, int num, int X, int position)
{
int i, lpos = -1, rpos = -1;
for (i = 0; i < num; i++)
if (L == X){
lpos = i;
break;
}
else if (L >; X)
return -1;
for (i = num - 1; i >;= 0; i--)
if (L == X){
rpos = i;
break;
}
else if (L < X)
return -1;
if ((lpos == -1) || (rpos == -1) || (position < lpos) || (position >;rpos))
return -1;
for (i = lpos; i <=rpos; i++)
if (L != X)
return -1;
return 0;
}
int SWAP(long *L, int a, int b)
{
long temp;
temp = L[a];
L[a] = L;
L = temp;
return 0;
}
四)至少给出前4组测试数据的结果
i )L[]={5,5,5,5,5,5,7,1,6,8}; n=10; X=5; (在main函数提供,注释掉其中一些语句即可)
X=5
L[]=5 5 5 5 5 5 7 1 6 8
-----------------------------------------------------------------
Check :correct.
Time : 0.000 seconds.
-----------------------------------------------------------------
position=1
L[]=1 5 5 5 5 5 5 7 6 8
ii )n=10的随机数组
Number of elements to sort=>;10
Seed for random number generator (integer)=>;1
X=31051
L[]=16838 5758 10113 17515 31051 5627 23010 7419 16212 4086
-----------------------------------------------------------------
Check :correct.
Time : 0.000 seconds.
-----------------------------------------------------------------
position=9
L[]=16838 5758 10113 17515 5627 23010 7419 16212 4086 31051
iii)n=10,000,000的随机数组
Number of elements to sort=>;10000000
Seed for random number generator (integer)=>;1
X=5500
L[]=16838 5758 10113 17515 31051 5627 23010 7419 16212 4086 2749 12767 9084 1206
0 32225 17543 25089 21183 25137 25566 26966 ...(too much number, ignore)
-----------------------------------------------------------------
Check :correct.
Time : 2.440 seconds.
-----------------------------------------------------------------
position=1678846
L[]=2266 2127 2110 1762 626 1023 4566 2197 572 4086 2749 4060 1120 2206 2836 293
5 3375 3800 3392 4703 2069 ...(too much number, ignore)
iv )n=50,000,000随机数组
Number of elements to sort=>;50000000
Seed for random number generator (integer)=>;1
X=16413
L[]=16838 5758 10113 17515 31051 5627 23010 7419 16212 4086 2749 12767 9084 1206
0 32225 17543 25089 21183 25137 25566 26966 ...(too much number, ignore)
-----------------------------------------------------------------
Check :correct.
Time : 38.350 seconds.
-----------------------------------------------------------------
position=25052130
L[]=3827 5758 10113 7067 1970 5627 10035 7419 16212 4086 2749 12767 9084 12060 6
42 4478 5374 2400 10405 7411 10487 ...(too much number, ignore)
v )其他自己认为具有“边界”特性的数组。
none |
|