parent
4562f8e0c1
commit
4305177015
@ -0,0 +1,53 @@
|
|||||||
|
package maslaos.shejimos;
|
||||||
|
|
||||||
|
public class Person {
|
||||||
|
int a;
|
||||||
|
int b;
|
||||||
|
int c;
|
||||||
|
int d;
|
||||||
|
|
||||||
|
private Person(){
|
||||||
|
A a = new A(){};
|
||||||
|
|
||||||
|
}
|
||||||
|
static class PersonBuilder{
|
||||||
|
Person p =new Person();
|
||||||
|
|
||||||
|
PersonBuilder basic(int a,int b){
|
||||||
|
p.a=a;
|
||||||
|
p.b=b;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
PersonBuilder c(int c){
|
||||||
|
p.c=c;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
PersonBuilder d(int d){
|
||||||
|
p.d=d;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
Person builder(){
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract class A{
|
||||||
|
// abstract void m();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Person person = new PersonBuilder()
|
||||||
|
.basic(1, 2)
|
||||||
|
.c(3)
|
||||||
|
.d(4)
|
||||||
|
.builder();
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,55 @@
|
|||||||
|
package maslaos.shejimos;
|
||||||
|
|
||||||
|
|
||||||
|
public class Visitor {
|
||||||
|
|
||||||
|
class DianRao{
|
||||||
|
Memory m;
|
||||||
|
Cpu cpu;
|
||||||
|
public DianRao(Vi v){
|
||||||
|
m.accpect(v);
|
||||||
|
cpu.accpect(v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
abstract class DianRaoPart{
|
||||||
|
abstract void accpect(Vi v);
|
||||||
|
}
|
||||||
|
|
||||||
|
class Memory extends DianRaoPart{
|
||||||
|
@Override
|
||||||
|
void accpect(Vi v) {
|
||||||
|
v.accpectMemory(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Cpu extends DianRaoPart{
|
||||||
|
@Override
|
||||||
|
void accpect(Vi v) {
|
||||||
|
v.accpectCpu(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
interface Vi{
|
||||||
|
void accpectCpu(Cpu c);
|
||||||
|
void accpectMemory(Memory m);
|
||||||
|
}
|
||||||
|
|
||||||
|
class Avi implements Vi{
|
||||||
|
@Override
|
||||||
|
public void accpectCpu(Cpu c) {
|
||||||
|
//我在你之前
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void accpectMemory(Memory m) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
package zuolaos.jichuban;
|
||||||
|
|
||||||
|
public interface A {
|
||||||
|
void a();
|
||||||
|
}
|
@ -0,0 +1,196 @@
|
|||||||
|
package zuolaos.jichuban;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.lang.reflect.Proxy;
|
||||||
|
|
||||||
|
public class Code10_各种排序 {
|
||||||
|
|
||||||
|
//归并排序-递归
|
||||||
|
public static void guiBing(int[] arr) {
|
||||||
|
if (arr == null || arr.length < 2) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
guiBingProcess(arr, 0, arr.length - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//归并排序-迭代 内部merge
|
||||||
|
public static void guiBing2(int[] arr) {
|
||||||
|
if (arr == null || arr.length < 2) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int step = 1;
|
||||||
|
int N = arr.length;
|
||||||
|
while (true) {
|
||||||
|
int L = 0;
|
||||||
|
int R = 0;
|
||||||
|
int mid = 0;
|
||||||
|
for (int i = 0; i < N; i += step) {
|
||||||
|
L = i;
|
||||||
|
R = (i + step - 1) < N ? (i + step - 1) : (N - 1);
|
||||||
|
mid = L + (step >> 1) - 1;
|
||||||
|
// mid = L + ((R - L) >> 1);
|
||||||
|
merge(arr, L, mid, R);
|
||||||
|
}
|
||||||
|
if (step >= N) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
step <<= 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//归并排序-迭代 外部左右merge
|
||||||
|
public static void guiBing3(int[] arr) {
|
||||||
|
if (arr == null || arr.length < 2) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int step = 1;
|
||||||
|
int N = arr.length;
|
||||||
|
while (step < N) {
|
||||||
|
int L = 0;
|
||||||
|
while (L < N) {
|
||||||
|
int mid = 0;
|
||||||
|
if (L < N - step - 1) {
|
||||||
|
mid = L + step - 1;
|
||||||
|
} else {
|
||||||
|
mid = N - 1;
|
||||||
|
}
|
||||||
|
int R = 0;
|
||||||
|
if (mid == N - 1) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (mid + step < N) {
|
||||||
|
R = mid + step;
|
||||||
|
} else {
|
||||||
|
R = N - 1;
|
||||||
|
}
|
||||||
|
merge(arr, L, mid, R);
|
||||||
|
L = R + 1;
|
||||||
|
}
|
||||||
|
if (step > (N << 1)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
step <<= 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static void guiBingProcess(int[] arr, int L, int R) {
|
||||||
|
if (L == R) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int mid = L + ((R - L) >> 1);
|
||||||
|
guiBingProcess(arr, L, mid);
|
||||||
|
guiBingProcess(arr, mid + 1, R);
|
||||||
|
merge(arr, L, mid, R);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void swap(int[] arr, int i, int j) {
|
||||||
|
int tmp = arr[i];
|
||||||
|
arr[i] = arr[j];
|
||||||
|
arr[j] = tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//快速排序
|
||||||
|
public static void quickSort(int[] arr) {
|
||||||
|
if (arr == null || arr.length < 2) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
quickSortProcess(arr, 0, arr.length - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void quickSortProcess(int[] arr, int L, int R) {
|
||||||
|
if (L >= R) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int[] equlas = partition(arr, L, R);
|
||||||
|
quickSortProcess(arr, L, equlas[0] - 1);
|
||||||
|
quickSortProcess(arr, equlas[1] + 1, R);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//快速排序 - 核心方法
|
||||||
|
public static void split(int[] arr) {
|
||||||
|
int N = arr.length;
|
||||||
|
int lessXR = -1;
|
||||||
|
int moreXL = N - 1;
|
||||||
|
int index = 0;
|
||||||
|
while (index < moreXL) {
|
||||||
|
if (arr[index] < arr[N - 1]) {
|
||||||
|
lessXR++;
|
||||||
|
swap(arr, lessXR, index);
|
||||||
|
index++;
|
||||||
|
} else if (arr[index] > arr[N - 1]) {
|
||||||
|
moreXL--;
|
||||||
|
swap(arr, moreXL, index);
|
||||||
|
} else {
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
swap(arr, moreXL, N - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//快速排序 - 核心方法
|
||||||
|
public static int[] partition(int[] arr, int L, int R) {
|
||||||
|
int lessXR = L - 1;
|
||||||
|
int moreXL = R;
|
||||||
|
int index = L;
|
||||||
|
while (index < moreXL) {
|
||||||
|
if (arr[index] < arr[R]) {
|
||||||
|
lessXR++;
|
||||||
|
swap(arr, lessXR, index);
|
||||||
|
index++;
|
||||||
|
} else if (arr[index] > arr[R]) {
|
||||||
|
moreXL--;
|
||||||
|
swap(arr, moreXL, index);
|
||||||
|
} else {
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
swap(arr, moreXL, R);
|
||||||
|
return new int[]{lessXR + 1, moreXL};
|
||||||
|
}
|
||||||
|
|
||||||
|
public void test() {
|
||||||
|
System.out.println(12);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//归并排序核心方法
|
||||||
|
private static void merge(int[] arr, int L, int M, int R) {
|
||||||
|
int[] help = new int[R - L + 1];
|
||||||
|
int i = 0;
|
||||||
|
int p1 = L;
|
||||||
|
int p2 = M + 1;
|
||||||
|
while (p1 <= M && p2 <= R) {
|
||||||
|
help[i++] = arr[p1] < arr[p2] ? arr[p1++] : arr[p2++];
|
||||||
|
}
|
||||||
|
while (p1 <= M) {
|
||||||
|
help[i++] = arr[p1++];
|
||||||
|
}
|
||||||
|
while (p2 <= R) {
|
||||||
|
help[i++] = arr[p2++];
|
||||||
|
}
|
||||||
|
for (int j = 0; j < help.length; j++) {
|
||||||
|
arr[L + j] = help[j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
int[] arr = {9, 8, 7, 5, 6, 7};
|
||||||
|
// guiBing3(arr);
|
||||||
|
// split(arr);
|
||||||
|
// System.out.println(arr);
|
||||||
|
|
||||||
|
Method guiBing = Class.forName("zuolaos.jichuban.Code10_各种排序").getMethod("guiBing", int[].class);
|
||||||
|
|
||||||
|
Object invoke = guiBing.invoke(Code10_各种排序.class,arr);
|
||||||
|
System.out.println(arr);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,13 @@
|
|||||||
package zuolaos.jichuban;
|
package zuolaos.jichuban;
|
||||||
|
|
||||||
public class Demo {
|
public class Demo implements A {
|
||||||
|
public void a(){
|
||||||
|
System.out.println(12);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in new issue