【数据结构】24种常见算法题 您所在的位置:网站首页 数据结构算法题 【数据结构】24种常见算法题

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

#【数据结构】24种常见算法题| 来源: 网络整理| 查看: 265

补全下面顺序表的插入操作算法代码:

public void insert(int i, Object x) {

//0.1 满校验:存放实际长度 和 数组长度 一样

if(curLen == listEle.length) {

throw new Exception("已满");

}

//0.2 非法校验,在已有的数据中间插入 [0, curLen],必须连续,中间不能空元素

if(i < 0 || i > curLen ) throw new Exception("位置非法");

//1 将i及其之后后移

for(int j = curLen ; j > i; j --) {

listEle[j] = listEle[j-1];

}

//2 插入i处

listEle[i] = x;

//3 记录长度

curLen ++;

}

补全顺序表的删除算法代码

public void remove(int i ) throws Exception {

// 0.1 校验非法数据

if(i < 0 || i > curLen – 1 ) {

throw new Exception("位置非法");

}

// 1 将i之后向前移动

for(int j = i ; j < curLen - 1 ; j ++ ) {

listEle[j] = listEle[j+1];

}

// 2 长度减一

curLen--;

}

补全顺序表的查找算法1代码

循环遍历已有数据,进行判断,如果有返回第一个索引号,如果没有返回-1

public int indexOf(Object x) {

for(int i = 0; i < curLen ; i ++) {

if( listEle[i].equals(x) ) {

return i;

}

}

return -1;

}

补全顺序表的查找算法2代码:

使用变量记录没有匹配到索引

public int indexOf(Object x) {

int j = 0; //用于记录索引信息

while(j < curLen && !listElem[j].equals(x) ) j++;

// j记录索引小于数量

if(j < curLen ) {

return j;

} else {

return -1

}

}

补全单链表长度算法:

public class Node{

public Object data; //数据域

public Node next; //指针域

}

public int length() {

Node p = head.next; // 获得第一个结点

int length = 0; // 定义一个变量记录长度

while(p != null) {

length ++; //计数

p = p.next; //p指向下一个结点

}

return length;

}

补全单链表按索引号(位序号)查找算法代码:

public class Node{

public Object data; //数据域

public Node next; //指针域

}

public Object get(int i) {

Node p = head.next; //获得头结点

int j = 0; //已经处理的结点数

while(p != null && j end) {

throw new StringIndexOutOfBoundsException("条件不合法");

}

// 2 优化:当前串直接返回

if(begin == 0 && end == curlen) return this;

// 3 核心算法

char[] buffer = new char[end - begin]; // 构建新数组

for(int i = 0 ; i < buffer.length ; i ++) { // 依次循环遍历新数组,一个一个赋值

buffer[i] = strvalue[i + begin];

}

return new SeqString(buffer); // 使用字符数组构建一个新字符串

}

串删除算法代码补全

public IString delete(int begin , int end) {

// 1 参数校验

if(begin < 0 || end > curlen || begin > end) {

throw new StringIndexOutOfBoundsException("条件不合法");

}

// 2 核心:将后面内容移动到签名

// 2.1 移动

for(int i = 0 ; i < curlen - end ; i ++) {

strvalue[i + begin] = strvalue[i + end];

}

// 2.2 重新统计长度 (end-begin 需要删除串的长度)

curlen = curlen - (end-begin)

return this;

}

n!算法代码补全(n=10):

public class TestFactorial {

public static void main(String[] args) {

System.out.println(factorial(4));

}

private static int factorial(int n) {

if(n == 1 ) { 【代码1】

return 1;

}

return n * factorial(n-1); 【代码2】

}

}

不带监视哨的插入排序算法

public void insertSort() {

RecordNode temp;

int i, 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();

}

}

带监视哨的插入排序算法

public void insertSortWithGuard() {

int i, j;

for (i = 1; i 0) {

temp = r[j];

r[j] = r[j + 1];

r[j + 1] = temp;

flag = true;

}

}

System.out.print("第" + i + "趟: ");

display();

}

}

直接选择排序算法

public void selectSort() {

RecordNode temp;

for (int i = 0; i < this.curlen - 1; i++) {

int min = i;

for (int j = 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;

}

}

}

二路归并算法

public void mergepass(RecordNode[] r, RecordNode[] order, int s, int n) {……}//一趟归并算法

public void mergeSort() {

int s = 1;

int n = this.curlen;

RecordNode[] temp = new RecordNode[n];

while (s < n) {

mergepass(r, temp, s, n);

s *= 2;

mergepass(temp, r, s, n);

s *= 2;

}

}

补全带监视哨的顺序查找算法代码

public int seqSearchWithGuard(Comparable key) {

int i = length() - 1;

r[0].key = key;

while(r[i].key.compareTo(key) != 0) {

i--;

}

return i>0 ? i : -1;

}

补全顺序查找代码

public int seqSearch(Comparable key) {

int i = 0 , n = length();

while(i < n && r[i].key.compareTo(key) != 0) {

i++;

}

return i < n ? i : -1

}

补全二分查找算法代码

public int binarySearch(Comparable key) {

if(length() > 0) {

int low = 0, high = length() - 1;

while (low 0) { // 中间值比给定值大

high = mid - 1;

} else {

low = mid + 1;

}

}

}

return -1;

}

补全二叉排序树的递归算法代码

private Object searchBST(BiTreeNode p, Comparable key) {

if (p != null) {

if (key.compareTo(((RecordNode) p.data).key) == 0) {

return p.data;

}

if (key.compareTo(((RecordNode) p.data).key) < 0) {

return searchBST(p.lchild, key);

} else {

return searchBST(p.rchild, key);

}

}

return null;

}



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有