java面試-資料結構和演算法
阿新 • • 發佈:2018-11-12
1.排序
1.1 氣泡排序
package sort; /** * Created by david on 2018/8/16 * 氣泡排序 */ public class BubbleSort { private static int[] bubbleSort(int[] a) { int len = a.length; for (int i = 1; i < len - 1; i++) { for (int j = 1; j < len - 1-i; j++) { if (a[j + 1] < a[j]) { swap(a, j + 1, j); } } } return a; } //交換方法 private static void swap(int[] a, int i, int j) { int tmp = a[i]; a[i] = a[j]; a[j] = tmp; } //測試 public static void main(String[] args) { int[] a = {1, 4, 6, 8, 99, 9, 2, 99}; int[] sort = bubbleSort(a); for (int s : sort) { System.out.print(s + " "); } } }
1.2快速排序
package sort; /** * Created by david on 2018/8/16 * 快速排序 * 不穩定,時間複雜度 最理想 O(nlogn) 最差時間O(n^2) */ public class QuickSort { private static int[] quickSort(int[] a, int low, int high) { //中心點 int mid = 0; if (low < high) { mid = partition(a, low, high); quickSort(a, low, mid - 1); quickSort(a, mid + 1, high); } return a; } private static int partition(int[] a, int low, int high) { int b = a[low]; while (low < high) { while (low < high && a[high] >= b) { high--; } a[low] = a[high]; while (low < high && a[low] <= b) { low++; } a[high] = a[low]; } a[low] = b; return low; } //測試 public static void main(String[] args) { int[] a = {1, 14, 6, 8, 99, 9, 2, 99}; int[] sort = quickSort(a, 0, 7); for (int s : sort) { System.out.print(s + " "); } } }
1.3 二分查詢
package sort; /** * Created by david on 2018/8/16 * 查詢前的資料必須是已經排好序的, 然後得到陣列的開始位置start和結束位置end, * 取中間位置mid的資料a[mid]跟待查詢資料key進行比較, 若 a[mid] > key, 則取end = mid - 1; * 若 a[mid] < key, 則取start = mid + 1; 若 a[mid] = key 則直接返回當前mid為查詢到的位置. * 依次遍歷直到找到資料或者最終沒有該條資料 */ public class BinarySearch { public static int binarySearch(int[] a, int key) { int start = 0; int end = a.length - 1; int mid = -1; while (start <= end) { mid = (start + end) / 2; if (a[mid] == key) { return mid; } else if (a[mid] > key) { end = mid - 1; } else if (a[mid] < key) { start = mid + 1; } } return -1; } // 測試 public static void main(String[] args) { int[] a = {1, 4, 6, 8, 99}; int i = binarySearch(a, 99); System.out.println(i); } }
1.3 String與Array轉換
import java.util.Arrays;
/**
* Created by david on 2018/8/16
* String/Array轉換
*/
public class Convert {
public static void main(String[] args) {
String str = "we are family";
//String轉成Array
char[] chars = str.toCharArray();
//排序
Arrays.sort(chars);
//轉成String
String s = Arrays.toString(chars);
//根據index獲得char
char c = str.charAt(7);
//長度
str.length();
int length = chars.length;
//子串
String substring1 = str.substring(1, 4);
String substring2 = str.substring(3);
//int轉string
Integer integer = Integer.valueOf("3");
//string轉int
String value = String.valueOf(3);
}
}
1.4 單鏈表反轉
public class Node {
//為了方便,這兩個變數都使用public,而不用private就不需要編寫get、set方法了。
//存放資料的變數,簡單點,直接為int型
public int data;
//存放結點的變數,預設為null
public Node next;
//構造方法,在構造時就能夠給data賦值
public Node(int data){
this.data = data;
}
}
package link;
/**
* Created by david on 2018/8/16
* 單鏈表反轉
*/
public class NodeRe {
public static void main(String[] args) {
Node head = new Node(0);
Node node1 = new Node(1);
Node node2 = new Node(2);
Node node3 = new Node(3);
head.setNext(node1);
node1.setNext(node2);
node2.setNext(node3);
// 呼叫反轉方法
head = reverse(head);
// 列印反轉後的結果
while (null != head) {
System.out.print(head.getData() + " ");
head = head.getNext();
}
}
public static Node reverse(Node head){
// head看作是前一結點,head.getNext()是當前結點,
// reHead是反轉後新連結串列的頭結點
if(head == null || head.getNext() == null){
// 若為空鏈或者當前結點在尾結點,則直接還回
return head;
}
Node reHead = reverse(head.getNext());
// 將當前結點的指標域指向前一結點
head.getNext().setNext(head);
// 前一結點的指標域令為null;
head.setNext(null);
// 反轉後新連結串列的頭結點
return reHead;
}
}
1.5 雙向連結串列反轉
1.6 多執行緒