import java.util.Arrays;
class Vecto {
private static Object[] elements;
public static int size;
public static int campacity;
public Vecto(int size,int campacity) {
this.size=size;
this.campacity=campacity;
this.elements=new Object[campacity];
}
public static void expandCampacity(int addCampacity) {
Object[] expand=elements;
elements=new Object[expand.length+addCampacity];
for(int i=0;i<expand.length;i++)
elements[i]=expand[i];
campacity=campacity+addCampacity;
}
public static void put(Object element) {
if(size==campacity)
expand();
elements[size]=element;
size+=1;
}
public static void insert(Object element,int position) {
if(position<0||position>=size)
System.out.println("illegal index");
else {
if(size==campacity)
expand();
for(int i=size;i>position;i--)
elements[i]=elements[i-1];
elements[position]=element;
size=size+1;}
}
public static void modify(int position,Object element) {
if(position<0||position>=size)
System.out.println("illegal index");
else
elements[position]=element;
}
public static Object get(int position) {
if(position<0||position>=size) {
System.out.println("illegal index");
return null;}
else
return elements[position];
}
public static void remove(int position) {
if(position<0||position>=size)
System.out.println("illegal index");
else {
for(int i=position;i<size-1;i++)
elements[i]=elements[i+1];
elements[size-1]=null;
size=size-1;}
}
public static void removeBatch(int low,int high) {
if(low<0||high>=size)
System.out.println("illegal region");
else {
for(int i=0;i<size-high;i++)
elements[low+i]=elements[high+i];
for(int i=size-1;i>size-1-high+low;i--)
elements[i]=null;
}
}
public static int findsize() {
return size;
}
public static int disOrder() {
int number=0;
for(int i=1;i<size;i++) {
if((int)(elements[i-1])>(int)(elements[i]))
number++;
}
return number;
}
public static int find(Object element,int low,int high) {
int position=0;
for(int i=low;i<high;i++) {
if(elements[i].equals(element))
position=i;
}
return position;
}
public static void sorted() {
Object temporary;
for(int i=1;i<size;i++)
for(int j=0;j<size-i;j++)
if((int)(elements[j])>(int)(elements[j+1])) {
temporary=elements[j];
elements[j]=elements[j+1];
elements[j+1]=temporary;
}
}
public static Boolean bubble(int low,int high) {
Boolean sorted=true;
Object temporary;
for(int i=low;i<high-1;i++) {
if((int)(elements[i])>(int)(elements[i+1])) {
sorted=false;
temporary=elements[i];
elements[i]=elements[i+1];
elements[i+1]=temporary;
}
}
return sorted;
}
public static void bubbleSort() {
int i=size;
Boolean isSorted=bubble(0,i);
while(!isSorted) {
i--;
isSorted=bubble(0,i);
}
}
public static int optimization(int low,int high) {
int last=low;
Object temporary;
for(int i=low;i<high-1;i++) {
if((int)(elements[i])>(int)(elements[i+1])) {
temporary=elements[i];
elements[i]=elements[i+1];
elements[i+1]=temporary;
last=i+1;
}
}
return last;
}
public static void bubbleSortOptimization() {
int last=optimization(0,size);
while(last>0)
last=optimization(0,last);
}
public static void merge(int low,int middle,int high) {
Object[] b=new Object[middle-low];
for(int m=0;m<(middle-low);m++)
b[m]=elements[low+m];
int i=0,k=0,j=0;
while((j<(middle-low))&&(k<(high-middle))){
if((int)(b[j])<=(int)elements[middle+k]) {
elements[i+low]=b[j];
i++;
j++;
}
else {
elements[low+i]=elements[middle+k];
i++;
k++;
}
}
if(i<(high-low)) {
if(j<(middle-low)) {
for(;j<middle-low;j++) {
elements[i+low]=b[j];
i++;
}
}
else {
for(;k<high-middle;k++) {
elements[low+i]=elements[middle+k];
i++;
}
}
}
}
public static void mergeSort(int low,int high) {
if((low+1)==high)
return ;
else{
mergeSort(low,(low+high)/2);
mergeSort((low+high)/2,high);
merge(low,(low+high)/2,high);
}
}
public static int findLessElement(Object element) {
int index=-1;
for(int i=0;i<elements.length;i++) {
if(elements[i].equals(element)) {
index=i;
break;
}
else {
if((int)elements[i]>(int)(element)) {
index=i-1;
break;
}
}
}
return index;
}
public void printElement() {
for(int i=0;i<size;i++)
System.out.print(elements[i]+" ");
System.out.println();
}
public static void removeDuplicateElement() {
for(int i=1;i<size;i++)
if(elements[i].equals(elements[i-1])) {
for(int j=i;j<size-1;j++)
elements[j]=elements[j+1];
elements[size-1]=null;
size--;}
}
public static void uniqueVector() {
for(int i=1;i<size;)
if(elements[i].equals(elements[i-1]))
remove(i);
else
i++;
}
public static void unique() {
int j,k;
for(int i=0;i<size;i++) {
j=i+1;
while(elements[i].equals(elements[j]))
j++;
j--;
for(k=i;j<size;k++,j++)
elements[k]=elements[j];
for(int n=k;n<size;n++) {
elements[n]=null;
size--;
}
}
}
public static void removeDisorderDuplicate() {
for(int i=1;i<size;i++) {
for(int j=0;j<i;j++)
if(elements[i].equals(elements[j]))
remove(j);
}
}
public static void removeDisorder() {
int flag;
for(int i=1;i<size;) {
flag=find(elements[i],0,i);
if(flag!=0)
remove(flag);
else
i++;
}
}
public static Object[] copy(Object[] a,int low,int high) {
Object[] b=new Object[high-low];
int t=0;
for(int i=low;i<high;i++) {
b[t]=a[i];
t++;
}
return b;
}
public static void expand() {
if(size==campacity) {
Object[] temporary=elements;
campacity=campacity*2;
elements=new Object[campacity];
for(int i=0;i<size;i++)
elements[i]=temporary[i];
}
}
public static void iterator() {
for(int i=0;i<size;i++)
elements[i]=(int)(elements[i])+1;
}
public static int binarySearch(Object element,int low,int high) {
int middle=(high+low)/2 ;
while(low<high) {
middle=(high+low)/2 ;
if((int)element<(int)(elements[middle]))
high=middle;
else if((int)(elements[middle])<(int)element)
low=middle+1;
else
return middle;
}
return -1;
}
public static int binarySearchDigui(Object element,int low,int high) {
if(low<high) {
int middle=(high+low)/2;
if((int)(elements[middle])<(int)element)
return binarySearchDigui(element,middle+1,high);
else if((int)element<(int)(elements[middle]))
return binarySearchDigui(element,low,middle);
else
return low;
}
else
return -1;
}
public static int binarySearchTwo(Object element,int low,int high) {
int middle=low;
while(low+1<high) {
middle=(high+low)/2;
if((int)element<(int)(elements[middle]))
high=middle;
else
low=middle;
}
if(elements[low].equals(element))
return low;
else
return -1;
}
public static int binarySearchC(Object element,int low,int high) {
int middle=(high+low)/2;
while(low<high) {
middle=(high+low)/2;
if((int)element<(int)(elements[middle]))
high=middle;
else
low=middle+1;
}
return low-1;
}
public static int fibonacci(int n) {
if(n==0)
return 0;
else if(n==1)
return 1;
else
return fibonacci(n-1)+fibonacci(n-2);
}
public static int fibonacciPosition(int length) {
int k=1;
int m=fibonacci(k);
while(m-1<length) {
k++;
m=fibonacci(k);}
return k;
}
public static int fibonacciSearch(Object element,int low,int high) {
int k=fibonacciPosition(high-low);
int middle=low+fibonacci(k-1)-1;
while(middle>=low&&middle<high) {
if((int)element<(int)(elements[middle])) {
high=middle;
k=fibonacciPosition(high-low);
middle=fibonacci(k-1)-1;
}
else if((int)(elements[middle])<(int)element) {
low=middle+1;
k=fibonacciPosition(high-low);
middle=low+fibonacci(k-1)-1;
}
else
return middle;
}
return -1;
}
}
public class Vect{
public static void main(String[] args) {
Vecto v=new Vecto(0,5);
v.put(2);
v.put(1);
v.put(4);
v.put(3);
v.put(6);
v.put(3);
v.printElement();
v.mergeSort(0, 6);;
v.printElement();
}
}
Java实现向量基本操作
最新推荐文章于 2024-01-04 11:44:02 发布