4-11-初学排序与查找

本文介绍了C语言的基础知识,包括数组的操作、排序算法(如冒泡排序)、查找算法(如二分法查找和插值查找)以及插入排序等,并通过实例展示了如何使用这些算法和技术进行数据处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

学习4-11期视频笔记

1、数组

#include<stdio.h>
#include<stdlib.h>
#definenum 4 //常量
//constint num=4;  vc这个就不可以,这是伪常量
//vc必须静态分配,gcc里面都可以
void main()
{
       double a[num] = { 1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0};//默认没有初始化的,会填充为0
       printf("\n%d", sizeof(a));//数组的大小,即8(每个元素的大小)*10(元素个数)
       for (int i = 0; i < 10; i++)
       {
              printf("\n%f,%f,%f",a[i], *(&a[i]), *(a + i));//都是表示数组第i个元素
              printf("%p,%p",&a[i], a + i);//都是表示数组第i个元素的地址
       }
       system("pause");
}


数组不能批量操作(比如不能整体性输出,printf(“%d”,a)),字符串除外

数组不能加减运算(a+b)

数组越界不一定出错,不越界一定不会出错。


2、冒泡排序

       inta[n];
冒泡排序,寻找最小数
       for (int i = 0; i < n-1; i++)
       {
              min = i;
              for (int j = i+1; j < n-1; j++)
              {
                     if (a[j] < a[min])
                            min = j;
              }
              if (min != i)
              {
                     temp = a[i];
                     a[i] = a[min];
                     a[min] = temp;
              }
       }
//计算了n*(n-1)/2
时间复杂度为O(n²)
                                             
冒泡排序,交换数值
       for (int i = 0; i < n-1; i++)//n-1次
       {
              for (int j = 0; j < n - 1 - i;j++)
              {
                     if (a[j] < a[j+ 1])//对比
                     {
                            temp = a[j];//交换
                            a[j] = a[j + 1];
                            a[j + 1] = temp;
                     }
              }
       }


3、二分法查找

voidsearch(int a[N], int num)
{
       int tou = 0;
       int wei = N - 1;
       int zhong;
       int flag = -1;
       while (tou <= wei)
       {
              zhong = (tou + wei) / 2;
              if (num == a[zhong])
              {
                     printf("找到,a[%d]=%d",zhong, num);
                     flag = 1;
                     break;
              }
              else if (num > a[zhong])
              {
                     tou = zhong + 1;
              }
              else
              {
                     wei = zhong - 1;
              }
       }
       if (flag == -1)
       {
              printf("没有找到");
       }
}


4、插值查找

voidsearch(int a[N], int num)
{
       int tou = 0;
       int wei = N - 1;
       int zhong;
       int flag = -1;
       while (tou <= wei)
       {
              //zhong = (tou + wei) / 2;
              //zhong = tou + (wei - tou) / 2;//递增数列
              zhong = tou + (wei - tou)*1.0*(num- a[tou]) / (a[wei] - a[tou]);
              if (num == a[zhong])
              {
                     printf("找到,a[%d]=%d",zhong, num);
                     flag = 1;
                     break;
              }
              else if (num > a[zhong])
              {
                     tou = zhong + 1;
              }
              else
              {
                     wei = zhong - 1;
              }
       }
       if (flag == -1)
       {
              printf("没有找到");
       }
}


5、插入排序

#define_CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
 
voidmain()
{     
       int a[10] = { 1,5,2,4,3,8,7,9,10,6 };
       for (int i = 1; i < 10; i++)
       {
              int temp = a[i];
              int j = i;
              while (j > 0 && a[j -1] > temp)
              {
                     a[j] = a[j - 1];
                     j--;
 
              }
              a[j] = temp;
              for (int i = 0; i < 10; i++)
              {
                     printf("%d",a[i]);
              }
              printf("\n");
       }
       system("pause");
}


 

6、大数据实战-硬盘查找开放数据

#define_CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
 
charstrpath[256] = "E:\\c\\大数据相关数据\\kaifang.txt";//路径
charsavepath[256] = { 0 };
 
voidshowlist(char str[256])
{
       sprintf(savepath, "E:\\%s.txt",str);
       FILE *pf;//文件指针
       pf = fopen(strpath, "r");//读取
       FILE *pfw = fopen(savepath,"w");
       if (pf == NULL|| pfw == NULL)
       {
              printf("文件打开失败");
       }
       else
       {  //feof(pf)到了文件末尾就返回1,否则返回0
              while (!feof(pf))//没有到文件末尾就继续
              {
                     char readstr[1024] = { 0 };
                     fgets(readstr, 1024, pf);//读取一行
                     char *p =strstr(readstr,str);//字符串查找
                     if (p != NULL)
                     {
                            puts(readstr);//打印
                            fputs(readstr,pfw);//写入
                     }
              }
              fclose(pf);
              fclose(pfw);
       }
}
voidmain()
{
       char str[256] = { 0 };
       scanf("%s", str);
       printf("你要查询的是%s\n", str);
 
       time_t start ,end;
       time(&start);
       showlist(str);
       time(&end);
 
       printf("花了%f秒", difftime(end, start));
       system(savepath);
       system("pause");
}
 


7、二维数组

#define_CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
 
voidmain()
{   //未完全初始化的元素,默认为0
       //int b[5][5] = {1,2,3,4,5}//前5个数据有数值,后面为0
       //int a[5][5]={{1,2,3,4,5},{1,2}}//第一二行部分数据设定,其余为0
       //行坐标可以省略a[][3]={{1,2,3},{4,5,6}};
       //纵坐标不可以省略a[3][];
       //二维数组元素在内存中按行存放,第一行首地址位a[0],第二行a[1]...   &a[i][j]=a[i]+j
       int a[5][5] = { {1,2,3,4,5,6},{1,2,3} };
       printf("%d\n", sizeof(a));
       printf("%p", &a);
       int num = 1;
       for (int i = 0; i < 5; i++)
       {
              for (int j = 0; j < 5; j++)
              {
                     printf("%4d",a[i][j]);//查看可以发现,在内存中以1,2,3...25顺序排列,且相邻数据内存位置差4
              }
              printf("\n");
       }
       system("pause");
}
 


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值