【数据结构】24种常见算法题(二)

简介: 常见算法题
  1. 串删除算法代码补全
publicIStringdelete(intbegin , intend) {
// 1 参数校验if(begin<0||end>curlen||begin>end) {
thrownewStringIndexOutOfBoundsException("条件不合法");
    }
// 2 核心:将后面内容移动到签名// 2.1 移动for(inti=0 ; i<curlen-end ; i++) {
strvalue[i+begin] =strvalue[i+end];      
    }
// 2.2 重新统计长度  (end-begin 需要删除串的长度)curlen=curlen- (end-begin)    
returnthis;
}
  1. n!算法代码补全(n=10):
publicclassTestFactorial {
publicstaticvoidmain(String[] args) {
System.out.println(factorial(4));
    }
privatestaticintfactorial(intn) {
if(n==1 ) {                         【代码1】return1;
        }
returnn*factorial(n-1);             【代码2】    }
}
  1. 不带监视哨的插入排序算法
publicvoidinsertSort() {
RecordNodetemp;
inti, j;
for (i=1; i<this.curlen; i++) {
temp=r[i];
for (j=i-1; j>=0&&temp.key.compareTo(r[j].key) <0; j--) {
r[j+1] =r[j];
        }
r[j+1] =temp;
display();
    }
}
  1. 带监视哨的插入排序算法
publicvoidinsertSortWithGuard() {
inti, j;
for (i=1; i<this.curlen; i++) {
r[0] =r[i];
for (j=i-1; r[0].key.compareTo(r[j].key) <0; j--) {
r[j+1] =r[j];
        }
r[j+1] =r[0];
System.out.print("第"+i+"趟: ");
display(9);
    }
}
  1. 优化版冒泡排序算法
publicvoidbubbleSort() {
RecordNodetemp;
booleanflag=true;
for (inti=1; i<this.curlen&&flag; i++) {
flag=false;
for (intj=0; j<this.curlen-i; j++) {
if (r[j].key.compareTo(r[j+1].key) >0) {
temp=r[j];
r[j] =r[j+1];
r[j+1] =temp;
flag=true;
            }
        }
System.out.print("第"+i+"趟: ");
display();
    }
}
  1. 直接选择排序算法
publicvoidselectSort() {
RecordNodetemp;
for (inti=0; i<this.curlen-1; i++) {
intmin=i;
for (intj=i+1; j<this.curlen; j++) {
if (r[j].key.compareTo(r[min].key) <0) {
min=j;
            }
        }
if (min!=i) {
temp=r[i];
r[i] =r[min];
r[min] =temp;
        }
    }
}
  1. 二路归并算法
publicvoidmergepass(RecordNode[] r, RecordNode[] order, ints, intn) {……}//一趟归并算法publicvoidmergeSort() {
ints=1;
intn=this.curlen;
RecordNode[] temp=newRecordNode[n];
while (s<n) {
mergepass(r, temp, s, n);
s*=2;
mergepass(temp, r, s, n);
s*=2;
}
}
  1. 补全带监视哨的顺序查找算法代码
publicintseqSearchWithGuard(Comparablekey) {
inti=length() -1;
r[0].key=key;
while(r[i].key.compareTo(key) !=0) {
i--;
}
returni>0?i : -1;
}
  1. 补全顺序查找代码
publicintseqSearch(Comparablekey) {
inti=0 , n=length();
while(i<n&&r[i].key.compareTo(key) !=0) {
i++;
}
returni<n?i : -1}
  1. 补全二分查找算法代码
publicintbinarySearch(Comparablekey) {
if(length() >0) {
intlow=0, high=length() -1;
while (low<=high) {
intmid= (low+high) /2;
if(r[mid].key.compareTo(key) ==0) {
returnmid;
            } elseif (r[mid].key.compareTo(key) >0) { // 中间值比给定值大high=mid-1;
            } else {
low=mid+1;
            }
        }
    }
return-1;
}
  1. 补全二叉排序树的递归算法代码
privateObjectsearchBST(BiTreeNodep, Comparablekey) {
if (p!=null) {
if (key.compareTo(((RecordNode) p.data).key) ==0) {
returnp.data;
        }
if (key.compareTo(((RecordNode) p.data).key) <0) {
returnsearchBST(p.lchild, key);
        } else {
returnsearchBST(p.rchild, key);
        }
    }
returnnull;
}
相关文章
|
2天前
|
存储 算法
【数据结构和算法】--- 二叉树(4)--二叉树链式结构的实现(2)
【数据结构和算法】--- 二叉树(4)--二叉树链式结构的实现(2)
7 0
|
2天前
|
存储 算法 Linux
【数据结构和算法】---二叉树(1)--树概念及结构
【数据结构和算法】---二叉树(1)--树概念及结构
10 0
|
2天前
|
存储 算法
【数据结构和算法】--队列的特殊结构-循环队列
【数据结构和算法】--队列的特殊结构-循环队列
6 0
|
2天前
|
算法 分布式数据库
【数据结构和算法】--- 二叉树(5)--二叉树OJ题
【数据结构和算法】--- 二叉树(5)--二叉树OJ题
8 1
|
3天前
|
算法
【C/数据结构和算法】:栈和队列
【C/数据结构和算法】:栈和队列
13 1
|
2天前
|
算法 Java
Java数据结构与算法:最短路径算法
Java数据结构与算法:最短路径算法
|
2天前
|
存储 算法 搜索推荐
【数据结构和算法】--- 基于c语言排序算法的实现(2)
【数据结构和算法】--- 基于c语言排序算法的实现(2)
4 0
|
2天前
|
搜索推荐 算法 大数据
​【数据结构与算法】冒泡排序:简单易懂的排序算法解析
​【数据结构与算法】冒泡排序:简单易懂的排序算法解析
|
2天前
|
搜索推荐 算法 C语言
【数据结构和算法】--- 基于c语言排序算法的实现(1)
【数据结构和算法】--- 基于c语言排序算法的实现(1)
12 0
|
2天前
|
算法
【数据结构和算法】--- 二叉树(3)--二叉树链式结构的实现(1)
【数据结构和算法】--- 二叉树(3)--二叉树链式结构的实现(1)
4 0