线性表
插入
public void insert(int i, Object x) throws Exception { if (curLen == listElem.length) // 判断顺序表是否已满 throw new Exception("顺序表已满");// 输出异常 if (i < 0 || i > curLen) // i小于0或者大于表长 throw new Exception("插入位置不合理");// 输出异常 for (int j = curLen; j > i; j--) listElem[j] = listElem[j - 1];// 插入位置及之后的元素后移 listElem[i] = x; // 插入x curLen++;// 表长度增1 }
删除
public void remove(int i) throws Exception { if (i < 0 || i > curLen - 1) // i小于1或者大于表长减1 throw new Exception("删除位置不合理");// 输出异常 for (int j = i; j < curLen - 1; j++) listElem[j] = listElem[j + 1];// 被删除元素之后的元素左移 curLen--; // 表长度减1 }
查找
public int indexOf(Object x) { int j = 0;// j为计数器 while (j < curLen && !listElem[j].equals(x)) // 从顺序表中的首结点开始查找,直到listElem[j]指向元素x或到达顺序表的表尾 j++; if (j < curLen)// 判断j的位置是否位于表中 return j; // 返回x元素在顺序表中的位置 else return -1;// x元素不在顺序表中 }
逆置
public void reverse() { for (int i = 0,j=curLen-1; i < j; i++,j--) { Object temp = listElem[i]; listElem[i] = listElem[j]; listElem[j] = temp; } }
有序表插入
void insert(int x) throws Exception { int i; if (n>MaxSize) //表满不能插入 throw new Exception("Overflow"); r[0]=x; for(i=n;r[i]>x;i--) r[i+1]=r[i];//将i位置元素后移 r[i+1]=x;//在位置i+1插入元素x n++;//线性表长度增1 }
单链表
获取
public Object get(int i) throws Exception { Node p = head.next;// 初始化,p指向首结点,j为计数器 int j = 0; while (p != null && j < i) {// 从首结点向后查找,直到p指向第i个元素或p为空 p = p.next;// 指向后继结点 ++j;// 计数器的值增1 } if (j > i || p == null) { // i小于0或者大于表长减1 throw new Exception("第" + i + "个元素不存在");// 输出异常 } return p.data;// 返回元素p }
插入
public void insert(int i, Object x) throws Exception { Node p = head;// 初始化p为头结点,j为计数器 int j = -1; // 第i个结点前驱的位置 while (p != null && j < i - 1) {// 寻找i个结点的前驱 p = p.next; ++j;// 计数器的值增1 } if (j > i - 1 || p == null) // i不合法 throw new Exception("插入位置不合理");// 输出异常 Node s = new Node(x); // 生成新结点 s.next=p.next;// 插入单链表中 p.next=s; }
删除
public void remove(int i) throws Exception { Node p = head;// p指向要删除结点的前驱结点 int j = -1; while (p.next != null && j < i - 1) {// 寻找i个结点的前驱 p = p.next; ++j;// 计数器的值增1 } if (j > i - 1 || p.next == null) { // i小于0或者大于表长减1 throw new Exception("删除位置不合理");// 输出异常 } p.next=p.next.next;// 删除结点 }
查找
public int indexOf(Object x) { Node p = head.next;// 初始化,p指向首结点,j为计数器 int j = 0; while (p != null && !p.data.equals(x)) {// 从单链表中的首结点元素开始查找,直到p.data指向元素x或到达单链表的表尾 p = p.next;// 指向下一个元素 ++j;// 计数器的值增1 } if (p != null)// 如果p指向表中的某一元素 return j;// 返回x元素在顺序表中的位置 else return -1;// x元素不在顺序表中 }
逆置
public void reverse() { if(head==null||head.next==null) return; Node pre = null; Node cur = null; Node next = null; cur = head.next; next = cur.next; cur.next = null; pre = cur; cur = next; while(cur.next != null){ next = cur.next; cur.next = pre; pre = cur; cur = next; } cur.next = pre; head.next = cur; }
二叉链表
class BiTree { private BiTreeNode root;// 树的根结点 public BiTree() {// 构造一棵空树 this.root = null; } public BiTree(BiTreeNode root) {// 构造一棵树 this.root = root; } // 由先根遍历的数组和中根遍历的数组建立一棵二叉树 // 其中参数preOrder是整棵树的 先根遍历,inOrder是整棵树的中根遍历,preIndex是 // 先根遍历从preOrder字符串中的开始位置,inIndex是中根遍历从字符串inOrder中的开始位置,count表示树结点的个数 public BiTree(String preOrder, String inOrder, int preIndex, int inIndex, int count) { if (count > 0) {// 先根和中根非空 char r = preOrder.charAt(preIndex);// 取先根字符串中的第一个元素作为根结点 int i = 0; for (; i < count; i++) // 寻找根结点在中根遍历字符串中的索引 if (r == inOrder.charAt(i + inIndex)) break; root = new BiTreeNode(r);// 建立树的根结点 root.lchild=new BiTree(preOrder, inOrder, preIndex + 1, inIndex, i).root;// 建立树的左子树 root.rchild=new BiTree(preOrder, inOrder, preIndex + i + 1, inIndex + i + 1, count - i - 1).root;// 建立树的右子树 } } // 由标明空子树的先根遍历序列建立一棵二叉树 private static int index = 0;// 用于记录preStr的索引值 public BiTree(String[] preStr) { String c = preStr[index++];// 取出字符串索引为index的字符,且index增1 if (!c.equals("#") ) {// 字符不为# root = new BiTreeNode(c);// 建立树的根结点 root.lchild=new BiTree(preStr).root;// 建立树的左子树 root.rchild=new BiTree(preStr).root;// 建立树的右子树 } else root = null; } // 先根遍历二叉树基本操作的递归算法 public void preRootTraverse(BiTreeNode T) { if (T != null) { System.out.print(T.data); // 访问根结点 preRootTraverse(T.lchild);// 访问左子树 preRootTraverse(T.rchild);// 访问右子树 } } // 先根遍历二叉树基本操作的非递归算法 public void preRootTraverse() { BiTreeNode T = root; if (T != null) { LinkStack S = new LinkStack();// 构造栈 S.push(T);// 根结点入栈 while (!S.isEmpty()) { T = (BiTreeNode) S.pop();// 移除栈顶结点,并返回其值 System.out.print(T.data +" "); // 访问结点 while (T != null) { if (T.lchild != null) // 访问左孩子 System.out.print(T.lchild.data +" "); // 访问结点 if (T.rchild != null)// 右孩子非空入栈 S.push(T.rchild); T = T.lchild; } } } } // 中根遍历二叉树基本操作的递归算法 public void inRootTraverse(BiTreeNode T) { if (T != null) { inRootTraverse(T.lchild);// 访问左子树 System.out.print(T.data); // 访问根结点 inRootTraverse(T.rchild);// 访问右子树 } } // 中根遍历二叉树基本操作的非递归算法 public void inRootTraverse() { BiTreeNode T = root; if (T != null) { LinkStack S = new LinkStack();// 构造链栈 S.push(T); // 根结点入栈 while (!S.isEmpty()) { while (S.peek() != null) // 将栈顶结点的所有左孩子结点入栈 S.push(((BiTreeNode) S.peek()).lchild); S.pop(); // 空结点退栈 if (!S.isEmpty()) { T = (BiTreeNode) S.pop();// 移除栈顶结点,并返回其值 System.out.print(T.data +" "); // 访问结点 S.push(T.rchild);// 结点的右孩子入栈 } } } } // 后根遍历二叉树基本操作的递归算法 public void postRootTraverse(BiTreeNode T) { if (T != null) { postRootTraverse(T.lchild);// 访问左子树 postRootTraverse(T.rchild);// 访问右子树 System.out.print(T.data); // 访问根结点 } } // 后根遍历二叉树基本操作的非递归算法 public void postRootTraverse() { BiTreeNode T = root; if (T != null) { LinkStack S = new LinkStack();// 构造链栈 S.push(T); // 根结点进栈 Boolean flag;// 访问标记 BiTreeNode p = null;// p指向刚被访问的结点 while (!S.isEmpty()) { while (S.peek() != null) // 将栈顶结点的所有左孩子结点入栈 S.push(((BiTreeNode) S.peek()).lchild); S.pop(); // 空结点退栈 while (!S.isEmpty()) { T = (BiTreeNode) S.peek();// 查看栈顶元素 if (T.rchild == null || T.rchild == p) { System.out.print(T.data +" "); // 访问结点 S.pop();// 移除栈顶元素 p = T;// p指向刚被访问的结点 flag = true;// 设置访问标记 } else { S.push(T.rchild);// 右孩子结点入栈 flag = false;// 设置未被访问标记 } if (!flag) break; } } } } // 层次遍历二叉树基本操作的算法(自左向右) public void levelTraverse() { BiTreeNode T = root; if (T != null) { LinkQueue L = new LinkQueue();// 构造队列 L.offer(T);// 根结点入队列 while (!L.isEmpty()) { T = (BiTreeNode) L.poll(); System.out.print(T.data); // 访问结点 if (T.lchild != null)// 左孩子非空,入队列 L.offer(T.lchild); if (T.rchild != null)// 右孩子非空,入队列 L.offer(T.rchild); } } } public BiTreeNode getRoot() { return root; } public void setRoot(BiTreeNode root) { this.root = root; } // 统计叶结点数目 public int countLeafNode(BiTreeNode T) { int count = 0; if (T != null) { if (T.lchild == null && T.rchild == null) { ++count;// 叶结点个数加1 } else { count += countLeafNode(T.lchild); // 加上左子树上叶结点的个数 count += countLeafNode(T.rchild);// 加上右子树上叶结点的个数 } } return count; } // 统计结点的数目 public int countNode(BiTreeNode T) { int count = 0; if (T != null) { ++count;// 结点的个数加1 count += countNode(T.lchild); // 加上左子树上结点的个数 count += countNode(T.rchild);// 加上右子树上结点的个数 } return count; } public int getDepth(BiTreeNode T) { if (T != null) { int lDepth = getDepth(T.lchild);// 左子树的深度 int rDepth = getDepth(T.rchild);// 右子树的深度 return 1 + (lDepth > rDepth ? lDepth : rDepth);// 返回左子树的深度和右子树的深度中的最大值加1 } return 0; } // 采用递归模型方法,计算二叉树的深度 public int getDepth1(BiTreeNode T) { if (T==null) return 0; else if (T.lchild==null&&T.rchild==null) return 1; else return 1 + (getDepth1(T.lchild)> getDepth1(T.rchild)? getDepth1(T.lchild) : getDepth1(T.rchild)); }}
图的邻接矩阵
class MGraph { private int vexNum, arcNum; private Object[] vexs; private int[][] arcs; private static boolean[] visited; public MGraph() { this(0, 0, null, null); } public MGraph(int vexNum, int arcNum, Object[] vexs, int[][] arcs) { this.vexNum = vexNum; this.arcNum = arcNum; this.vexs = vexs; this.arcs = arcs; } public void createGraph(String[] str, int n, int e, int[][] arc){ vexNum = n; arcNum = e; vexs = new Object[vexNum]; this.vexs = str; arcs = new int[vexNum][vexNum]; for (int v = 0; v < vexNum; v++) for (int u = 0; u < vexNum; u++) arcs[v][u] = 0; for (int k = 0; k < arcNum; k++) { int v = arc[k][0]; int u = arc[k][1]; arcs[v][u] = arcs[u][v] = 1; } } public int getVexNum() { return vexNum; } public int getArcNum() { return arcNum; } public void setArcNum(int arcNum) { this.arcNum = arcNum; } public void setArcs(int[][] arcs) { this.arcs = arcs; } public void setVexNum(int vexNum) { this.vexNum = vexNum; } public void setVexs(Object[] vexs) { this.vexs = vexs; } public int[][] getArcs() { return arcs; } public Object[] getVexs() { return vexs; } public int locateVex(Object vex) { for (int v = 0; v < vexNum; v++) if (vexs[v].equals(vex)) return v; return -1; } public Object getVex(int v) throws Exception { if (v < 0 && v >= vexNum) throw new Exception("no position"); return vexs[v]; } public int firstAdjVex(int v) throws Exception { if (v < 0 && v >= vexNum) throw new Exception("!"); for (int j = 0; j < vexNum; j++) if (arcs[v][j] != 0 && arcs[v][j] < 9999) return j; return -1; } public int nextAdjVex(int v, int w) throws Exception { if (v < 0 && v >= vexNum) throw new Exception("no position"); for (int j = w + 1; j < vexNum; j++) if (arcs[v][j] != 0 && arcs[v][j] < 9999) return j; return -1; } public void DFSTraverse(MGraph G) throws Exception { visited = new boolean[G.getVexNum()]; for (int v = 0; v < G.getVexNum(); v++) visited[v] = false; for (int v = 0; v < G.getVexNum(); v++) if (!visited[v]) DFS(G, v); } private void DFS(MGraph G, int v) throws Exception { visited[v] = true; System.out.print(G.getVex(v).toString() + " "); for (int w = G.firstAdjVex(v); w >= 0; w = G.nextAdjVex(v, w)) if (!visited[w]) DFS(G, w); }}public class Main { public static Scanner sc; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); String a[] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N" }; int i, n, e, j; n = sc.nextInt(); e = sc.nextInt(); int[][] arc = new int[e][2]; for (i = 0; i < e; i++) for (j = 0; j < 2; j++) arc[i][j] = sc.nextInt(); MGraph G = new MGraph(); G.createGraph(a, n, e, arc); System.out.print("DFS:"); G.DFSTraverse(G); System.out.println(); }}
邻接表
class ALGraph implements IGraph { private int vexNum, arcNum; private VNode[] vexs; private static boolean[] visited;// 访问标志数组 public ALGraph() { this(0, 0, null); } public ALGraph(int vexNum, int arcNum, VNode[] vexs) { this.vexNum = vexNum; this.arcNum = arcNum; this.vexs = vexs; } public void createGraph() { Scanner sc = new Scanner(System.in); vexNum = sc.nextInt(); arcNum = sc.nextInt(); vexs = new VNode[vexNum]; String Node[]={"A","B","C","D","E","F","G","H","I","J","K","L","M","N"}; //顶点信息 for (int v = 0; v < vexNum; v++) vexs[v] = new VNode(Node[v]); for (int k = 0; k < arcNum; k++) { int v = sc.nextInt();// 弧尾 int u = sc.nextInt();// 弧头 int value = 1; addArc(v, u, value); addArc(u, v, value); } } public void addArc(int v, int u, int value) { ArcNode arc = new ArcNode(u, value); arc.nextArc=vexs[v].firstArc; vexs[v].firstArc=arc; } public int getVexNum() { return vexNum; } public int getArcNum() { return arcNum; } public int locateVex(Object vex) { for (int v = 0; v < vexNum; v++) if (vexs[v].data.equals(vex)) return v; return -1; } public VNode[] getVexs() { return vexs; } public Object getVex(int v) throws Exception { if (v < 0 && v >= vexNum) throw new Exception("第" + v + "个顶点不存在!"); return vexs[v].data; } public int firstAdjVex(int v) throws Exception { if (v < 0 && v >= vexNum) throw new Exception("第" + v + "个顶点不存在!"); VNode vex = vexs[v]; if (vex.firstArc != null) return vex.firstArc.adjVex; else return -1; } public int nextAdjVex(int v, int w) throws Exception { if (v < 0 && v >= vexNum) throw new Exception("第" + v + "个顶点不存在!"); VNode vex = vexs[v]; ArcNode arcvw = null; for (ArcNode arc = vex.firstArc; arc != null; arc = arc.nextArc) if (arc.adjVex == w) { arcvw = arc; break; } if (arcvw != null && arcvw.nextArc != null) return arcvw.nextArc.adjVex; else return -1; } public void setArcNum(int arcNum) { this.arcNum = arcNum; } public void setVexNum(int vexNum) { this.vexNum = vexNum; } public void setVexs(VNode[] vexs) { this.vexs = vexs; } public void DFSTraverse() throws Exception { visited = new boolean[getVexNum()]; for (int v = 0; v < getVexNum(); v++) // 访问标志数组初始化 visited[v] = false; System.out.print("DFS:"); for (int v = 0; v < getVexNum(); v++) if (!visited[v])// 对尚未访问的顶点调用DFS DFS(v); } private void DFS(int v) throws Exception { visited[v] = true; // 访问第v个顶点 System.out.print(getVex(v).toString() + " "); for (int w = firstAdjVex(v); w >= 0; w = nextAdjVex(v, w)) if (!visited[w])// 对v的尚未访问的邻接顶点w递归调用DFS DFS(w); } public void BFSTraverse() throws Exception { visited = new boolean[getVexNum()];// 访问标志数组 for (int v = 0; v < getVexNum(); v++) // 访问标志数组初始化 visited[v] = false; System.out.print("BFS:"); for (int v = 0; v < getVexNum(); v++) if (!visited[v]) // v尚未访问 BFS(v); } private void BFS(int v) throws Exception { visited[v] = true; System.out.print(getVex(v).toString() + " "); CirQueue Q = new CirQueue();// 辅助队列Q Q.EnQueue(v);// v入队列 while (!Q.isEmpty()) { int u = (Integer) Q.DeQueue();// 队头元素出队列并赋值给u for (int w = firstAdjVex(u); w >= 0; w = nextAdjVex(u, w)) if (!visited[w]) {// w为u的尚未访问的邻接顶点 visited[w] = true; System.out.print(getVex(w).toString() + " "); Q.EnQueue(w); } } } void DispMGraph(){ int i; ArcNode p; System.out.println("Graph adjlist:"); for(i=0;i<vexNum;i++){ System.out.print(i + " " +vexs[i].data + " "); p = vexs[i].firstArc; while(p!=null){ System.out.print(p.adjVex + " "); p= p.nextArc; } System.out.println(""); } }}
折半查找
插入
oid insert(int x) throws Exception { int i; if (n>MaxSize) //表满不能插入 throw new Exception("Overflow"); r[0]=x; for(i=n;r[i]>x;i--) r[i+1]=r[i];//将i位置元素后移 r[i+1]=x;//在位置i+1插入元素x n++;//线性表长度增1 }
查找
int Bin_Search(int key){ if(n>0){ int low = 0,high = n; while(low<=high){ int mid = (low+high)/2; if(r[mid]==key){ return mid; }else if(r[mid]>key){ high = mid - 1; }else{ low = mid + 1; } } } return -1; }
查找(递归)
int Bin_Search(int key){ return Bin_Search(1,n,key); } int Bin_Search(int low,int high,int key){ if(low>high) return -1; int mid = (low + high) >> 1; if(r[mid] > key) return Bin_Search(low, mid - 1, key); else if(r[mid] < key) return Bin_Search(mid + 1, high, key); return mid; }
二叉排序树(1)-递归方式
public class BSTree { //二叉排序树类 public BiTreeNode root; //根结点 public BSTree() { //构造空二叉排序树 root = null; }/* public BiTreeNode getRoot() { return root; } public void setRoot(BiTreeNode root) { this.root = root; }*/ public boolean isEmpty() { //判断是否空二叉树 return this.root == null; } public void inOrderTraverse(BiTreeNode p) { //中根次序遍历以p结点为根的二叉树 if (p != null) { inOrderTraverse(p.lchild); System.out.print(((RecordNode) p.data).toString() + ""); inOrderTraverse(p.rchild); } } //查找关键字值为key的结点,若查找成功返回结点值,否则返回null public Object searchBST(Comparable key) { if (key == null || !(key instanceof Comparable)) { return null; } return searchBST(root, key); } //二叉排序树查找的递归算法 //在二叉排序树中查找关键字为key的结点。若查找成功则返回结点值,否则返回null private Object searchBST(BiTreeNode p, Comparable key) { if (p != null) { if (key.compareTo(((RecordNode) p.data).key) == 0) //查找成功 { return p.data; } // System.out.print(((RecordNode) p.data).key + "? "); if (key.compareTo(((RecordNode) p.data).key) < 0) { return searchBST(p.lchild, key); //在左子树中查找 } else { return searchBST(p.rchild, key); //在右子树中查找 } } return null; } //在二叉排序树中插入关键字为Key,数据项为theElement的结点,若插入成功返回true,否则返回false public boolean insertBST(Comparable key, Object theElement) { if (key == null || !(key instanceof Comparable)) {//不能插入空对象或不可比较大小的对象 return false; } if (root == null) { root = new BiTreeNode(new RecordNode(key, theElement)); //建立根结点 return true; } return insertBST(root, key, theElement); } //将关键字为key,数据项为theElement的结点插入到以p为根的二叉排序树中的递归算法 private boolean insertBST(BiTreeNode p, Comparable key, Object theElement) { if (key.compareTo(((RecordNode) p.data).key) == 0) { return false; //不插入关键字重复的结点 } if (key.compareTo(((RecordNode) p.data).key) < 0) { if (p.lchild == null) { //若p的左子树为空 p.lchild=new BiTreeNode(new RecordNode(key, theElement)); //建立叶子结点作为p的左孩子 return true; } else { //若p的左子树非空 return insertBST(p.lchild, key, theElement); //插入到p的左子树中 } } else if (p.rchild == null) { //若p的右子树为空 p.rchild=new BiTreeNode(new RecordNode(key, theElement)); //建立叶子结点作为p的右孩子 return true; } else { //若p的右子树非空 return insertBST(p.rchild, key, theElement); //插入到p的右子树中 } } //二叉排序树中删除结点算法。若删除成功返回删除结点值,否则返回null public Object removeBST(Comparable key) { if (root == null || key == null || !(key instanceof Comparable)) { return null; } //在以root为根的二叉排序树中删除关键字为elemKey的结点 return removeBST(root, key, null); } //在以p为根的二叉排序树中删除关键字为elemKey的结点。parent是p的父结点,递归算法 private Object removeBST(BiTreeNode p, Comparable elemKey, BiTreeNode parent) { if (p != null) { if (elemKey.compareTo(((RecordNode) p.data).key) < 0) { //在左子树中删除 return removeBST(p.lchild, elemKey, p); //在左子树中递归搜索 } else if (elemKey.compareTo(((RecordNode) p.data).key) > 0) { //在右子树中删除 return removeBST(p.rchild, elemKey, p); //在右子树中递归搜索 } else if (p.lchild != null && p.rchild != null) { //相等且该结点有左右子树 BiTreeNode innext = p.rchild; //寻找p在中根次序下的后继结点innext while (innext.lchild != null) { //即寻找右子树中的最左孩子 innext = innext.lchild; } p.data=innext.data; //以后继结点值替换p return removeBST(p.rchild, ((RecordNode) p.data).key, p); //递归删除结点p } else {//p是1度和叶子结点 if (parent == null) { //删除根结点,即p==root if (p.lchild != null) { root = p.lchild; } else { root = p.rchild; } return p.data; //返回删除结点p值 } if (p == parent.lchild) { //p是parent的左孩子 if (p.lchild != null) { parent.lchild=p.lchild; //以p的左子树填补 } else { parent.lchild=p.rchild; } } else if (p.lchild != null) { //p是parent的右孩子且p的左子树非空 parent.rchild=p.lchild; } else { parent.rchild=p.rchild; } return p.data; } } return null; }}
排序
不带监视哨的直接插入排序算法
public void insertSort() { RecordNode temp; int i, j; for (i = 1; i < this.curlen; i++) {//n-1趟扫描 temp = r[i]; //将待插入的第i条记录暂存在temp中 for (j = i - 1; j >= 0 && temp.key.compareTo(r[j].key) < 0; j--) { //将前面比r[i]大的记录向后移动 r[j + 1] = r[j]; } r[j + 1] = temp; //r[i]插入到第j+1个位置 display(); } }
带监视哨的直接插入排序算法
public void insertSortWithGuard() { int i, j; for (i = 2; i <this.curlen; i++) { //n-1趟扫描 r[0] = r[i]; //将待插入的第i条记录暂存在r[0]中,同时r[0]为监视哨 for (j = i - 1; r[0].key.compareTo(r[j].key) < 0; j--) { //将前面较大元素向后移动 r[j + 1] = r[j]; } r[j + 1] = r[0]; // r[i]插入到第j+1个位置 display(); } }
冒泡排序算法
public void bubbleSort() { // System.out.println("冒泡排序"); RecordNode temp; //辅助结点 boolean flag = true; //是否交换的标记 for (int i = 1; i < this.curlen && flag; i++) { //有交换时再进行下一趟,最多n-1趟 flag = false; //假定元素未交换 for (int j = 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; } } display(); } }
快速排序算法
//交换排序表r[i..j]的记录,使支点记录到位,并返回其所在位置 //此时,在支点之前(后)的记录关键字均不大于(小于)它 public int Partition(int i, int j) { RecordNode pivot = r[i]; //第一个记录作为支点记录 // System.out.print(i + ".." + j + ", pivot=" + pivot.key + " "); while (i < j) { //从表的两端交替地向中间扫描 while (i < j && pivot.key.compareTo(r[j].key) <= 0) { j--; } if (i < j) { r[i] = r[j]; //将比支点记录关键字小的记录向前移动 i++; } while (i < j && pivot.key.compareTo(r[i].key) > 0) { i++; } if (i < j) { r[j] = r[i]; //将比支点记录关键字大的记录向后移动 j--; } } r[i] = pivot; //支点记录到位 // display(); return i; //返回支点位置 } //对子表r[low..high]快速排序 public void qSort(int low, int high) { if (low < high) { int pivotloc = Partition(low, high); //一趟排序,将排序表分为两部分 qSort(low, pivotloc - 1); //低子表递归排序 qSort(pivotloc + 1, high); //高子表递归排序 } } public void quickSort() { qSort(0, this.curlen - 1); }
直接选择排序
public void selectSort() { RecordNode temp; //辅助结点 for (int i = 0; i < this.curlen - 1; i++) {//n-1趟排序 //每趟在从r[i]开始的子序列中寻找最小元素 int min = i; //设第i条记录的关键字最小 for (int j = i + 1; j < this.curlen; j++) {//在子序列中选择关键字最小的记录 if (r[j].key.compareTo(r[min].key) < 0) { min = j; //记住关键字最小记录的下标 } } if (min != i) { //将本趟关键字最小的记录与第i条记录交换 temp = r[i]; r[i] = r[min]; r[min] = temp; } } }
扩展
散列表
class HashList{ int MaxSize = 11; int[] ht = new int[MaxSize]; int HashFunc(int k) { return k%MaxSize; } public HashList() { for(int i=0;i<MaxSize;i++) ht[i]=-1; } void Display(){ for (int i=0;i<MaxSize;i++) System.out.print(ht[i]+" "); System.out.println(); } int HashSearch(int k) throws Exception { int j = HashFunc(k); if (ht[j] == k) return 1; else if (ht[j] == -1) ht[j] = k; else { int i = (j + 1) % MaxSize; int cnt = 1; while (ht[i] != -1 && i != j) { cnt++; if (ht[i] == k) { return cnt; } else i = (i + 1) % MaxSize; } if(i == j) throw new Exception("Overflow"); else { ht[i] = k; } } return -1; } double HashASL() throws Exception { double ASL=0; int n=0; for(int i=0;i<MaxSize;i++) if(ht[i]!=-1) { ASL+=HashSearch(ht[i]); System.out.println(ht[i] + " " + HashSearch(ht[i])); n++; } return (double)Math.round(ASL/n*100000)/100000; }}class LinkHash{ int MaxSize = 11; Node[] ht = new Node[MaxSize]; public LinkHash() { for (int i = 0; i < MaxSize; i++) ht[i] = null; //NULL is empty } void Display() { for (int i = 0; i < MaxSize; i++) { System.out.print("Hash address:" + i + ",value:"); for (Node p = ht[i]; p != null; p = p.next) System.out.print(p.data + " "); System.out.println(); } } int HashFunc(int k){ return k % 11; //hash函数,假设为除余法,余数为11 } int HashSearch(int k){ int j = HashFunc(k); Node p = ht[j]; while(p!=null && p.data != k) { p = p.next; } if(p!=null && p.data == k) return 1; else { Node s = new Node(); s.data = k; s.next = ht[j]; ht[j] = s; } return 0; }}
栈
class SeqStack{ int stackMax = 5; Object[] data; int top = 0; public SeqStack() { data = new Object[stackMax]; top = 0; } public void push(Object x) throws Exception { if(top==stackMax){ throw new Exception("Overflow"); }else{ data[top++] = x; } } public Object pop() throws Exception { if(top==0){ throw new Exception("Downflow"); }else { return data[--top]; } } public Object getTop() throws Exception { if(top==0){ throw new Exception("Downflow"); }else { return data[top - 1]; } } public boolean isEmpty(){ return top==0; }}class LinkStack{ Node top; public LinkStack() { top = null; } public void push(Object x){ top = new Node(x,top); } public Object pop() throws Exception { if(isEmpty()){ throw new Exception("Downflow"); }else { Node p = top; top = top.next; return p.data; } } public Object getTop() throws Exception { if(isEmpty()){ throw new Exception("Downflow"); }else { return top.data; } } public boolean isEmpty(){ return top==null; }}
队列
class LinkQueue{ Node front; Node rear; public LinkQueue(){ rear = front = null; } void EnQueue(Object data){ Node p =new Node(data); if(front != null){ rear.next = p; rear = p; }else{ front = rear = p; } } Object DeQueue() throws Exception{ if(front != null){ Node p = front; front = front.next; if(p==rear){ rear = null; } return p.data; }else{ throw new Exception("Downflow"); } } Object GetQueue() throws Exception{ if(front != null){ return front.data; }else{ throw new Exception("Downflow"); } } boolean isEmpty(){ return front == null; }}class CirQueue{ Object[] queueElem; int front; int rear; int QueueSize=5; public CirQueue(){ front = rear = 0; queueElem = new Object[QueueSize]; } public void clear(){ front = rear = 0; } public boolean isEmpty(){ return front == rear; } public int length(){ return (rear-front+queueElem.length)% queueElem.length; } public boolean isFull(){ return (rear+1) % QueueSize ==front; } public void EnQueue(Object x) throws Exception{ if((rear+1)%queueElem.length==front){ throw new Exception(" Overflow"); }else{ queueElem[rear] = x; rear = (rear+1)%queueElem.length; } } public Object DeQueue() throws Exception { if(front==rear){ throw new Exception("Downflow"); }else{ Object t = queueElem[front]; front = (front+1)%queueElem.length; return t; } } public Object GetQueue() throws Exception { if(front==rear){ throw new Exception("Downflow"); }else{ Object t = queueElem[front]; return t; } }}