练习1;
0-1999的随机数1000个,已知无重复,求最快排序算法;
答案:
利用数组下标;
1-> 定义一个长度是2000的数组arr,清0;
2-> 遍历1000个随机数,arr[随机数] = 1;
3-> 打印值是1的数组下标就ok;
练习2:
一个文件中包含n个正整数,其中n=10的7次方;已知数据不重复;
请编写程序,将文件中的数字按升序排序;
根据练习1的思路,但是为节省空间,每个数只用1个bit;
#define NUM 10000000
申请的数组为a[1 + NUM/32];
参考代码如下
gcc generate.c -o generate
gcc sort.c -o sort
然后分别运行generate可以生成10000000个数字,sort可以完成排序
算法是没有固定的,就好比是说飞机快还是汽车快;
算法要靠平时慢慢的积累;
0-1999的随机数1000个,已知无重复,求最快排序算法;
答案:
利用数组下标;
1-> 定义一个长度是2000的数组arr,清0;
2-> 遍历1000个随机数,arr[随机数] = 1;
3-> 打印值是1的数组下标就ok;
练习2:
一个文件中包含n个正整数,其中n=10的7次方;已知数据不重复;
请编写程序,将文件中的数字按升序排序;
根据练习1的思路,但是为节省空间,每个数只用1个bit;
#define NUM 10000000
申请的数组为a[1 + NUM/32];
参考代码如下
/*
* generate.c
*/
/* Include Files */
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <time.h>
#include <stdlib.h>
/* MACROS */
#define NUM 10000000
#define SHIFT 5
#define MASK 0x1F
/* CONSTANTS AND VARIABLES */
int a[1 + NUM/32] = { 0 }; /* define an arry and initialize every member */
/* INTERNAL FUNCTIONS */
void setbit(int n) {
/* set flag */
a[n >> SHIFT] |= 1 << (n & MASK); /* a[n/32] |= 1<<(n%32) */
}
void cl_bit(int n) {
/* clear flag */
a[n >> SHIFT] &= ~(1 << (n & MASK)); /* a[n/32] |= 1<<(n%32) */
}
int testbit(int n) {
/* if the n bit exist, resturn 1 */
return a[n >> SHIFT] & (1 << ( n & MASK));
}
int test_all_bit() {
int i, res;
for(i = 0; i <= NUM/32; i++) {
if (a[i]){
return a[i];
}
}
return a[i];
}
int list_bit(int n) {
int i = 0, j = 0;
for (i = 0; i <= NUM - 1; i++) {
if (testbit(i)){
if (j == n) {
return i;
}
j++;
}
}
return i;
}
/* IMPLEMENTATION */
int main() {
/* create file data.db to save number */
int fd1 = open("data.db", O_RDWR | O_CREAT | O_TRUNC, 0664);
if (-1 == fd1) {
perror("open");
return -1;
}
/* read number of the file, and set flags */
int i, res;
for (i = 0; i <= NUM - 1; i++) {
setbit(i); /* set flags */
}
srand(time(0));
int n = NUM;
int node;
printf("starting to generate datas...\n");
while (test_all_bit()) {
if (n < 1000 ){
node = rand() % n;
i = list_bit(node);
} else {
i = rand() % NUM;
}
if (testbit(i)) {
write(fd1, &i, sizeof(int));
printf("%d\r", i);
cl_bit(i);
n--;
}
}
printf("generated ok!\n");
close(fd1);
return 0;
}
/*
* sort.c
*/
/* Include Files */
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <time.h>
/* MACROS */
#define NUM 10000000
#define SHIFT 5
#define MASK 0x1F
/* CONSTANTS AND VARIABLES */
int a[1 + NUM/32] = { 0 }; /* define an arry and initialize every element */
/* INTERNAL FUNCTIONS */
void setbit(int n) {
/* find the array member of bit n, and set flag */
if (n > NUM){
printf("too large\n");
return ;
}
a[n >> SHIFT] |= 1 << (n & MASK); /* a[n/32] |= 1<<(n%32) */
}
int testbit(int n) {
/* if the n bit exist, resturn 1 */
return a[n >> SHIFT] & (1 << ( n & MASK));
}
void show_time() {
time_t t = time(NULL);
struct tm *mytm = localtime(&t);
printf("%04d年%02d月%02d日 %02d:%02d:%02d t=%d\t",
mytm->tm_year+1900, mytm->tm_mon+1, mytm->tm_mday,
mytm->tm_hour, mytm->tm_min, mytm->tm_sec, (int)t);
}
/* IMPLEMENTATION */
int main() {
/* open data.db and create file res.db to save the result of sorting */
int fd1 = open("data.db", O_RDONLY);
if (-1 == fd1) {
perror("open");
return -1;
}
/* read number of the file, and set flags */
int i, n, res;
show_time();
printf("start to sorting %d nums...\n", NUM);
for (i = 0; i <= NUM - 1; i++) {
res = read(fd1, &n, sizeof(int));
if (res == -1) {
perror("read");
return -1;
} else if (res == 0) {
break;
}
setbit(n); /* set flags */
}
/* traversal the array and write every element to res.db */
int fd2 = open("res.db", O_RDWR | O_CREAT | O_TRUNC, 0664);
if (-1 == fd2) {
perror("open");
return -1;
}
printf("writing to file...\n");
for (i = 0; i <= NUM -1; i++) {
if (testbit(i)) {
write(fd2, &i, sizeof(int));
//printf("%d\t", i);
}
}
show_time();
printf("sorting over!\n");
close(fd1);
close(fd2);
return 0;
}
gcc generate.c -o generate
gcc sort.c -o sort
然后分别运行generate可以生成10000000个数字,sort可以完成排序
算法是没有固定的,就好比是说飞机快还是汽车快;
算法要靠平时慢慢的积累;