码迷,mamicode.com
首页 > 编程语言 > 详细

排序 没事练习

时间:2018-03-10 21:57:13      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:col   []   unit   arrays   set   练习   display   point   pos   

package com.taotao.api;

import org.apache.commons.lang3.StringUtils;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;

public class SortTest2 {


    @Test
    public void sortInsert() {

        int[] array = new int[5];

        array[0] = 2;
        array[1] = 1;
        array[2] = 4;
        array[3] = 5;
        array[4] = 1;

        for (int i = 0; i < array.length; i++) {
            //查找合适位置
            int point = i;
            for (int j = 0; j < i; j++) {
                if (array[i] < array[j]) {
                    point = j;
                    break;
                }
            }
            if (i == point) continue;

            int temp = array[i];
            //序列后移
            for (int j = i; j > point; j--) {
                array[j] = array[j - 1];
            }
            //赋值合适位置
            array[point] = temp;
        }

        for (int i : array) {
            System.out.print(i);
        }

    }

    @Test
    public void sortQuick() {
        List<Integer> list = Arrays.asList(25, 10, 22, 30, 45, 53, 12, 43);
        quick(list, 0, list.size() - 1);
    }

    private void quick(List<Integer> list, int left, int right) {

        int head=left;
        int tail=right;

        Integer temp = list.get(head);
        while (head < tail) {
            while (head < tail && list.get(tail) >= temp) tail--;
            if (head != tail) {
                list.set(left, list.get(tail));
            }

            while (head < tail && list.get(head) <= temp) head++;
            if (head != tail) {
                list.set(tail, list.get(head));
            }
        }
        if (head == tail) list.set(head, temp);

        System.out.println(StringUtils.join(list,","));

        if (left > right) return;

        if (left < head) {
            quick(list, left, head);
        }

        if (head + 1 < right) {
            quick(list, head + 1, right);
        }

    }
}

 

 

排序 没事练习

标签:col   []   unit   arrays   set   练习   display   point   pos   

原文地址:https://www.cnblogs.com/rufus-hua/p/8541913.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!