Excel排序

主类 ExcelSort.java

import sort.*;
import java.util.Scanner;
public class ExcelSort{
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        LinkList linkList = new LinkList();
        int N=1,C=0;
        //输入
        while(N!=0)
        {
            String[] line = scan.nextLine().split(" ");
            N = Integer.valueOf(line[0]);
            C = Integer.valueOf(line[1]);
            if(N!=0)
            {
                Student[] student = new Student[N];
            for(int i=0;i<N;i++)
            {
                String[] str = scan.nextLine().split(" ");
                student[i] = new Student(C,str[0],str[1],Integer.valueOf(str[2]));
                //System.out.println(student[i].getNumber());
            }
                linkList.insert(student);
            }

        }
        //输出
        Node p = linkList.getHead().getNext();
        int i=0;
        while(p!=null)
        {
            ++i;
            Student[] out = (Student[])p.getData();
            //int order = out[0].getOrder();
            Sort.quick(out);
            System.out.println("Case "+i+":");
            for(int k=0;k<out.length;k++)
                System.out.println(out[k].getNumber()+" "+out[k].getName()+" "+out[k].getScore());
            p = p.getNext();
        }


    }
}

sort包中的类:
1.Student.java

package sort;
public class Student implements Comparable<Student>{
    private int order;
    private String number;
    private String name;
    private int score;

    public Student(int order,String number,String name,int score){
        this.order = order;
        this.number = number;
        this.name = name;
        this.score = score;
    }

    public int compareTo(Student another){
        int thisNumber = Integer.valueOf(this.getNumber());
        int anotherNumber = Integer.valueOf(another.getNumber());
        switch (order){
            case 1:

            return thisNumber<anotherNumber?-1:(thisNumber==anotherNumber?0:1);

            case 2:
            if (this.getName().equals(another.getName())) 
                return thisNumber<anotherNumber?-1:(thisNumber==anotherNumber?0:1);
            else
                return this.getName().compareTo(another.getName());
                /*
                String 类型比较方法的原理:
                int i=0;
                while(i<this.getName().length()&&i<another.getName().length())
                {
                    if(this.getName().charAt(i)<another.getName().charAt(i))
                        return -1;
                    else if(this.getName().charAt(i)>another.getName().charAt(i))
                        return 1;
                    else 
                        ++i;
                }
                return this.getName().length()<another.getName().length()?-1:(this.getName().length()==another.getName().length()?0:1);*/

            case 3:
                if (this.getScore()==another.getScore()) 
                    return thisNumber<anotherNumber?-1:(thisNumber==anotherNumber?0:1);
                else
                    return this.getScore()<another.getScore()?-1:(this.getScore()==another.getScore()?0:1);
            default: 
                return -1;
            }
    }

    public void setOrder(int order){
        this.order = order;
    }
    public int getOrder(){
        return this.order;
    }
    public void setNumber(String number){
        this.number = number;
    }
    public String getNumber(){
        return this.number;
    }
    public void setName(String name){
        this.name = name;
    }
    public String getName(){
        return this.name;
    }
    public void setScore(int score){
        this.score = score;
    }
    public int getScore(){
        return this.score;
    }
}

2.Node.java

package sort;
public class Node{
    private Comparable[] data;
    private Node next;


    public Node(){
        this(null,null);
    }
    public Node(Comparable[] data){
        this(data,null);
    }
    public Node(Comparable[] data, Node next){
        this.data = data;
        this.next = next;

    }
    public void setData(Comparable[] data){
        this.data = data;
    }
    public Comparable[] getData(){
        return this.data;
    }
    public void setNext(Node next){
        this.next = next;
    }
    public Node getNext(){
        return this.next;
    }
}

3.LinkList.java

package sort;
public class LinkList{
    private Node head;

    public LinkList(){
        head = new Node();
    }
    public void clear(){
        head.setData(null);
        head.setNext(null);
    }
    public boolean isEmpty(){
        return head.getNext()==null;
    }
    public int length(){
        Node p = head.getNext();
        int length=0;
        while(p!=null)
        {
            p = p.getNext();
            length++;
        }
        return length;
    }

    //尾插法
    public void insert(Comparable[] x){
        Node p = head.getNext();
        Node s = new Node(x);
        if(p==null)
            head.setNext(s);
        else{
            int i = 1;
            while(p!=null&&i<length()){
                i++;
                p = p.getNext();
            }
            p.setNext(s);
        }

    }

    public Comparable[] get(int i) throws Exception{
        Node p = head.getNext();
        int j = 0;
        while(p!=null&&j<i){
            p = p.getNext();
            ++j;
        }
        if(j>i||p==null)
            throw new Exception("第"+i+"个元素不存在");
        return p.getData();
    }
    public int indexOf(Comparable[] x){
        Node  p =head.getNext();
        int j=0;
        while(p!=null&&!p.getData().equals(x)){
            p = p.getNext();
            ++j;
        }
        if(p!=null)
            return j;
        else
            return -1;

    }
    public Node getHead(){
        return this.head;
    }
}

4.Sort.java

package sort;
public class Sort {
public static void main(String[] args) {
    Integer[] a1 = {52, 39, 67, 95, 70, 8, 25, 52};
    //String类型已经实现Comparable接口,compareTo方法定义为按字典顺序比较
    //(当第一个字符相同时,从第二个比较,以此类推)
    String[] a = {"Sa", "Sb", "O", "R", "T", "E", "X", "A", "M", "P", "L", "E"};

    System.out.println("排序前:");
    for (int i = 0; i < a.length; i++)
        System.out.print(a[i] + " ");
    //insert(a);
    //shell(a);
    quick(a);
    //select(a);
    //merge(a);
    //heap(a);
    System.out.println("\n排序后:");
    for (int i = 0; i < a.length; i++)
        System.out.print(a[i] + " ");
}
//直接插入算法,时间复杂度为O(n^2),稳定
public static void insert(Comparable[] a) {
    for (int i = 0; i < a.length; i++)
        for (int j = i; j > 0; j--)
            //保证插入排序是稳定算法
            if (less(a[j], a[j - 1]))
                exch(a, j, j - 1);
            else
                break;  //当后比前大或相等时终止内部的后续for循环,因为j之前的元素已经有序

}
//Shell排序,时间复杂度为O(n^3/2),不稳定
public static void shell(Comparable[] a) {
    //选择增量为3x+1
    int N = a.length;
    int h = 1;
    while (h < N / 3)
        h = 3 * h + 1;
    while (h >= 1) {
        for (int i = h; i < N; i++) {
            for (int j = i; j >= h && less(a[j], a[j - h]); j -= h)
                exch(a, j, j - h);
        }
        h = h / 3;
    }
}
public static void quick(Comparable[] a)
{
    quick(a,0,a.length-1);
}
//快速排序,时间复杂度O(log2n),不稳定
private static void quick(Comparable[] a, int lo, int hi) {
    if (hi <= lo)
        return;
    int j = partition(a, lo, hi);
    quick(a, lo, j - 1);
    quick(a, j + 1, hi);
}
//快速排序的partition算法,返回中间位置的索引
private static int partition(Comparable[] a, int lo, int hi) {
    int i = lo;
    int j = hi + 1;
    Comparable v = a[lo];
    while (true) {
        //即使不满足less()的条件,也会先执行++i或者--j
        while (less(a[++i], v))
            if (i == hi)
                break;
        while (less(v, a[--j]))
            if (j == lo)
                break;
        if (i >= j) break;
        exch(a, i, j);
    }
    exch(a, lo, j);
    return j;
}

//直接选择排序,时间复杂度O(n^2),不稳定
public static void select(Comparable[] a) {
    int N = a.length;
    Comparable temp = null;
    for (int i = 0; i < N - 1; i++) {
        int min = i;
        for (int j = i + 1; j < N; j++)
            if (!less(a[min], a[j]))
                min = j;
        if (min != i)
            exch(a, i, min);
    }
}
//2路归并算法,时间复杂度O(nlog2n),稳定
public static void merge(Comparable[] a) {
    int N = a.length;
    Comparable[] aux = new Comparable[N];
    /*for (int k = 0; k <N; k++) 
        aux[k] = a[k];
    不能在此处对aux赋值,因为在for循环中会使得每次传入的值
    都为merge(Comparable[]a)中的aux数组。
        */
    for(int sz=1;sz<N;sz=sz+sz)
        for(int lo = 0;lo<N-sz;lo+=sz+sz)
            merge(a,aux,lo,lo+sz-1,Math.min(lo+sz+sz-1,N-1));
}

private static void merge(Comparable[]a,Comparable[]aux, int lo, int mid, int hi) {

    for (int k = lo; k <= hi; k++) {
        aux[k] = a[k];
    }

    //归并回a数组*/
    int i = lo, j = mid + 1;
    System.out.println("\nlo: "+lo+" hi: "+hi);
    for (int k = 0; k <aux.length; k++) 
        System.out.print(aux[k]+" ") ;

    for (int k = lo; k <= hi; k++) {
        if      (i > mid)              a[k] = aux[j++];     //当左半部分指针右移超过中间,只剩右半部
        else if (j > hi)               a[k] = aux[i++];     //同上,对边界条件进行判断
        else if (less(aux[j], aux[i])) a[k] = aux[j++];     
        else                           a[k] = aux[i++];     //i=mid等情况
    }
    aux =a;
    System.out.println("aux2:");
    for (int k = 0; k <aux.length; k++) 
        System.out.print(aux[k]+" ") ;

}
//堆排序,时间复杂度O(nlog2n),不稳定
public static void heap(Comparable[] pq) {
    int N = pq.length;
    for (int k = N / 2; k >= 1; k--)
        sink(pq, k, N);
    while (N > 1) {
        heapexch(pq, 1, N);
        sink(pq, 1, --N);
    }
}

private static void sink(Comparable[] pq, int k, int N) {
    while (2 * k <= N) {
        int j = 2 * k;
        if (j < N && heapless(pq, j, j + 1)) j++; //若左节点比右节点小,则移到右节点
        if (!heapless(pq, k, j)) break; //若父节点不小于子节点则退出循环
        heapexch(pq, k, j); //交换父节点和子节点
        k = j;
    }
}
private static boolean heapless(Comparable[] pq, int i, int j) {
    return pq[i - 1].compareTo(pq[j - 1]) < 0;
}

private static void heapexch(Object[] pq, int i, int j) {
    Object swap = pq[i - 1];
    pq[i - 1] = pq[j - 1];
    pq[j - 1] = swap;
}



private static boolean less(Comparable v, Comparable w) {
    return v.compareTo(w) < 0;
}

private static void exch(Comparable[] a, int i, int j) {
    Comparable temp = null;
    temp = a[i];
    a[i] = a[j];
    a[j] = temp;
}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值