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 多线程
<li class="tool-item tool-active is-like eye-protector-processed" style="transition: background-color 0.3s ease 0s; background-color: rgb(193, 230, 198);"><a href="javascript:;"><svg class="icon" aria-hidden="true"> <use xlink:href="#csdnc-thumbsup"></use> </svg><span class="name">点赞</span> <span class="count"></span> </a></li> <li class="tool-item tool-active is-collection eye-protector-processed" style="transition: background-color 0.3s ease 0s; background-color: rgb(193, 230, 198);"><a href="javascript:;" data-report-click="{"mod":"popu_824"}"><svg class="icon" aria-hidden="true"> <use xlink:href="#icon-csdnc-Collection-G"></use> </svg><span class="name">收藏</span></a></li> <li class="tool-item tool-active is-share eye-protector-processed" style="transition: background-color 0.3s ease 0s; background-color: rgb(193, 230, 198);"><a href="javascript:;" data-report-click="{"mod":"1582594662_002"}"><svg class="icon" aria-hidden="true"> <use xlink:href="#icon-csdnc-fenxiang"></use> </svg>分享</a></li> <!--打赏开始--> <!--打赏结束--> <li class="tool-item tool-more"> <a> <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg> </a> <ul class="more-box eye-protector-processed" style="transition: background-color 0.3s ease 0s; border-color: rgba(0, 0, 0, 0.35); background-color: rgb(193, 230, 198);"> <li class="item"><a class="article-report">文章举报</a></li> </ul> </li> </ul> </div> </div> <div class="person-messagebox eye-protector-processed" style="border-top-color: rgba(0, 0, 0, 0.35);"> <div class="left-message"><a href="https://blog.csdn.net/a1032818891"> <img src="https://profile.csdnimg.cn/5/2/5/3_a1032818891" class="avatar_pic" username="a1032818891"> <img src="https://g.csdnimg.cn/static/user-reg-year/1x/7.png" class="user-years"> </a></div> <div class="middle-message"> <div class="title"><span class="tit"><a href="https://blog.csdn.net/a1032818891" data-report-click="{"mod":"popu_379"}" target="_blank">David在学习</a></span> </div> <div class="text"><span>发布了35 篇原创文章</span> · <span>获赞 8</span> · <span>访问量 3万+</span></div> </div> <div class="right-message"> <a href="https://im.csdn.net/im/main.html?userName=a1032818891" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter eye-protector-processed" style="transition: background-color 0.3s ease 0s; background-color: rgb(193, 230, 198);">私信 </a> <a class="btn btn-sm bt-button personal-watch" data-report-click="{"mod":"popu_379"}">关注</a> </div> </div> </div>