Javaデータ構造とアルゴリズムノートを学ぶ(一)
10705 ワード
package com.example.demo;
/**
*
* Description:
* @ClassName: List
* @author 2018 4 19 3:16:39
* @see TODO
*/
public class List {
private volatile int size;
private Long[] array= {};
public List(int max) {
array=new Long[max];
size=0;
}
public int find(Long key) {
int up=size-1;
int low=0;
int model;
while(true) {
model=(up+low)/2;
if(array[model]==key) {
return model;
}else if(low>up) {
return size;
}else {
if(array[model]>key) {
up=model-1;
}else {
low=model+1;
}
}
}
}
public void insert(Long value) {
int i;
for ( i = 0; i < size; i++) {
if(array[i]>value) {
break;
}
}
for(int j=size;j>i;j--) {
array[j]=array[j-1];
}
array[i]=value;
size++;
}
public boolean delete(Long key) {
int find = find(key);
if(find==size) {
return false;
}
for (int i = find; i < size; i++) {
array[i]=array[i+1];
}
size--;
return true;
}
/**
*
* Title: dubboSort
* Description:
* 2,1,3,4,7,8,1
* ,
* @return
* @author 2018 4 19 3:29:01
*/
public Long[] dubboSort() {
for(int i=size-1;i) {
for(int j=0;j) {
if(array[j]>array[j+1]) {
long tem=array[j];
array[j]=array[j+1];
array[j+1]=tem;
}
}
}
return array;
}
/**
*
* Title: choseSort
* Description:
* -- , , ,
* @return
* @author 2018 4 19 5:02:57
*/
public Long[] choseSort() {
int min;
for (int i = 0; i < size; i++) {
min=i;
for (int j = i+1; j ) {
if(array[min]>array[j]) {
long tem = array[min];
array[min]=array[j];
array[j]=tem;
}
}
}
return array;
}
public Long[] insertSort() {
for (int i = 1; i < size; i++) {
long tem=array[i];
while(i>0 && tem]) {
array[i]=array[i-1];
--i;
}
array[i]=tem;
}
return array;
}
public static void main(String[] args) {
List list=new List(10);
list.insert(1l);
list.insert(2l);
list.insert(3l);
int find = list.find(2l);
// System.out.println(find);
// Long[] dubboSort = list.dubboSort();
// Long[] dubboSort = list.choseSort();
Long[] dubboSort = list.insertSort();
for (Long long1 : dubboSort) {
if(long1!=null) {
System.out.println(long1);
}
}
}
}
転載先:https://www.cnblogs.com/nihaofenghao/p/8889283.html