您的位置:首页 > 编程语言 > Java开发

java实现的用插入法进行排序

2017-10-19 10:34 239 查看
用java实现一种排序,java类型实现序列化的方法(二种)?如在collection框架中,实现比较要实现什么养的接口?

/**

 * 用插入法进行排序代码实现如下:

 */

import java.util.ArrayList;

import java.util.Random;

public class InsertSort {

 ArrayList al;// 定义一个链表;

 public InsertSort(int num, int mod) {

  al = new ArrayList(num);// 实例化一个链表

  Random random = new Random();// 去一个随机数

  System.out.println("The ArrayList Sort Before:");

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

   al.add(new Integer(Math.abs(random.nextInt()) % mod + 1));//

   System.out.println("al[" + i + "]=" + al.get(0));

  }

 }

 public void SortIt() {

  Integer tempInt;

  int MaxSize = 1;

  for (int i = 1; i < al.size(); i++) {

   tempInt = (Integer) al.remove(i);

   if (tempInt.intValue() >= ((Integer) al.get(MaxSize - 1))

     .intValue()) {

    al.add(MaxSize, tempInt);

    MaxSize++;

    System.out.println(al.toString());

   } else {

    for (int j = 0; j < MaxSize; j++) {

     if (((Integer) al.get(j)).intValue() >= tempInt.intValue()) {

      al.add(j, tempInt);

      MaxSize++;

      System.out.println(al.toString());

      break;

     }

    }

   }

  }

  System.out.println("The ArrayList Sort After:");

  for (int i = 0; i < al.size(); i++) {

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

  }

 }

 public static void main(String[] args) {

  InsertSort is = new InsertSort(10, 100);

  is.SortIt();

 }

}

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐