Java基础:常见算法

示例代码:

//100以内素数:

  1. class Prime{
  2.     public static void main(String[] args){
  3.         int i,j;
  4.         boolean isprime;
  5.         
  6.         for(i=2;i<100;i++){
  7.             isprime=true;
  8.             for(j=2;j<=i/j;j++){
  9.                 if((i%j)==0) {
  10.                     isprime=false;
  11.                     }
  12.             }
  13.             
  14.             if(isprime) {
  15.                 System.out.println(i+" is prime.");
  16.                 }
  17.         }
  18.     }
  19. }

//冒泡排序:

  1. class Buddle{
  2.     public static void main(String args[]){
  3.         int nums[]={58,99,63,14,2,-7,59595,-89546,452,-455};
  4.         int a,b,t;
  5.         System.out.println("nums数组:");
  6.         for(int i=0;i<nums.length;i++){
  7.             
  8.             System.out.print(" "+nums[i]);
  9.             }
  10.         System.out.println();
  11.         for(a=1;a<nums.length;a++){
  12.             for(b=nums.length-1;b>=a;b--){
  13.                 if(nums[b]<nums[b-1]){
  14.                     t=nums[b-1];
  15.                     nums[b-1]=nums[b];
  16.                     nums[b]=t;
  17.                     }
  18.                 }
  19.             }
  20.         System.out.println("nums数组排序后:");
  21.         for(int i=0;i<nums.length;i++){
  22.             
  23.             System.out.print(" "+nums[i]);
  24.             }
  25.         System.out.println();
  26.         }
  27.     }

//队列:

  1. class Queue{
  2.     char[] q;
  3.     int putloc,getloc;
  4.     public Queue(int size){
  5.         q=new char[size];
  6.         putloc=getloc=0;
  7.     }
  8.     
  9.     void put(char ch){
  10.         if(putloc==q.length){
  11.             System.out.println("Queue is full");
  12.             return;
  13.         }
  14.         q[putloc++]=ch;        
  15.     }
  16.     
  17.     char get(){
  18.         if(getloc==putloc){
  19.             System.out.println("Queue is empty");
  20.             return (char)0;
  21.         }
  22.         return q[getloc++];
  23.     }
  24. }
  25. class QDemo{
  26.     public static void main(String[] args){
  27.         Queue bigQ=new Queue(100);
  28.         Queue smallQ=new Queue(4);
  29.         
  30.         char ch;
  31.         int i;
  32.         System.out.println("Using bigQ to store the alphabet.");
  33.         for(i=0;i<26;i++){
  34.             bigQ.put((char)('A'+i));
  35.         }
  36.         System.out.println("Contents of bigQ:");
  37.         for(i=0;i<26;i++){
  38.             ch=bigQ.get();
  39.             if(ch!=(char)0) System.out.println(ch);
  40.         }
  41.         System.out.println("\n");
  42.         
  43.         System.out.println("Using smallQ to store the alphabet.");
  44.         for(i=0;i<5;i++){
  45.             //System.out.print("Attemping to store "+(char)('Z'-i));
  46.             smallQ.put((char)('Z'-i));
  47.             System.out.println();
  48.         }
  49.         System.out.println();
  50.         
  51.         System.out.println("Contents of smallQ:");
  52.         for(i=0;i<5;i++){
  53.             ch=smallQ.get();
  54.             if(ch!=(char)0){
  55.                 System.out.print(ch);
  56.             }
  57.         }
  58.     }
  59. }

//快速排序:

  1. class Qsort{
  2.     static void qsort(char[] num){
  3.         qs(num,0,(num.length-1));
  4.         }
  5.     private static void qs(char[] num,int left,int right){
  6.         int i,j,t;
  7.         char x,y;
  8.         i = left;
  9.         j = right;
  10.         t = (left + right)/2;
  11.         do{
  12.             x = num[t];
  13.     
  14.             while((num[i] < x) && (i < right)) i++; 
  15.                 
  16.             while((num[j] > x) && (j > left)) j--;         
  17.         
  18.             if(i <= j){
  19.                 y = num[i];
  20.                 num[i] = num[j];
  21.                 num[j] =y;
  22.                 i++;
  23.                 j--;
  24.                 }
  25.             
  26.             if(i < right){
  27.                 qs(num,i,right);
  28.                 }
  29.     
  30.             if(left < j){
  31.                 qs(num,left,j);
  32.                 }
  33.     
  34.                 
  35.         }while(i <= j);
  36.         }
  37.     }
  38. class QsortDemo{
  39.     public static void main(String args[]){
  40.         char[] nums = {'d','g','t','o','p','a','c','f','b','z'};
  41.         
  42.         char[] chars = {'d','g','t','o','p','a','c','f','h','k'};
  43.         for(char x : nums){
  44.             System.out.print(x +" ");
  45.             }
  46.         System.out.println();
  47.         
  48.         Qsort.qsort(nums);
  49.         for(char x : nums){
  50.             System.out.print(x +" ");
  51.             }
  52.         System.out.println();
  53.         
  54.         for(char x : chars){
  55.             System.out.print(x +" ");
  56.             }
  57.         System.out.println();
  58.     
  59.         Qsort.qsort(chars);
  60.         for(char x : chars){
  61.             System.out.print(x +" ");
  62.             }
  63.         System.out.println();
  64.         
  65.         
  66.         }
  67.     }

//堆栈:

  1. class Stack{
  2.     private char[] stak;
  3.     private int tos;
  4.     
  5.     Stack(int size){
  6.         stak = new char[size];
  7.         tos = 0;
  8.         }
  9.     Stack(Stack ob){
  10.         stak =new char[ob.stak.length];
  11.         tos =ob.tos;
  12.         for(int i=0;i<tos;i++){
  13.             stak[i] = ob.stak[i];
  14.             }
  15.         
  16.         }    
  17.     Stack(char[] ch){
  18.         stak = new char[ch.length];
  19.         for(int i=0;i<ch.length;i++){
  20.             push(ch[i]);
  21.             }
  22.         }
  23.         
  24.     void push(char t){
  25.         if(tos == stak.length){
  26.             System.out.println("this is full!!!");
  27.             return;
  28.             }
  29.             stak[tos] = t;
  30.             tos++;    
  31.                 
  32.         }
  33.     
  34.     int pop(){
  35.         if(tos == 0){
  36.             System.out.println("this is empty!!!");
  37.             return (char)0;
  38.             }
  39.             tos--;
  40.             return stak[tos];
  41.                 
  42.         }
  43.     }
  44. class stackDemo{
  45.     public static void main(String args[]){
  46.         Stack st1 = new Stack(10);
  47.         
  48.         int i;
  49.         char ch;
  50.         for(i=0;i<10;i++){
  51.             st1.push((char)('A'+i));
  52.             }
  53.             
  54.         char[] name={'T','o','m'};    
  55.         Stack st2 = new Stack(name);
  56.         
  57.         Stack st3 = new Stack(st1);
  58.         
  59.         System.out.println("st1:");
  60.         for(i=0;i<10;i++){
  61.             ch =(char) st1.pop();
  62.             System.out.print(ch+" ");
  63.             }
  64.         System.out.println();
  65.         
  66.         System.out.println("st2:");
  67.         for(i=0;i<3;i++){
  68.             ch = (char)st2.pop();
  69.             System.out.print(ch+" ");
  70.             }
  71.         System.out.println();    
  72.         
  73.         System.out.println("st3:");
  74.         for(i=0;i<10;i++){
  75.             ch = (char)st3.pop();
  76.             System.out.print(ch+" ");
  77.             }
  78.         System.out.println();    
  79.         }
  80.     
  81.     }

  82.  

猜你喜欢

转载自blog.csdn.net/QQhelphelp/article/details/88045903