一些基础算法

来源:互联网 发布:网络礼仪 编辑:程序博客网 时间:2024/05/02 00:49

1.字符串的顺序反向输出和数值的引用传递

public class MyClass{

           public static void main(String argv[]){

                      String s="abcdefg";

                      Integer i =new Integer(10);                  

                      MyClass myclass = new MyClass();                    

                      System.out.println(myclass.aaa(i));

                      System.out.println(myclass.reverse(s));

           }

 

           public Integer aaa(Integer i){

                      int x =i.intValue();

                      x=x*2;                                   

                      return  Integer.valueOf(x);                   

           }

           public static String reverse(String s){

                      int length=s.length();

                      StringBuffer result=new StringBuffer(length);

                      for(int i=length-1;i>=0;i--)

                      result.append(s.charAt(i));

                      return result.toString();

           }

}

2.数值排序

public class Test{   

              public static void main(String[] args)

              {

                  int[] maker ={3,5,7,2,8,1};

                  int[] arry;

                            Test test=new Test();

                            arry=test.f(maker);

                            for(int i=0;i<arry.length;i++){

                                          System.out.println("arry="+arry[i]);

                            }

              }            

              public int[] f(int[] maker)

              {

                            int[] arry=maker;

                            int length=arry.length;

                            for(int i=0;i<length;i++){

                                          for(int j=i+1;j<length;j++){

                                                        if(arry[i]>arry[j]){

                                                                      int temp=arry[j];

                                                                      arry[j]=arry[i];

                                                                      arry[i]=temp;

                                                        }

                                          }

                            }                          

                            return arry;

              }

}

3.字符排序

public class Test{   

              public static void main(String[] args)

              {

                  char[] maker ={'a','g','A','J','e'};

                  char[] arry;

                            Test test=new Test();

                            arry=test.f(maker);

                            for(int i=0;i<arry.length;i++){

                                          System.out.println("arry="+arry[i]);

                            }

              }            

              public char[] f(char[] maker)

              {

                            char[] arry=maker;

                            int length=arry.length;

                            for(int i=0;i<length;i++){

                                          for(int j=i+1;j<length;j++){

                                                        if(arry[i]>arry[j]){

                                                                      char temp=arry[j];

                                                                      arry[j]=arry[i];

                                                                      arry[i]=temp;

                                                        }

                                          }

                            }                          

                            return arry;

              }

}

4.字符串排序

public class Test{   

              public static void main(String[] args)

              {

                  String maker ="agAJe";

                            Test test=new Test();

                            char[] arry1=test.f(maker.toCharArray());

                            String str= new String(arry1);

                            System.out.println("str="+str);

              }            

              public char[] f(char[] maker)

              {

                            char[] arry=maker;

                            int length=arry.length;

                            for(int i=0;i<length;i++){

                                          for(int j=i+1;j<length;j++){

                                                        if(arry[i]>arry[j]){

                                                                      char temp=arry[j];

                                                                      arry[j]=arry[i];

                                                                      arry[i]=temp;

                                                        }

                                          }

                            }                          

                            return arry;

              }

}