This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding=utf-8 | |
import random,math,pylab | |
def dis(p1,p2):#compute distance between p and c | |
f=lambda (x,y):(x-y)**2 | |
return sum(map(f,zip(p1,p2))) | |
def update_center(sets):#sets=[(),(),...] | |
n=len(sets) | |
x,y=zip(*sets) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding=utf-8 | |
#KNN algorithm | |
#describe:given a Classification Class={ci|i=1...n} and ci={sample(j)|j=1...n} | |
#input test_sample | |
#output which class the test_sample in/ie.test_sample ∈ C? | |
import random | |
def dis(p1,p2): | |
f=lambda (x,y):(x-y)**2 | |
return sum(map(f,zip(p1,p2))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.b> | |
#define INITSIZE 100 | |
#define INCREMENT 10 | |
#define TRUE 1 | |
#define FALSE 0 | |
#define ElemType int //根据需要改变ElemType指代的类型 | |
/*...ADT Sequence List...*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#define ElemType int | |
/*...ADT Linked List...*/ | |
/*数据集的定义 */ | |
typedef struct Lnode{ | |
ElemType data; | |
struct Lnode *next; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1.为什么我们要了解常用的数据结构? | |
Changing the data structure does not change the correctness of the program, since we presumably | |
replace a correct implementation with a different correct implementation. | |
However, the new implementation of the data type realizes different tradeoffs | |
in the time to execute various operations, so the total performance can improve dramatically. | |
2.学习数据结构的建议 | |
In data structures, as with most subjects, it is more important to really understand | |
the basic material than have exposure to more advanced concepts. We | |
will focus on each of the three fundamental abstract data types (containers, dictionaries, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdlib.h> | |
#include<stdio.h> | |
struct bin_tree { | |
int data; | |
struct bin_tree * right, * left; | |
}; | |
typedef struct bin_tree node; | |
void insert(node ** tree, int val) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#define ElemType int | |
typedef struct tree{ | |
ElemType item; | |
tree *left; | |
tree *right; | |
}tree; | |
/* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*reference http://users.powernet.co.uk/eton/kandr2/index.html*/ | |
/* | |
Exercise 1-1. Run the ``hello, world'' program on your system. Experiment with | |
leaving out parts of the program, to see what error messages you get. | |
*/ | |
#include <stdio.h> | |
int main(){ | |
printf("hello world.\n"); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*reference http://users.powernet.co.uk/eton/kandr2/index.html*/ | |
/* | |
Exercise 2-1. Write a program to determine the ranges of char, short, int, and long variables, both | |
signed and unsigned, by printing appropriate values from standard headers and by direct | |
computation. Harder if you compute them: determine the ranges of the various floating-point types. | |
*/ | |
/*********too slow*************** | |
#include <stdio.h> | |
#define ElemType signed short | |
int main(){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*reference http://users.powernet.co.uk/eton/kandr2/index.html*/ | |
/* | |
Exercise 3-1. Our binary search makes two tests inside the loop, when one would suffice (at the price of | |
more tests outside.) Write a version with only one test inside the loop and measure the difference in runtime. | |
*/ | |
#include <stdio.h> | |
int binarysearch(int a[],int x,int n){ | |
int low=0,high=n-1; | |
int mid=(low+high)/2; | |
while(low<=high && a[mid]!=x){ |
OlderNewer