基于java的数据结构学习手记2

 二.选择排序法的java实现

       冒泡,选择,插入排序是最基本的也是有效的排序方式,这里主要用java编写了选择排序的实现,其中的思想在ArraySel.classs的selectionSort()方法中。

 

Code:
  1. //selectArray.java  
  2. //demonstrates selection sort  
  3. /  
  4. public class ArraySel {  
  5.     private long[]a;//ref to array a  
  6.     private int nElems;//number of array data items  
  7.     //---------------------------------------------------  
  8.     public ArraySel(int max)//construtor  
  9.     {  
  10.         a=new long[max];//creat the array,declaration  
  11.         nElems=0;//there is no items yet  
  12.     }  
  13.     //...................................................  
  14.     public boolean find(long searchKey)//find the specified value  
  15.     {  
  16.         int j;  
  17.         for(j=0;j<nElems;j++)  
  18.         {  
  19.             if(a[j]==searchKey)  
  20.                 break;//found it and jump out of the loop  
  21.         }  
  22.         if (j==nElems)return false;//not found  
  23.         else return true;  
  24.     }  
  25.     //...................................................  
  26.     public void insert(long value)//put element into array  
  27.     {  
  28.         a[nElems]=value;//put the value into the array  
  29.         nElems++;//add the total number of the data items     
  30.     }  
  31.     public boolean delete(long value)//delete an array item  
  32.     {  
  33.         int j,k;  
  34.         for(j=0;j<nElems;j++)//search the item  
  35.         {  
  36.             if(a[j]==value)  
  37.                 break;  
  38.         }  
  39.         if(j==nElems)return false;//not found  
  40.         else   
  41.             {  
  42.                 for(k=j;k<nElems;k++)//put every item behind the target one ahead  
  43.                 {  
  44.                     a[k]=a[k+1];  
  45.                 }  
  46.                 nElems--;     
  47.                 return true;  
  48.             }  
  49.     }  
  50.       
  51.     public void selectionSort()  
  52.     {  
  53.         int out,in,min;  
  54.         long temp;  
  55.         for(out=0;out<nElems-1;out++)//outer loop  
  56.         {  
  57.             min=out;              //minimum  
  58.             for(in=out+1;in<nElems;in++)//inner loop  
  59.                 if(a[in]<a[min])min=in;//if min greater  
  60.             temp=a[min];  
  61.             a[min]=a[out];                   //replace the first inner place with min  
  62.             a[out]=temp;  
  63.         }  
  64.           
  65.     }  
    •  //....................................................
    •     public void insertionSort()
    •     {
    •      int in,out;
    •      for(out=1;out<nElems;out++)//the out loop
    •     
    •      long temp=a[out];//remove marked item
    •      for(in=out;in>0;in--)//start inner loop at out
    •      {                    //until one is smaller
    •      if(a[in-1]>temp)
    •      a[in]=a[in-1];//when not met,shift item right
    •                       
    •     
    •      else break;//when met,jump out
    •      }
    •      a[in]=temp;    //insert the flag into a[in],the proper position
    •      }
    •     
    •     }
    •     //......................................................
  66.     public void display()  
  67.     {  
  68.         for(int j=0;j<nElems;j++)  
  69.         {  
  70.             System.out.println(a[j]+" ");  
  71.               
  72.         }  
  73.     //....................................................................    
  74.     }  
  75.       
  76. }  

主函数调用

Code:
  1. public class ArraySelApp {  
  2.   
  3.     /** 
  4.      * @param args 
  5.      */  
  6.     public static void main(String[] args) {  
  7.         // TODO Auto-generated method stub  
  8.         int maxSize=100;  
  9.         ArraySel arr;  
  10.         arr=new ArraySel(maxSize);//初始化  
  11.        arr.insert(77);  
  12.        arr.insert(99);  
  13.        arr.insert(44);  
  14.          
  15.        arr.insert(55);  
  16.          
  17.        arr.insert(22);  
  18.        arr.insert(16);  
  19.        arr.insert(33);  
  20.          
  21.        arr.insert(56);  
  22.        arr.insert(33);  
  23.        arr.display();   //display items  
  24.        System.out.println();  
  25.        System.out.println();  
  26.        arr.selectionSort();  
  27.        arr.display();  
  28.     }  
  29.       
  30.   
  31. }  

结果输出

Code:
  1. 77   
  2. 99   
  3. 44   
  4. 55   
  5. 22   
  6. 16   
  7. 33   
  8. 56   
  9. 33   
  10.   
  11.   
  12. 16   
  13. 22   
  14. 33   
  15. 33   
  16. 44   
  17. 55   
  18. 56   
  19. 77   
  20. 99   

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值