/**

 * 冒泡

 * 两两比较相邻记录,如果反序则交换,直到没有反序的记录为止

 */

public class bubble {

    public static void bubblesort(int[] sort){

        int tmp=0;

        for (int i = 0; i < sort.length-1; i++) {

            for (int j = 0; j < sort.length-i-1; j++) {

                if(sort[j]>sort[j+1]){

                    tmp=sort[j];

                    sort[j]=sort[j+1];

                    sort[j+1]=tmp;

                }

            }

        }

    }


    public static void main(String[] args) {

        int sort[]={1,45,78,90,2,15,1000};

        bubblesort(sort);

        System.out.println(Arrays.toString(sort));

    }

}



----------------------------------------------------------------------------------------------------------------------------------


/**

 * 插入排序

 * 依次将待排序序列中的每个记录插入到一个已排好序的序列中,直到全部序列排好序

 */

public class InsertSort {


    // 插入排序

    public static void insertSort(int[] arr) {

       int select=0;

        for (int i = 1; i < arr.length; i++) {

            select=arr[i];

            int j=0;

            for (j=i;j>0 && arr[j-1]>=select;j--) {

                arr[j]=arr[j-1];//跟前面比较,小值保留,大值后移

            }

            arr[j]=select;

        }

    }

    public static void main(String[] args) {

        int sort[]={1,45,2,15,1000};

        insertSort(sort);

        System.out.println(Arrays.toString(sort));

    }

}


----------------------------------------------------------------------------------------------------------------------------------

/**

 * 对象排序

 * 用的选择排序的思想

 */

public class StudentSort {


    private Student[] stus=new Student[50];

    private int length=0;

    public  void insert(Student stu){

        stus[length]=stu;

        length++;

    }


    public void sortByNo(){

        int min=0;

        Student tmp=null;

        for (int i = 0; i <length-1; i++) {

            min=i;

            for (int j = i+1; j < length; j++) {

                if(stus[j].getStuNo() < stus[min].getStuNo()){

                    min=j;

                }

            }

            tmp=stus[min];

            stus[min]=stus[i];

            stus[i]=tmp;

        }

    }

    public void sortByName(){

        int min=0;

        Student tmp=null;


        for (int i = 0; i < length-1; i++) {

            min=i;

            for (int j = i+1; j < length; j++) {

                if(stus[j].getName().compareTo(stus[min].getName()) < 0){

                    min=j;

                }

            }


            tmp=stus[min];

            stus[min]=stus[i];

            stus[i]=tmp;

        }

    }


    public void display(){

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

            System.out.println(stus[i].toString());

        }

    }



    public static void main(String[] args) {

        Student stu1=new Student(11,"d张三","男",12);

        Student stu2=new Student(4,"c李四","男",12);

        Student stu3=new Student(20,"b王五","男",12);

        Student stu4=new Student(10,"a赵六","男",12);


        StudentSort studentSort=new StudentSort();

        studentSort.insert(stu1);

        studentSort.insert(stu2);

        studentSort.insert(stu3);

        studentSort.insert(stu4);



        //studentSort.sortByNo();

        studentSort.sortByName();

        studentSort.display();



    }

}


Student实体属性:stuNo,name,sex,age


扩展:中文排序怎么排?

----------------------------------------------------------------------------------------------------------------------------------

/**

 * 选择排序

 * 第i趟排序在无序序列r1-rn中找到值最小的记录,并和第i个记录交换作为有序序列的第i个记录

 */

public class Select {

    public static void selectSort(int[] sort){

        int tmp=0;

        int min=0;

        for (int i = 0; i < sort.length-1; i++) {

            min=i;

            for (int j = i+1; j < sort.length; j++) {

                if(sort[j]<sort[min]){

                    min=j;//找最小

                }

            }

            tmp=sort[i];

            sort[i]=sort[min];

            sort[min]=tmp;

        }

    }


    public static void main(String[] args) {

        int sort[]={1,45,78,90,2,15,1000};

        selectSort(sort);

        System.out.println(Arrays.toString(sort));



    }

}


----------------------------------------------------------------------------------------------------------------------------------


/**

 * 单词逆序

 */

public class Reverse {

    private String input;

    public void insert(String input){

        this.input=input;

    }

    public String doReverse(){

        CharStack cs=new CharStack(input.length());

        for (int i = 0; i < input.length(); i++) {

            char ch=input.charAt(i);

            cs.push(ch);

        }

        String ouput="";

        while (!cs.isEmpty()){

            char ch=cs.pop();//弹栈

            ouput+=ch;

        }

        return ouput;

    }


    public static void main(String[] args) {

        Reverse reverse=new Reverse();

        reverse.insert("abcdef");

        System.out.println(reverse.doReverse());

    }


}

/**

 * 单词逆序字符

 */

public class CharStack {

    private char[] stack;

    private int top;

    private int maxSize;

    public CharStack() {

    }

    public CharStack(int maxSize) {

        stack=new char[maxSize];

        top=-1;

    }


    //压栈

    public void push(char arr){

        stack[++top]=arr;

    }

    //弹出数据

    public char pop(){

        return stack[top--];

    }


    //访问栈顶元素

    public char peek(){

        return stack[top];

    }


    public boolean isEmpty(){

        return (top <= -1);

    }

    //占满

    public boolean isFull(){

        return (top == maxSize-1);

    }

}


----------------------------------------------------------------------------------------------------------------------------------


/**

 * 栈

 */

public class MyStack {

    private long[] stack;

    private int top;

    private int maxSize;

    public MyStack() {

    }

    public MyStack(int maxSize) {

        stack=new long[maxSize];

        top=-1;

    }


    //压栈

    public void push(int arr){

        stack[++top]=arr;

    }

    //弹出数据

    public long pop(){

        return stack[top--];

    }

    //访问栈顶元素

    public long peek(){

        return stack[top];

    }

    public boolean isEmpty(){

        return (top <= -1);

    }

    //占满

    public boolean isFull(){

        return (top == maxSize-1);

    }

}


/**

 * 后进先出

 * 先进后出

 */

public class stackSort {

    public static void main(String[] args) {

        MyStack myStack=new MyStack(10);


        myStack.push(10);

        myStack.push(2);

        myStack.push(20);

        myStack.push(40);

        myStack.push(50);


        if(!myStack.isEmpty()){

            System.out.println("没弹出前的栈顶:"+myStack.peek());

            System.out.println("弹出的栈顶:"+myStack.pop());

            System.out.println("弹出后的栈顶:"+myStack.peek());

        }


        while (!myStack.isEmpty()){

            System.out.println("剩余全部弹出:"+myStack.pop());


        }

    }

}