这段时间有点困惑,在底层待了一段时间,太没有自由和个性了,重新找一下上层C语言无拘无束的感觉
- #include <stdio.h>
- #include <string.h>
- void bubble_sort(int a[], int n)
- {
- /*冒泡排序*/
- int i, j, t;
- int flag = 1;
- for (i = 0; i < n && flag; i ) {
- flag = 0;
- for (j = 0; j < n - i - 1; j ) {
- if (a[j] < a[j 1]) {
- t = a[j];
- a[j] = a[j 1];
- a[j 1] = t;
- flag = 1;
- }
- }
- }
- }
- void select_sort(int a[], int n)
- {
- /*选择排序*/
- int i, j, k, t;
- for (i = 0; i < n - 1; i ) {
- k = i;
- for (j = i 1; j < n; j ) {
- if (a[k] < a[j])
- k = j;
- }
- if (k != i) {
- t = a[i];
- a[i] = a[k];
- a[k] = t;
- }
- }
- }
- void insert_sort(int a[], int n)
- {
- /*插入排序*/
- int i, j, t;
- for (i = 1; i < n; i ) {
- t = a[i];
- j = i - 1;
- while (j >= 0 && t > a[j]) {
- a[j 1] = a[j];
- j--;
- }
- a[j 1] = t;
- }
- }
- int half_find(int a[], int n, int data)
- {
- /*二分查找*/
- int mid, top = n - 1, bot = 0;
- mid = (top bot) / 2;
- while (top >= bot) {
- if (a[mid] < data)
- bot = mid 1;
- else if (a[mid] > data)
- top = mid - 1;
- else
- return mid;
- mid = (top bot) / 2;
- }
- }
- void func()
- {
- printf("int= %d \n",sizeof(int));
- printf("float= %d \n",sizeof(float));
- printf("double= %d \n",sizeof(double));
- }
- /*
- * 下面这种写法是有错的
- GetMemory(char * p,int num)
- {
- p=(char *)malloc(sizeof(char)*num);
- }
- */
- char * GetMemory(int num)
- {
- return (char *)malloc(sizeof(char)*num);
- }
- void func1()
- {
- char *str =NULL;
- char *str1 =NULL;
- char *a="Hello";
- str=GetMemory(100);
- /*str=GetMemory1(&str1,100);*/
- strcpy(str,a);
- printf("%s \n",str);
- strcpy(str,"hello");
- printf("%s \n",str);
- /* strcpy(str1,a);*/
- /*printf("%s \n",str1);*/
- }
- void func2(int a)
- {
- int n;
- while(a>0)
- {
- if(a&0x01)
- {
- n;
- a=a>>1;
- }
- }
- printf("n=%d \n",n);
- }
- int main(void)
- {
- int a[5] = {1, 2, 3, 4, 5};
- int i;
- printf("%d\n", half_find(a, 5, 5));
- func();
- func1();
- func2(0x0fffff);
- /* select_sort(a, 5);
- for (i = 0; i < 5; i )
- printf("%d ", a[i]);
- */
- printf("\n");
- return 0;
- }
阅读(5777) | 评论(3) | 转发(8) |