java经典代码

二分法查找示例
成员类:
package com.junglesong;

public class Member implements Comparable{
private String name;
private int age;

public Member(String name,int age){
this.name=name;
this.age=age;
}

/** *//**
* 实现成员比较
*/
public int compareTo(Object obj){
Member another=(Member)obj;
return this.name.compareTo(another.name);
}

/** *//**
* 实现成员相等运算
*/
public boolean equals(Object obj){
Member another=(Member)obj;
return this.name.equals(another.name);
}

public String toString(){
return "名="+name+" 年龄="+age;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

二分查找类:
package com.junglesong;

import java.util.ArrayList;
import java.util.List;

/** *//**
* 二分查找示例二(对链表进行查找)
* @author: sitinspring([email protected])
* @date: 2008-3-8
*/
public class BinSearch2{
public static void main(String[] args){
// 欲查找的链表
List<Member> members=new ArrayList<Member>();
members.add(new Member("Andy",21));
members.add(new Member("Bill",22));
members.add(new Member("Cindy",23));
members.add(new Member("Douglas",24));
members.add(new Member("Felex",25));
members.add(new Member("Green",26));

// 测试链表
List<Member> tempList=new ArrayList<Member>();
tempList.add(new Member("Bill",22));
tempList.add(new Member("Cindy",23));
tempList.add(new Member("Douglas",24));
tempList.add(new Member("Felex",25));
tempList.add(new Member("Hill",26));

for(Member member:tempList){
System.out.println("成员"+member+"的下标为"+binSearch(members,member));
}
}

/** *//**
* 二分查找
* @param sortedArray 已排序的欲查找的链表
* @param seachValue 查找的值
* @return 找到的元素下标,若找不到则返回-1
*/
public static int binSearch(List<Member> sortedList,Member seachValue){
// 左边界
int leftBound=0;
// 右边界
int rightBound=sortedList.size()-1;
// 当前下标位置
int curr;

while(true){
// 定位在左边界和右边界中间
curr=(leftBound+rightBound)/2;

if(sortedList.get(curr).equals(seachValue)){
// 找到值
return curr;
}
else if(leftBound>rightBound){
// 左边界大于右边界,已经找不到值
return -1;
}
else{
if(sortedList.get(curr).compareTo(seachValue)<0){
// 当当前下标对应的值小于查找的值时,缩短左边界
leftBound=curr+1;
}
else{
// 当当前下标对应的值大于查找的值时,缩短右边界
rightBound=curr-1;
}
}
}
}
}

回应 删除
  • 傻傻~de

    2008-06-12 17:59:22 傻傻~de

    类的生明 if else

    回应 删除
  • 克洛泽大地

    2008-06-13 15:01:51 克洛泽大地 (DREG)

    选择排序


    public class NumSort{

    public static void main(String[] args){
    int[] a=new int[args.length];
    for(int i=0;i<args.length;i++){
    a[i]=Integer.parseInt(args[i]);

    }

    print(a);

    selectionSort(a);

    print(a);
    }


    private static void print(int[] a){
    for(int i=0;i<a.length;i++){
    System.out.print(a[i]+" ");
    }
    System.out.println();
    }

    //---------------------选择排序算法1-----------------
    /*
    private static void selectionSort(int[] a){
    for(int i=0;i<a.length;i++){
    for(int j=i+1;j<a.length;j++){
    if(a[j]<a[i]){
    int temp=a[i];
    a[i]=a[j];
    a[j]=temp;

    }
    }
    }

    }

    */
    //---------------------选择排序算法2-----------------
    /*
    private static void selectionSort(int[] a){
    for(int i=0;i<a.length;i++){
    int k = i;
    for(int j=k+1;j<a.length;j++){
    if(a[j]<a[k]){
    k = j;
    }
    }

    if(k!=i){
    int temp=a[i];
    a[i] = a[k];
    a[k] = temp;
    }
    }
    }

    */
    //---------------------选择排序算法3-----------------
    private static void selectionSort(int[] a){
    int k,temp;
    for(int i=0;i<a.length;i++){
    k = i;
    for(int j=k+1;j<a.length;j++){
    if(a[j]<a[k]){
    k = j;
    }
    }

    if(k!=i){
    temp=a[i];
    a[i] = a[k];
    a[k] = temp;
    }
    }
    }
    //------------------------------------------------


    }

  • 你可能感兴趣的:(java经典代码)