首页 文章 精选 留言 我的

精选列表

搜索[Tree-like算法],共10004篇文章
优秀的个人博客,低调大师

排序算法详细过程:选择排序

demo地址:https://github.com/weiman152/PaiXu.git 选择排序是先比较,并不急着交换,而是记录最小的值的位置,把最小的值与第一个位置的值进行交换。然后第二轮的时候,再次遍历除了第一个位置的之外的其他数字,找出最小的,放在第二个位置。以此类推,最后得出排序后的数组。假如数组的个数为n,选择排序会进行 n+(n-1)+(n-2)+......+2+1次比较,最多进行n次交换。下面进行举例说明:(这里为了更加容易理解,把每一次的比较都列了出来) qiaoba5.png 1, 16, 23, 56, 89, 33, 7, 27, 55, 37, 48 i: 第几轮比较 min: 当前最小值的下标 j: 同一轮中每一个值与最小值的比较 i=0://第一轮 min = 0 (步骤中用 m 代表) j=1: 1(m), 16(), 23, 56, 89, 33, 7, 27, 55, 37, 48 j=2: 1(m), 16, 23(), 56, 89, 33, 7, 27, 55, 37, 48 j=3: 1(m), 16, 23, 56(), 89, 33, 7, 27, 55, 37, 48 j=4: 1(m), 16, 23, 56, 89(), 33, 7, 27, 55, 37, 48 j=5: 1(m), 16, 23, 56, 89, 33(), 7, 27, 55, 37, 48 j=6: 1(m), 16, 23, 56, 89, 33, 7(), 27, 55, 37, 48 j=7: 1(m), 16, 23, 56, 89, 33, 7, 27(), 55, 37, 48 j=8: 1(m), 16, 23, 56, 89, 33, 7, 27, 55(), 37, 48 j=9: 1(m), 16, 23, 56, 89, 33, 7, 27, 55, 37(), 48 j=10: 1(m), 16, 23, 56, 89, 33, 7, 27, 55, 37, 48() 第一轮比较完成结果:(找到最小值1,放到第一个位置,共比较了 n-1 次) 结果: 1(m), 16, 23, 56, 89, 33, 7, 27, 55, 37, 48 i=1://第二轮 min = 1 j=2: 1(️), 16(m), 23(), 56, 89, 33, 7, 27, 55, 37, 48 j=3: 1(️), 16(m), 23, 56(), 89, 33, 7, 27, 55, 37, 48 j=4: 1(️), 16(m), 23, 56, 89(), 33, 7, 27, 55, 37, 48 j=5: 1(️), 16(m), 23, 56, 89, 33(), 7, 27, 55, 37, 48 j=6: 1(️), 16(m), 23, 56, 89, 33, 7(), 27, 55, 37, 48 j=7: 1(️), 16, 23, 56, 89, 33, 7(m), 27(), 55, 37, 48 j=8: 1(️), 16, 23, 56, 89, 33, 7(m), 27, 55(), 37, 48 j=9: 1(️), 16, 23, 56, 89, 33, 7(m), 27, 55, 37(), 48 j=10: 1(️), 16, 23, 56, 89, 33, 7(m), 27, 55, 37, 48() 比较完成之后交换 : 1(️), 7(m), 23, 56, 89, 33, 16, 27, 55, 37, 48 第二轮比较结果: 1(️), 7(m), 23, 56, 89, 33, 16, 27, 55, 37, 48 i=2://第三轮 min = 2 j=3:1(️), 7(️), 23(m), 56(), 89, 33, 16, 27, 55, 37, 48 j=4:1(️), 7(️), 23(m), 56, 89(), 33, 16, 27, 55, 37, 48 j=5:1(️), 7(️), 23(m), 56, 89, 33(), 16, 27, 55, 37, 48 j=6:1(️), 7(️), 23(m), 56, 89, 33, 16(), 27, 55, 37, 48 j=7:1(️), 7(️), 23, 56, 89, 33, 16(m), 27(), 55, 37, 48 j=8:1(️), 7(️), 23, 56, 89, 33, 16(m), 27, 55(), 37, 48 j=8:1(️), 7(️), 23, 56, 89, 33, 16(m), 27, 55, 37(), 48 j=8:1(️), 7(️), 23, 56, 89, 33, 16(m), 27, 55, 37, 48() 交换:1(️), 7(️), 16(m), 56, 89, 33, 23, 27, 55, 37, 48() 第三轮比较结果: 1(️), 7(️), 16(m), 56, 89, 33, 23, 27, 55, 37, 48 i=3://第四轮 min = 3 j=4:1(️), 7(️), 16(️), 56(m), 89(), 33, 23, 27, 55, 37, 48 j=5:1(️), 7(️), 16(️), 56(m), 89, 33(), 23, 27, 55, 37, 48 j=6:1(️), 7(️), 16(️), 56, 89, 33, 23(), 27, 55, 37, 48 j=7:1(️), 7(️), 16(️), 56, 89, 33, 23(m), 27(), 55, 37, 48 j=8:1(️), 7(️), 16(️), 56, 89, 33, 23(m), 27, 55(), 37, 48 j=9:1(️), 7(️), 16(️), 56, 89, 33, 23(m), 27, 55, 37(), 48 j=10:1(️), 7(️), 16(️), 56, 89, 33, 23(m), 27, 55, 37, 48() 交换:1(️), 7(️), 16(️), 23(m), 89, 33, 56, 27, 55, 37, 48 第四轮比较结果: 1(️), 7(️), 16(️), 23(m), 89, 33, 56, 27, 55, 37, 48 ......... 四轮比较下来,确认了前四个最小值。 下面我们就使用不同语言把代码写出来吧。 OC语言: main函数: #import <Foundation/Foundation.h> #import "Sort.h" int main(int argc, const char * argv[]) { @autoreleasepool { //选择排序 NSMutableArray * arr2= [NSMutableArray arrayWithArray:@[@1,@16,@23,@56,@89,@33,@7,@27,@55,@37,@48]]; NSArray * select = [sort selectedSortWithArray:arr2]; NSLog(@"选择: %@",select); } return 0; } Sort类: Sort.h: #import <Foundation/Foundation.h> @interface Sort : NSObject /** 选择排序 @param array 排序前的数组 @return 排序后的数组 */ - (NSArray *)selectedSortWithArray:(NSMutableArray *)array; Sort.m: #import "Sort.h" @implementation Sort - (NSArray *)selectedSortWithArray:(NSMutableArray *)array { NSMutableArray * arr = [array mutableCopy]; int min = 0; for (int j = 0; j<arr.count-1; j++) { min = j;//最小值下标记录 for (int i = j+1; i<arr.count; i++) { if (arr[min] > arr[i]) { min = i; } } if (min!=j) { [arr exchangeObjectAtIndex:min withObjectAtIndex:j]; } } return arr; } @end Swift语言 main.swift import Foundation let mySort: Sort = Sort() let arr2: [Int] = [1,16,23,56,89,33,7,27,55,37,48] mySort.selectedSort(array: arr2) Sort.swift // // Sort.swift // Sort_Swift // // Created by iOS on 2018/3/13. // Copyright © 2018年 weiman. All rights reserved. // import Cocoa class Sort: NSObject { /// 选择排序 /// /// - Parameter array: 需要排序的数组 func selectedSort(array: [Int]) { var arr = array //记录最小值的下标 var min = 0; for i in 0..<arr.count-1 { min = i for j in i+1..<arr.count { if arr[j] < arr[min] { min = j } } if min != i { arr.swapAt(i, min) } } print("选择排序: \(arr)") } } C语言 // // main.c // Sort_C // // Created by iOS on 2018/3/13. // Copyright © 2018年 weiman. All rights reserved. // #include <stdio.h> void selectedSort(int a[], int size); int main(int argc, const char * argv[]) { printf("Hello, World!\n"); int arr[] = {1,16,23,56,89,33,7,27,55,37,48}; int sizeA = sizeof(arr)/sizeof(arr[0]); selectedSort(arr, sizeA); for(int i=0; i<sizeA; i++){ printf(" %d ",arr[i]); } printf("\n"); return 0; } /** 选择排序 @param a 排序的数组 @param size 数组的长度 */ void selectedSort(int a[], int size){ int min = 0; for (int i = 0; i< size-1; i++) { min = i; for (int j=i+1; j<size; j++) { if (a[j]<a[min]) { min = j; } } if (min != i) { int tmp = a[i]; a[i] = a[min]; a[min] = tmp; } } } 排序前: [1, 16, 23, 56, 89, 33, 7, 27, 55, 37, 48] 打印下排序执行的每一个步骤: i:0,j:1 [1, 16, 23, 56, 89, 33, 7, 27, 55, 37, 48] i:0,j:2 [1, 16, 23, 56, 89, 33, 7, 27, 55, 37, 48] i:0,j:3 [1, 16, 23, 56, 89, 33, 7, 27, 55, 37, 48] i:0,j:4 [1, 16, 23, 56, 89, 33, 7, 27, 55, 37, 48] i:0,j:5 [1, 16, 23, 56, 89, 33, 7, 27, 55, 37, 48] i:0,j:6 [1, 16, 23, 56, 89, 33, 7, 27, 55, 37, 48] i:0,j:7 [1, 16, 23, 56, 89, 33, 7, 27, 55, 37, 48] i:0,j:8 [1, 16, 23, 56, 89, 33, 7, 27, 55, 37, 48] i:0,j:9 [1, 16, 23, 56, 89, 33, 7, 27, 55, 37, 48] i:0,j:10 [1, 16, 23, 56, 89, 33, 7, 27, 55, 37, 48] i:1,j:2 [1, 16, 23, 56, 89, 33, 7, 27, 55, 37, 48] i:1,j:3 [1, 16, 23, 56, 89, 33, 7, 27, 55, 37, 48] i:1,j:4 [1, 16, 23, 56, 89, 33, 7, 27, 55, 37, 48] i:1,j:5 [1, 16, 23, 56, 89, 33, 7, 27, 55, 37, 48] i:1,j:6 [1, 16, 23, 56, 89, 33, 7, 27, 55, 37, 48] i:1,j:7 [1, 16, 23, 56, 89, 33, 7, 27, 55, 37, 48] i:1,j:8 [1, 16, 23, 56, 89, 33, 7, 27, 55, 37, 48] i:1,j:9 [1, 16, 23, 56, 89, 33, 7, 27, 55, 37, 48] i:1,j:10 [1, 16, 23, 56, 89, 33, 7, 27, 55, 37, 48] i:2,j:3 [1, 7, 23, 56, 89, 33, 16, 27, 55, 37, 48] i:2,j:4 [1, 7, 23, 56, 89, 33, 16, 27, 55, 37, 48] i:2,j:5 [1, 7, 23, 56, 89, 33, 16, 27, 55, 37, 48] i:2,j:6 [1, 7, 23, 56, 89, 33, 16, 27, 55, 37, 48] i:2,j:7 [1, 7, 23, 56, 89, 33, 16, 27, 55, 37, 48] i:2,j:8 [1, 7, 23, 56, 89, 33, 16, 27, 55, 37, 48] i:2,j:9 [1, 7, 23, 56, 89, 33, 16, 27, 55, 37, 48] i:2,j:10 [1, 7, 23, 56, 89, 33, 16, 27, 55, 37, 48] i:3,j:4 [1, 7, 16, 56, 89, 33, 23, 27, 55, 37, 48] i:3,j:5 [1, 7, 16, 56, 89, 33, 23, 27, 55, 37, 48] i:3,j:6 [1, 7, 16, 56, 89, 33, 23, 27, 55, 37, 48] i:3,j:7 [1, 7, 16, 56, 89, 33, 23, 27, 55, 37, 48] i:3,j:8 [1, 7, 16, 56, 89, 33, 23, 27, 55, 37, 48] i:3,j:9 [1, 7, 16, 56, 89, 33, 23, 27, 55, 37, 48] i:3,j:10 [1, 7, 16, 56, 89, 33, 23, 27, 55, 37, 48] i:4,j:5 [1, 7, 16, 23, 89, 33, 56, 27, 55, 37, 48] i:4,j:6 [1, 7, 16, 23, 89, 33, 56, 27, 55, 37, 48] i:4,j:7 [1, 7, 16, 23, 89, 33, 56, 27, 55, 37, 48] i:4,j:8 [1, 7, 16, 23, 89, 33, 56, 27, 55, 37, 48] i:4,j:9 [1, 7, 16, 23, 89, 33, 56, 27, 55, 37, 48] i:4,j:10 [1, 7, 16, 23, 89, 33, 56, 27, 55, 37, 48] i:5,j:6 [1, 7, 16, 23, 27, 33, 56, 89, 55, 37, 48] i:5,j:7 [1, 7, 16, 23, 27, 33, 56, 89, 55, 37, 48] i:5,j:8 [1, 7, 16, 23, 27, 33, 56, 89, 55, 37, 48] i:5,j:9 [1, 7, 16, 23, 27, 33, 56, 89, 55, 37, 48] i:5,j:10 [1, 7, 16, 23, 27, 33, 56, 89, 55, 37, 48] i:6,j:7 [1, 7, 16, 23, 27, 33, 56, 89, 55, 37, 48] i:6,j:8 [1, 7, 16, 23, 27, 33, 56, 89, 55, 37, 48] i:6,j:9 [1, 7, 16, 23, 27, 33, 56, 89, 55, 37, 48] i:6,j:10 [1, 7, 16, 23, 27, 33, 56, 89, 55, 37, 48] i:7,j:8 [1, 7, 16, 23, 27, 33, 37, 89, 55, 56, 48] i:7,j:9 [1, 7, 16, 23, 27, 33, 37, 89, 55, 56, 48] i:7,j:10 [1, 7, 16, 23, 27, 33, 37, 89, 55, 56, 48] i:8,j:9 [1, 7, 16, 23, 27, 33, 37, 48, 55, 56, 89] i:8,j:10 [1, 7, 16, 23, 27, 33, 37, 48, 55, 56, 89] i:9,j:10 [1, 7, 16, 23, 27, 33, 37, 48, 55, 56, 89] 选择排序结果: [1, 7, 16, 23, 27, 33, 37, 48, 55, 56, 89]

优秀的个人博客,低调大师

算法学习之路|日期问题

小明正在整理一批历史文献。这些历史文献中出现了很多日期。小明知道这些日期都在1960年1月1日至2059年12月31日。令小明头疼的是,这些日期采用的格式非常不统一,有采用年/月/日的,有采用月/日/年的,还有采用日/月/年的。更加麻烦的是,年份也都省略了前两位,使得文献上的一个日期,存在很多可能的日期与其对应。 比如02/03/04,可能是2002年03月04日、2004年02月03日或2004年03月02日。 给出一个文献上的日期,你能帮助小明判断有哪些可能的日期对其对应吗? 输入 一个日期,格式是"AA/BB/CC"。 (0 <= A, B, C <= 9) 输出 输出若干个不相同的日期,每个日期一行,格式是"yyyy-MM-dd"。多个日期按从早到晚排列。 样例输入 02/03/04 样例输出 2002-03-04 2004-02-03 2004-03-02 资源约定: 峰值内存消耗(含虚拟机) < 256M CPU消耗 < 1000ms 请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。 注意: main函数需要返回0; 只使用ANSI C/ANSI C++ 标准; 不要调用依赖于编译环境或操作系统的特殊函数。 所有依赖的函数必须明确地在源文件中 #include 不能通过工程设置而省略常用头文件。 提交程序时,注意选择所期望的语言类型和编译器类型 解题思路: 把每一部分的功能都分开了。 先判断天数,月份,年数是否正确。 注意: 瑞年以及不是瑞年要当心。 #include<iostream> using namespace std; int day(int month,int year); bool isrui(int year);//这两个包用于给其他包用 //剩下的包只给main函数用 bool isyear(int year){ if(year<=2059&&year>=1960){ return true; } else{ return false; } } bool ismonth(int month){ if(month<=12&&month>=1){ return true; } else{ return false; } } bool isday(int year,int month,int yourday){ if(yourday>day(month,year)||yourday==0){ return false; } else return true; } bool isrui(int year){ if ((year%4==0&&year%100!=0)||year%400==0){ return true; } else{ return false; } } int day(int month,int year){ if(month==0){ return 0; } if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){ return 31; } else if(month==2&&isrui(year)){ return 29; } else if(month==2&&!isrui(year)){ return 28; } else{ return 30; } } void abc(int A,int B,int C){//核心函数 if(A<60) A+=2000; else if(A>=60) A+=1900; if(isyear(A)){ if(ismonth(B)){ if(isday(A, B, C)){ printf("%d-%02d-%02d\n",A,B,C); } } } } int main(){ int A,B,C; scanf("%d/%d/%d",&A,&B,&C); abc(A,B,C); abc(C,A,B); abc(C,B,A); }

优秀的个人博客,低调大师

ES 相似度算法设置(续)

Tuning BM25 One of the nice features of BM25 is that, unlike TF/IDF, it has two parameters that allow it to be tuned: k1 This parameter controls how quickly an increase in term frequency results in term-frequency saturation. The default value is 1.2. Lower values result in quicker saturation, and higher values in slower saturation. b This parameter controls how much effect field-length normalization should have.A value of 0.0disables normalization completely, and a value of 1.0normalizes fully. The default is 0.75. The practicalities of tuning BM25 are another matter. The default values fork1andbshould be suitable for most document collections, but the optimal values really depend on the collection. Finding good values for your collection is a matter of adjusting, checking, and adjusting again. The similarity algorithm can be set on a per-field basis.It’s just a matter of specifying the chosen algorithmin the field’s mapping: PUT /my_index { "mappings": { "doc": { "properties": { "title": { "type": "string", "similarity": "BM25" }, "body": { "type": "string", "similarity": "default" } } } } Thetitlefield uses BM25 similarity. Thebodyfield uses the default similarity (seeLucene’s Practical Scoring Function). Currently, it is not possible to change thesimilaritymapping for an existing field. You would need to reindex your data in order to do that. Configuring BM25 Configuring a similarity is muchlike configuring an analyzer. Custom similarities can be specified when creating an index. For instance: PUT /my_index { "settings": { "similarity": { "my_bm25": { "type": "BM25", "b": 0 } } }, "mappings": { "doc": { "properties": { "title": { "type": "string", "similarity": "my_bm25" }, "body": { "type": "string", "similarity": "BM25" } } } } } 参考:https://www.elastic.co/guide/en/elasticsearch/guide/current/changing-similarities.html 本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/bonelee/p/6472828.html,如需转载请自行联系原作者

资源下载

更多资源
腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册