public class Show{
public static void main(String arges[]){
int arr={1,5,3,0,8,4};
int temp=0;
for(int i=0;i<arr.length-1;i++){ //外部比较
for(int j=0;j<arr.length-1-i;j++){ //内部比较
if(arr[j]>arr[j+1]){
temp=arr[j]; //换位
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
for(int k=0;k<arr.length;k++){
System.out.println(arr[k]);
}
}
}
二分查找
package com.singel;
public class Test {
/**二分查找
*
*/
public static void main(String[] args) {
int a[]={1,4,5,6,7,8};
BinaryFind bf=new BinaryFind();
bf.find(0, a.length-1, 1, a);
}
}class BinaryFind{
public void find(int left,int right,int val,int a[]){
if(left<=right){
int mid=(left+right)/2;
int m=a[mid];
if(m<val){
find(mid+1,right,val,a);
}else if(m>val){
find(left,mid-1,val,a);
}else if(m==val){
System.out.println("find index");
}
}
}
}
串
package com.data;
public class Show implements IString {
private char strval[]; //字符数组,存放串值
private int curlen; //当前串的长度
public Show(){
strval=new char[0];
curlen=0;
}
public Show(String str){
char temp[]=str.toCharArray();
strval=temp;
curlen=temp.length;
}
public Show(char[] val){
this.strval=new char[val.length];
for(int i=0;i<val.length;i++){
this.strval[i]=val[i];
}
curlen=val.length;
}
public char charAt(int index) {
// 查找
if((index<0)||(index>=this.curlen)){
throw new StringIndexOutOfBoundsException(index);
}
return this.strval[index];
}
public void clear() {
// 清空
this.curlen=0;
}
public int compareTo(Show str) {
// 比较
int len1=this.curlen;
int len2=str.curlen;
int n=Math.min(len1, len2);
char c1[]=this.strval;
char c2[]=str.strval;
int k=0;
while(k<n){
char ch1=c1[k];
char ch2=c2[k];
if(ch1!=ch2){
return ch1-ch2;
}
}
return len1-len2;
}
public IString concat(IString str) {
// 连接
return this.insert(this.curlen, str);
}
public IString delete(int begin, int end) {
// 删除
if(begin<0){
throw new StringIndexOutOfBoundsException("开始位置不能小于0");
}
if(end>this.curlen){
throw new StringIndexOutOfBoundsException("结束位置不能大于当前串的长度:"+this.curlen);
}
if(begin>end){
throw new StringIndexOutOfBoundsException("开始位置不能大于结束位置");
}
for(int i=0;i<this.curlen-end;i++){
this.strval[begin+i]=this.strval[end+i];
}
this.curlen=this.curlen-(end-begin);
return this;
}
public int indexOf(IString str, int begin) {
// 查找
return 0;
}
public IString insert(int offset, IString str) {
// 添加
if((offset<0||(offset>this.curlen))){
throw new StringIndexOutOfBoundsException("插入位置不合法");
}
int len=str.length();
int newCount=this.curlen+len;
if(newCount>this.strval.length){
this.allocate(newCount);
}
for(int i=this.curlen-1;i>=offset;i--){
this.strval[len+i]=this.strval[i];
}
for(int i=0;i<len;i++){
this.strval[offset+i]=str.charAt(i);
}
this.curlen=newCount;
return this;
}
public boolean isEmpty() {
// 是否为空
return curlen==0;
}
public int length() {
// 长度
return this.curlen;
}
public IString substring(int begin, int end) {
// 截取
if(begin<0){
throw new StringIndexOutOfBoundsException("开始位置不能小于0");
}
if(end>this.curlen){
throw new StringIndexOutOfBoundsException("结束位置不能大于串的当前长度:"+this.curlen);
}
if(begin>end){
throw new StringIndexOutOfBoundsException("开始位置不能大于结束位置");
}
if(begin==0&&end==0){
return this;
}else{
char[] buffer=new char[end-begin];
for(int i=0;i<buffer.length;i++){
buffer[i]=this.strval[i+begin];
}
return new Show(buffer);
}
}
public void allocate(int newCapacity) {
//扩充空间
char temps[]=this.strval;
this.strval=new char[newCapacity];
for(int i=0;i<temps.length;i++){
this.strval[i]=temps[i];
}
}
}
队列(链式)
package com.jeecms.common.crawler.data;
import java.util.LinkedList;
/**
* 队列
* @author javacoo
* @since 2011-11-01
* @param <T>
*/
public class Queue<T> {
private LinkedList<T> queue = new LinkedList<T>();
/**
* 入队列
* @param t
*/
public void enQueue(T t){
queue.addLast(t);
}
/**
* 出队列
* @return t
*/
public T deQueue(){
return queue.removeFirst();
}
/**
* 判断队列是否为空
* @return
*/
public boolean isEmpty(){
return queue.isEmpty();
}
/**
* 判断队列是否含有t
* @param t
* @return
*/
public boolean contains(T t){
return queue.contains(t);
}
/**
* 取得队列大小
* @return
*/
public int getSize(){
return queue.size();
}
}