`
200830740306
  • 浏览: 106353 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

排序效率分析1(源码+报告)

    博客分类:
  • java
阅读更多
    做课程设计时,写了一个排序的算法包,可用来分析常用的几种排序算法的交换,比较和移动等的次数。同时生成数据的方式有随机,正序和逆序等功能。这里拿来共享下,有什么错的,请指正!
先写个排序的数组类,以方便求取各种基本操作的次数
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nc.norman.sorter;

/**
 * 自定义数组,下标都是从1开始的。
 * 可以记录排序时的交换次序,移动次数和比较次数
 * 可以由比较方法去确定排序的顺序
 * @author NC
 * @version 1.0
 */
public class SortArray {

    private int[] array;//数组
    public int order;
    public int length;//记录数组长度
    private int key;//用于暂存比较的关键字
    private int exchangeCount = 0;//记录交换次数
    private int compareCount = 0;//记录比较次数
    private int moveCount = 0;//记录移动次数
    private int insertCount = 0;//记录插入次数
    public static final int ORDERDATA = 1;
    public static final int INVERTEDDATA = 2;
    public static final int RANDOMDATA = 3;

    public SortArray(int number) {
        //默认产生指定长度随机数组
        this.array = new int[number];
        this.length = number;
        this.order = RANDOMDATA;
        for (int i = 0; i < number; i++) {
            int elem = (int) Math.round(Math.random() * number * 2);
            this.array[i] = elem;
        }
    }

    public SortArray(int length, int order) {
        //产生指定长度和顺序的数组
        this.array = new int[length];
        this.length = length;
        this.order = order;
        if (order == ORDERDATA) {
            for (int i = 0; i < length; i++) {
                int elem = i + 1;
                this.array[i] = elem;
            }
        } else if (order == INVERTEDDATA) {
            for (int i = length - 1,j = 0; i >= 0; i--,j++) {
                int elem = i + 1;
                this.array[j] = elem;//之前弄错了
            }
        } else {
            for (int i = 0; i < length; i++) {
                int elem = (int) Math.round(Math.random() * length * 2);
                this.array[i] = elem;
            }
        }
    }

    public SortArray copyArray() {
        if (this.order != RANDOMDATA) {
            return new SortArray(this.length, this.order);
        } else {
            int[] arr = new int[length];
            for (int i = 0; i < arr.length; i++) {
                arr[i] = this.array[i];
            }
            SortArray sa = new SortArray(arr);
            return sa;
        }
    }

    public SortArray(int[] array) {
        this.length = array.length;
        this.array = new int[array.length];
        this.order = RANDOMDATA;
        for (int i = 0; i < array.length; i++) {
            this.array[i] = array[i];
        }
    }

    public int getInsertCount() {
        return insertCount;
    }

    public void addInsertCount() {
        this.insertCount++;
    }

    public int getMoveCount() {
        return moveCount;
    }

    public void addMoveCount() {
        this.moveCount++;
    }

    public int getCompareCount() {
        return compareCount;
    }

    public void addCompareCount() {
        this.compareCount++;
    }

    public int getExchangeCount() {
        return exchangeCount;
    }

    public void addExchangeCount() {
        this.exchangeCount++;
    }

    public int getKey() {
        return key;
    }

    public void setKey(int i) {
        this.key = this.array[i - 1];
    }

    public void setArray(int[] array) {
        //要注意,此方法并不会改变其他属性值
        for (int i = 0; i < array.length; i++) {
            this.array[i] = array[i];
        }
    }

    public int[] getArray() {
        return this.array;
    }

    public int getElement(int i) {
        return this.array[i - 1];
    }

    public void setElement(int i, int value) {
        this.array[i - 1] = value;
    }

    public void print() {
        for (int i = 0; i < this.array.length; i++) {
            System.out.print(this.array[i] + " ");
        }
        System.out.println();
    }

    public void swap(int i, int j) {
        int t = this.array[i - 1];
        this.array[i - 1] = this.array[j - 1];
        this.array[j - 1] = t;
        this.exchangeCount++;
    }

    public boolean compare(int i, int j, boolean order) {
        this.compareCount++;
        if (order) {//如果是正序
            return this.array[i - 1] < this.array[j - 1];
        } else {
            return this.array[i - 1] > this.array[j - 1];
        }
    }

    public boolean compareNotMoreThan(int a, int b, boolean order) {
        this.compareCount++;
        if (order) {
            return a <= b;
        } else {
            return a >= b;
        }
    }

    public boolean compareToKey(int i, boolean order) {
        this.compareCount++;
        if (order) {//如果是正序
            return this.array[i - 1] < key;
        } else {
            return this.array[i - 1] > key;
        }
    }

    public boolean equal(int i, int j) {
        return this.array[i - 1] == this.array[j - 1];
    }

    @Override
    public String toString() {
        String s = "";
        for (int i = 0; i < this.array.length; i++) {
            s = s + this.array[i] + " ";
        }
        return s.trim();
    }
}

再写个排序器的抽象类,以方便各种排序算法的实现
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nc.norman.sorter;

/**
 * 排序器
 * @author NC
 * @version 1.0
 */
public abstract class Sorter {

    public  final static boolean POSITIVE = true;
    public  final static boolean INVERTED = false;

    public abstract void sort(SortArray array, boolean order);

    public  final void sort(SortArray array) {
        sort(array, POSITIVE);
    }
}

具体的实现请看另一排序效率分析2

本来附件是实现的排序效率分析小程序,可生成各种相关的表格
但是,在生成表格时,出了一点小问题,还没改好,暂时不上传了。。。
之前,下过的,不好意思了。。。。。。。

原来这还没有上传啊。。。刚刚才发现,现在重新上传。。。



附件是之前的数据结构的源码和报告
  • 大小: 41.1 KB
1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics