# 随机化一维数组

1. 构造两个数组，一个辅助数组，用于哈希，另一个用于保留优先级。例如，输入一组数据2,1,0

2. 按照优先级，对原数组进行插入排序。

```/*************************************************************************
> File Name: randomArray.c
> Author: ma6174
> Mail: [email protected]
> Created Time: Mon 25 Aug 2014 09:44:03 PM CST
************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#define random(m) (rand() % m)
#define N 10
int main() {
srand((int)time(0));   // set the seed
int array[N];          // input array
int assist_array[N];
int prority[N];       // indicate prority of every element in the array
int e;
int i, j;
int tmp, tmp1;
for(i = 0;i < N;i++) {
assist_array[i] = -1;   // initialize
}
for(i = 0;i < N;i++) {
scanf("%d", &array[i]);
}
for(i = 0;i < N;i++) {
e = random(N);
if(assist_array[e] == -1) {   // if the e is only one
assist_array[e] = e;
prority[i] = e;        // put in the prority
}
else {
i--;                 // game is continuing
}
}
/*
* insert sort in terms of prority
* */
for(i = 1;i < N;i++) {
tmp = prority[i];
tmp1 = array[i];
j = i - 1;
while(j >= 0 && prority[j] > tmp) {
array[j + 1] = array[j];
j--;
}
array[j + 1] = tmp1;
}
printf("random the array is:\n");
for(i = 0;i < N;i++) {
printf("%d\n", array[i]);
}

return 0;
}
```

(0)
(0)

0条