zoukankan      html  css  js  c++  java
  • 排序 没事练习

      1 package com.taotao.api;
      2 
      3 import org.junit.Test;
      4 
      5 import java.util.Arrays;
      6 import java.util.List;
      7 
      8 public class SortTest2 {
      9 
     10 
     11     @Test
     12     public void sortInsert() {
     13 
     14         int[] array = new int[5];
     15 
     16         array[0] = 2;
     17         array[1] = 1;
     18         array[2] = 4;
     19         array[3] = 5;
     20         array[4] = 1;
     21 
     22         for (int i = 0; i < array.length; i++) {
     23             //查找合适位置
     24             int point = i;
     25             for (int j = 0; j < i; j++) {
     26                 if (array[i] < array[j]) {
     27                     point = j;
     28                     break;
     29                 }
     30             }
     31             if (i == point) continue;
     32 
     33             int temp = array[i];
     34             //序列后移
     35             for (int j = i; j > point; j--) {
     36                 array[j] = array[j - 1];
     37             }
     38             //赋值合适位置
     39             array[point] = temp;
     40         }
     41 
     42         for (int i : array) {
     43             System.out.print(i);
     44         }
     45 
     46     }
     47 
     48     @Test
     49     public void sortQuick() {
     50         List<Integer> arrayList = Arrays.asList(15,17,2,4,3);
     51         int[] list = arrayList.stream().mapToInt(x -> x).toArray();
     52         quick(list, 0, list.length - 1);
     53     }
     54 
     55     private void quick(int[] list, int left, int right) {
     56 
     57         int head = left;
     58         int tail = right;
     59 
     60         //以temp 作为对比值
     61         Integer temp = list[head];
     62         while (head < tail) {
     63             //从后往前 找最近一个小于 对比值
     64             //并将 此值 扔到 head位置
     65             while (head < tail && list[tail] >= temp) tail--;
     66             if (head != tail) {
     67                 list[head] = list[tail];
     68             }
     69             //从前往后 找大于对比值的
     70             //扔到 tail 位置
     71             while (head < tail && list[head] <= temp) head++;
     72             if (head != tail) {
     73                 list[tail] = list[head];
     74             }
     75         }
     76         //将对比值 扔到 合适位置
     77         if (head == tail) list[head] = temp;
     78 
     79         //Stream.of(list).forEach(x-> System.out.println(x+","));
     80         for (int i : list) {
     81             System.out.print(i);
     82             System.out.print(",");
     83         }
     84         System.out.println("");
     85 
     86         if (left > right) return;
     87 
     88         //递归 左块
     89         if (left < head) {
     90             quick(list, left, head);
     91         }
     92         //递归右块
     93         if (head + 1 < right) {
     94             quick(list, head + 1, right);
     95         }
     96 
     97     }
     98 
     99     /***
    100      * 交换排序
    101      */
    102     @Test
    103     public void sortNormal() {
    104 
    105         List<Integer> arrayList = Arrays.asList(25, 10, 22, 30, 45, 53, 12, 43);
    106         int[] list = arrayList.stream().mapToInt(x -> x).toArray();
    107 
    108 
    109         for (int i = 0; i < list.length; i++) {
    110             for (int j = 0; j < i; j++) {
    111                 if (list[i] <= list[j]) {
    112                     int temp = list[i];
    113                     list[i] = list[j];
    114                     list[j] = temp;
    115                 }
    116             }
    117         }
    118 
    119         for (int i : list) {
    120             System.out.print(i);
    121             System.out.print(",");
    122         }
    123         System.out.println("");
    124     }
    125 
    126 
    127 
    128 }
  • 相关阅读:
    springmvc的控制器是不是单例模式,如果是,有什么问题,怎么解决?
    数据库中的锁机制
    在inux中安装redis的时候,会出现下面的这个异常
    使用SecureCRT操作linux系统时候的简单设置
    装饰者设计模式
    java.sql.SQLException: The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone.
    事务
    2.6.1 测试架构师
    测试专家讲述通往测试架构师之路
    什么是软件测试架构师
  • 原文地址:https://www.cnblogs.com/rufus-hua/p/8541913.html
Copyright © 2011-2022 走看看