简单排序

bubble:


public class bubbleSort {
 private int nNum;
 private int[] str;
 public bubbleSort(int i){
     str = new int[i];
 }
 public void insert(int i){
  str[nNum] = i;
  nNum++;  
 }
 public boolean del(int i){
  for(int root = 0;root<str.length;root++){
   if(str[root]==i&&str[root+1]!=0){
    for(int del = root;del<str.length;del++){
    str[root] = str[root+1];
    return true;
    }
   }
    else if(str[root]==i&&str[root+1]==0){
     str[root]=0;
     return true;
    }
  }
     return false;
 }
 
 public void print(){
  for(int g = 0;g<str.length;g++){
   System.out.print(str[g]);
   System.out.print(" ");
  }
 }
 
 public void sort(){
  int flag;
  int out = str.length;
  for(int j = out-1;j>=0;j--){
   for(int i = 0;i<j;i++){
   if (str[i]>str[i+1]){
    flag = str[i];
    str[i] = str[i+1];
    str[i+1] = flag;
   }
   }     
  }
   System.out.println("");
   System.out.println("after bublle sort::");      
 }  
 }

class bubbleSortApp{
 public bubbleSortApp(){
  bubbleSort num = new bubbleSort(10);
  num.insert(45);
  num.insert(56);
  num.insert(78);
  num.insert(44);
  num.insert(79);
  num.insert(34);
  num.insert(98);
  num.insert(678);
  num.insert(787);
  num.insert(789);
  num.print();
  num.sort();
  num.print();
 }
 public static void main(String[] args){
  new bubbleSortApp();
 }
}

select:


public class selectSort {
 private int nNum;
 private int[] str;
 public selectSort(int i){
     str = new int[i];
 }
 public void insert(int i){
  str[nNum] = i;
  nNum++;  
 }
 public boolean del(int i){
  for(int root = 0;root<str.length;root++){
   if(str[root]==i&&str[root+1]!=0){
    for(int del = root;del<str.length;del++){
    str[root] = str[root+1];
    return true;
    }
   }
    else if(str[root]==i&&str[root+1]==0){
     str[root]=0;
     return true;
    }
  }
     return false;
 }
 
 public void print(){
  for(int g = 0;g<str.length;g++){
   System.out.print(str[g]);
   System.out.print(" ");
  }
 }
 
 public void sort(){
  int flag = str[0];
  int head = str.length;
  for(int i = 0;i<head;i++){
   for(int j = i;j<head;j++){
    if(str[j]<flag) flag = str[j];
    }
    int temp;
    temp = flag;
    flag = str[i];
    str[i] = temp;
   }
   System.out.println("");
   System.out.println("after select sort::");            
 }
}

class selectSortApp{
 public selectSortApp(){
  selectSort num = new selectSort(10);
  num.insert(45);
  num.insert(56);
  num.insert(78);
  num.insert(44);
  num.insert(79);
  num.insert(34);
  num.insert(98);
  num.insert(678);
  num.insert(787);
  num.insert(789);
  num.print();
  num.sort();
  num.print();
 }
 public static void main(String[] args){
  new selectSortApp();
 }
}

 

insert:


public class insertSort {
 private int nNum;
 private int[] str;
 public insertSort(int i){
     str = new int[i];
 }
 public void insert(int i){
  str[nNum] = i;
  nNum++;  
 }
 public boolean del(int i){
  for(int root = 0;root<str.length;root++){
   if(str[root]==i&&str[root+1]!=0){
    for(int del = root;del<str.length;del++){
    str[root] = str[root+1];
    return true;
    }
   }
    else if(str[root]==i&&str[root+1]==0){
     str[root]=0;
     return true;
    }
  }
     return false;
 }
 
 public void print(){
  for(int g = 0;g<str.length;g++){
   System.out.print(str[g]);
   System.out.print(" ");
  }
 }
 
 public void sort(){
            for(int a = 1;a<nNum;a++){
             int right = str[a];
             int left = a;
             while(left>0&&str[left-1]>=right){
              str[left] = str[left-1];
              --left;
              }
              str[left] = right;
             }
   System.out.println("");
   System.out.println("after insert sort::");            
                    }
}

class insertSortApp{
 public insertSortApp(){
  insertSort num = new insertSort(10);
  num.insert(45);
  num.insert(16);
  num.insert(78);
  num.insert(44);
  num.insert(1);
  num.insert(34);
  num.insert(98);
  num.insert(678);
  num.insert(787);
  num.insert(389);
  num.print();
  num.sort();
  num.print();
 }
 public static void main(String[] args){
  new insertSortApp();
 }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值