代码备份

简介: 堆排序写得很烂,也可能是错的,自己看看就好了…… typedef int debug_int [50];debug_int* p;void print(int a[], int length){ for ...

堆排序

写得很烂,也可能是错的,自己看看就好了……

 

typedef int debug_int [50]; debug_int* p; void print(int a[], int length) { for (int i = 0; i< length; ++i) { cout<<a[i]<<" "; } cout<<endl; } void heapadjust(int a[], int s, int length) { int temp; int child_pos; for (int i = s; 2*i+1 < length ; i = child_pos) { child_pos = 2*i +1; if (child_pos != length - 1 && a[child_pos] < a[child_pos +1] ) { ++child_pos; } if (a[i] < a[child_pos]) { temp = a[i]; a[i] = a[child_pos]; a[child_pos] = temp; } else break; } } void heapsort(int a[], int length) { for (int i = length/2 - 1 ; i >= 0 ; --i) { heapadjust(a, i, length); print(a, length); } cout<<"************************"<<endl; for (int i = length - 1; i>0; --i) { swap(a[i], a[0]); heapadjust(a, 0, i); print(a, length); } } void main() { int a[50]; p = &a; int i = 0; while(cin>>a[i]) { ++i; } heapsort(a, i); cout<<"------------------"<<endl; print(a, i); } 

快速排序

template<class T> size_t Partition(T a[], int low, int high) { T pivotkey = a[low]; int old_high = high; int old_low = low; while(low < high) { while(low < high && a[high] >= pivotkey) --high; a[low] = a[high]; //swap(a[low], a[high]); while(low < high && a[low] <= pivotkey) ++low; a[high] = a[low]; //swap(a[low], a[high]); //if (low < high) //{ // swap(a[low], a[high]); //} } if (low <= old_high) { a[low] = pivotkey; } return low; } template<class T> void Qsort(T a[], int low, int high) { int location; if (low < high) { location = Partition(a, low, high); Qsort(a, low, location - 1); Qsort(a, location + 1, high); } } 

 

atoi

long __cdecl atol( const char *nptr ) { int c; /* current char */ long total; /* current total */ int sign; /* if '- ', then negative, otherwise positive */ /* skip whitespace */ while ( isspace((int)(unsigned char)*nptr) ) ++nptr; c = (int)(unsigned char)*nptr++; sign = c; /* save sign indication */ if (c == '- ' || c == '+ ') c = (int)(unsigned char)*nptr++; /* skip sign */ total = 0; while (isdigit(c)) { total = 10 * total + (c - '0 '); /* accumulate digit */ c = (int)(unsigned char)*nptr++; /* get next char */ } if (sign == '- ') return -total; else return total; /* return result, negated if necessary */ } int __cdecl atoi( const char *nptr ) { return (int)atol(nptr); } 

 

二分查找

int binaryfind(int a[], int n, int val) { int low = 0; int high = n - 1; if (n <= 0) { return -1; } while(low <= high) { int mid = low + (high - low)/2; if (a[mid] == val) { return mid; } else if (a[mid] > val) { high = mid -1; } else { low = mid + 1; } } return -1; } 

链表反向和合并

//定义数据结构 struct Node { Node(int i):data(i),next(0){} int data; Node* next; }; void ListReverse(Node* &p) { Node* oldhead = p->next; Node* newhead = p; if (p == NULL) { return; } newhead->next = NULL; while(oldhead) { Node* temp = oldhead->next; oldhead->next = newhead; newhead = oldhead; oldhead = temp; } p = newhead; } Node* ListMerge(Node* p1, Node* p2) { if (p1 == NULL) { return p2; } if (p2 == NULL) { return p1; } Node* head = p1; p1 = p1->next; Node* cur = head; while(p1 && p2) { if (p1->data <= p2->data) { head->next = p1; head = head->next; p1 = p1->next; } else { head->next = p2; head = head->next; p2 = p2->next; } } if (p1 != NULL) { head->next = p1; } if (p2 != NULL) { head->next = p2; } return cur; } 

判断大小端机器

int isSmallEndian() { static union u{ char c[2]; short i; }U = {0x0001}; return U.c[0]; } 

变参函数

void printInt(int n, ...) { va_list ap; va_start(ap, n); for (int i = 0; i < n; ++i) { int temp = va_arg(ap, int); cout<<temp<<' '; } va_end(ap); cout<<endl; } 

strstr

char* strstr(const char *str1, const char *str2) { char *s1, *s2; _ASSERT(str1 && str2); //空字符串是任何字符串的子串 if ('/0' == *str2){ return (char*)str1; } while (*str1) { s1 = (char*)str1; s2 = (char*)str2; while ((*s1 == *s2) && *s1 && *s2){ s1++; s2++; } //匹配成功 if ('/0' == *s2){ return (char*)str1; } str1++; } return NULL; } 

strcpy

char * strcpy (char * dst, char * src) { char * cp = dst; while( *cp++ = *src++ ) ; /* Copy src over dst */ return( dst ); } 

strcat

char * strcat (char * dst, char * src) { char * cp = dst; while( *cp ) ++cp; /* Find end of dst */ while( *cp++ = *src++ ) ; /* Copy src to end of dst */ return( dst ); } 

strcmp

int strcmp2 (const char *src, const char *dst) { int ret = 0 ; while(!(ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst) { ++src; ++dst; } if ( ret < 0 ) ret = -1 ; else if ( ret > 0 ) ret = 1 ; return( ret ); } 

统计二进制位为1的个数

//算法:统计x转化为2进制的位中为1的个数 //来源:网上 int func(int x) { int countx = 0; while(x) { countx ++; x = x&(x-1); } return countx; } int func(int x) { int countx = 0; for(int i=0;i<32;i++){ if((x>>i)&1) countx ++; } return countx; } 

相关文章
|
3月前
|
弹性计算 IDE 开发工具
ECS热门应用 | 轻松打造一套 Web IDE
使用ECS云服务器搭建网页IDE,增强编码便捷性,提升开发者体验。
ECS热门应用 | 轻松打造一套 Web IDE
|
2月前
|
安全 Java 程序员
代码救火队:try-catch-finally带你走出异常困境
代码救火队:try-catch-finally带你走出异常困境
29 0
|
8天前
|
NoSQL Cloud Native Redis
Redis核心开发者的新征程:阿里云与Valkey社区的技术融合与创新
阿里云瑶池数据库团队后续将持续参与Valkey社区,如过往在Redis社区一样耕耘,为开源社区作出持续贡献。
Redis核心开发者的新征程:阿里云与Valkey社区的技术融合与创新
|
7天前
|
关系型数据库 分布式数据库 数据库
PolarDB闪电助攻,《香肠派对》百亿好友关系实现毫秒级查询
PolarDB分布式版助力《香肠派对》实现百亿好友关系20万QPS的毫秒级查询。
PolarDB闪电助攻,《香肠派对》百亿好友关系实现毫秒级查询
|
9天前
|
消息中间件 Cloud Native Serverless
RocketMQ 事件驱动:云时代的事件驱动有啥不同?
本文深入探讨了云时代 EDA 的新内涵及它在云时代再次流行的主要驱动力,包括技术驱动力和商业驱动力,随后重点介绍了 RocketMQ 5.0 推出的子产品 EventBridge,并通过几个云时代事件驱动的典型案例,进一步叙述了云时代事件驱动的常见场景和最佳实践。
115100 1
|
9天前
|
弹性计算 安全 API
访问控制(RAM)|云上安全使用AccessKey的最佳实践
集中管控AK/SK的生命周期,可以极大降低AK/SK管理和使用成本,同时通过加密和轮转的方式,保证AK/SK的安全使用,本次分享为您介绍产品原理,以及具体的使用步骤。
101871 3
|
6天前
|
物联网 PyTorch 测试技术
手把手教你捏一个自己的Agent
Modelscope AgentFabric是一个基于ModelScope-Agent的交互式智能体应用,用于方便地创建针对各种现实应用量身定制智能体,目前已经在生产级别落地。
|
9天前
|
自然语言处理 Cloud Native Serverless
通义灵码牵手阿里云函数计算 FC ,打造智能编码新体验
近日,通义灵码正式进驻函数计算 FC WebIDE,让使用函数计算产品的开发者在其熟悉的云端集成开发环境中,无需再次登录即可使用通义灵码的智能编程能力,实现开发效率与代码质量的双重提升。
95438 2
|
1天前
|
机器人 Linux API
基于Ollama+AnythingLLM轻松打造本地大模型知识库
Ollama是开源工具,简化了在本地运行大型语言模型(ile优化模型运行,支持GPU使用和热加载。它轻量、易用,可在Mac和Linux上通过Docker快速部署。AnythingLLM是Mintplex Labs的文档聊天机器人,支持多用户、多种文档格式,提供对话和查询模式,内置向量数据库,可高效管理大模型和文档。它也是开源的,能与Ollama结合使用,提供安全、低成本的LLM体验。这两款工具旨在促进本地高效利用和管理LLMs。
31313 18
|
21小时前
|
人工智能 自然语言处理 API
Claude3是什么?
Claude 3最近备受各大媒体瞩目,成为了AI领域备受关注的新宠。在ChatGPT推出更高版本之前,Claude 3已经被公认为是语言类AI工具中的佼佼者,特别在处理逻辑性和长篇上下文方面表现突出。然而,与此同时,Claude 3的注册流程也备受诟病,被认为是所有AI工具中最为复杂的之一。 这篇内容教大家 注册Claude 3 以及升级 教程。
13667 1
Claude3是什么?