赞
踩
public class TestBubbleSort1 { public static void main(String[] args) { int [] arr = {75,87,56,45,89,100,76,34,89,97}; // n个元素排序,最多比较n-1次 for (int i = 0; i < arr.length - 1; i++) { // 假设有序 boolean flag = true; for (int j = 0; j < arr.length - 1 -i; j++) { // 前一个比后一个大 if(arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; // 发生交换了,数组无序 flag = false; } } // 有序,直接退出循环 if(flag) break; } System.out.println(Arrays.toString(arr)); // [34, 45, 56, 75, 76, 87, 89, 89, 97, 100] } }
public class TestSelectSort { public static void main(String[] args) { int [] scoreArr = {75,87,56,45,89,100,76,34,89,97}; // scoreArr.length个元素排序,需要比较scoreArr.length-1趟 for (int i = 0; i < scoreArr.length - 1; i++) { // 第i趟,假设第i个最小 int minIndex = i; // 从第i+1个数开始比较,使用第i和每个数相比较,比到最后 for (int j = i + 1; j < scoreArr.length; j++) { // 找到了更小的数,更新第i个的最小值 if(scoreArr[minIndex] > scoreArr[j]) { minIndex = j; } } // 一趟比较完成后,如果最小值的索引改变了,则交换 if(minIndex != i) { int temp = scoreArr[i]; scoreArr[i] = scoreArr[minIndex]; scoreArr[minIndex] = temp; } } System.out.println(Arrays.toString(scoreArr)); // [34, 45, 56, 75, 76, 87, 89, 89, 97, 100] } }
public class TestRecursion1 {
public static void main(String[] args) {
int n = 6;
int result = fac(n);
System.out.println(result); // 720
}
public static int fac(int n) {
int result;
if(n == 1) result = 1;
else result = n * fac(n - 1);
return result;
}
}
F(1) = 1, F(2) = 1, F(n) = F(n - 1) + F(n - 2)
public class TestRecursion2 {
public static void main(String[] args) {
int n = 40;
int result = fibo(n);
System.out.println(result); // 102334155
}
public static int fibo(int n) {
int result = 0;
if(n == 1 || n == 2) result = 1;
else result = fibo(n - 1) + fibo(n - 2);
return result;
}
}
折半查找又称二分查找,使用折半查找的查找表必须使用顺序存储结构且按关键字大小有序排列
public class BinarySearch { public static void main(String[] args) { int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; System.out.println(searchLoop(array, 8)); // 7 } public static int searchLoop(int[] array, int findValue) { if(array == null) return -1; int start = 0; int end = array.length - 1; while(start <= end) { // 中间位置 int middle = (start + end) / 2; // 中间值 int middleValue = array[middle]; if(findValue == middleValue) return middle; // 小于中间值,在中间位置前面找 else if(findValue < middleValue) end = middle - 1; // 大于中间值,在中间位置后面找 else start = middle + 1; } // 没有找到,返回-1 return -1; } }
public class BinarySearch2 { public static void main(String[] args) { int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; System.out.println(binSearch(array, 8)); // 7 } public static int binSearch(int[] array, int key) { int start = 0; int end = array.length - 1; return binSearch(array, start, end, key); } public static int binSearch(int[] array, int start, int end, int key) { int mid = (start + end) / 2; if(start > end) return -1; if(array[mid] == key) return mid; else if(key > array[mid]) return binSearch(array, mid + 1, end, key); else return binSearch(array, start, mid - 1, key); } }
java集合框架提供了一套性能优良、使用方便的接口和类,位于java.util包中。存放在集合中的数据,被称为元素
import java.util.ArrayList; import java.util.Iterator; public class TestArrayList1 { public static void main(String[] args) { ArrayList list = new ArrayList(); list.add(12); list.add(34); // 加到指定位置,底层发生了元素大量后移 list.add(2, 100); ArrayList list2 = new ArrayList(); list2.addAll(0, list); // 使用增强for循环输出 for (Object elem : list) { Integer i = (Integer)elem; System.out.print(i + "\t"); // 12 34 100 } System.out.println(); // 使用Interator迭代器输出 Iterator i = list.iterator(); // 还有元素,没有就结束循环 while (i.hasNext()) { // 有,就取出 int elem = (Integer)i.next(); System.out.print(elem + "\t"); // 12 34 100 } } }
import java.util.ArrayList; import java.util.Iterator; public class TestArrayList2 { public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<Integer>(); list.add(1); list.add(1, 100); ArrayList<Integer> list2 = new ArrayList<Integer>(); list2.add(3); list2.addAll(0, list); Iterator<Integer> it = list2.iterator(); while (it.hasNext()) { int elem = it.next(); System.out.print(elem + "\t"); // 1 100 3 } } }
import java.util.ArrayList;
public class TestArrayList3 {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.remove(new Integer(2));
list.set(0, 65);
// list.clear()
System.out.println(list.contains(65)); // true
System.out.println(list.toString()); // [65]
list.ensureCapacity(100);
}
}
public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable {
private static final int DEFAULT_CAPACITY = 10;
private static final Object[] EMPTY_ELEMENTDATA = {};
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
transient Object[] elementData;
private int size;
}
public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable {}
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
private static int calculateCapacity(Object[] elementData, int minCapacity) {
if(elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
return Math.max(DEFAULT_CAPACITY, minCapacity);
}
return minCapacity;
}
private void grow(int minCapacity) {
int oldCapacity = elementData.length;
int newCapactiy = oldCapacity + (oldCapacity >> 1);
if(newCapactiy - minCapacity < 0) newCapactiy = minCapacity;
if(newCapactiy - MAX_ARRAY_SIZE > 0) newCapactiy = hugeCapacity(minCapacity);
elementData = Arrays.copyOf(elementData, newCapactiy);
}
public Iterator<E> iterator() {
return new ltr();
}
private class ltr implements Iterator<E> {
}
功能:存储多个学生的分数
import java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class TestLinkedList { public static void main(String[] args) { // 1. 创建一个ArrayList集合对象 // ArrayList<Integer> list = new ArrayList<Integer>(); // LinkedList<Integer> list = new LinkedList<Integer>(); // List<Integer> list = new ArrayList<Integer>(); List<Integer> list = new LinkedList<Integer>(); list.add(1); list.add(new Integer(2)); list.add(0, 3); System.out.println(list.size()); // 3 System.out.println(list); // 3 1 2 list.remove(1); System.out.println(list); // 3 2 System.out.println(list.isEmpty()); // false } }
public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>, Deque<E>, Cloneable, java.io.Serializable {
// 节点的数量
transient int size = 0;
// 指向第一个节点
transient Node<E> first;
// 指向最后一个节点
transient Node<E> last;
public LinkedList() {
}
}
private static class Node<E> {
// 存储节点的数据
E item;
// 指向后一个节点
Node<E> next;
// 指向前一个节点
Node<E> prev;
Node(Node<E> prev, E element, Node<E> next) {
this.item = element;
this.next = next;
this.prev = prev;
}
}
/* * push:入栈 * pop:出栈 * peek:获取栈顶元素 * */ public class TestLinkedList2 { public static void main(String[] args) { Deque<String> deque = new LinkedList<String>(); deque.push("wyb"); deque.push("xz"); System.out.println(deque.size()); // 2 System.out.println(deque.peek()); // xz while (!deque.isEmpty()) { String elem = deque.pop(); System.out.println(elem); // xz wyb } System.out.println(deque.size()); // 0 } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。