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); } } }