排序 沒事練習

 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 }
相關文章
相關標籤/搜索