import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
public class InsertSort {
public static void display(int[] arr){
System.out.print("[");
for(int i=0;i<arr.length;i++){
if(i == arr.length-1){
System.out.print(arr[i]);
}else{
System.out.print(arr[i]+",");
}
}
System.out.print("]");
}
/**
* 直接插入排序
* 原理:
* 在要排序的一組數中,假設前面(n-1)[n>=2] 個數已經是排 好順序的,
* 現在要把第n 個數插到前面的有序數中,使得這 n個數 也是排好順序的。
* 如此反復循環,直到全部排好順序。
*/
public void insertSort1(int[] arr){
if(arr.length<2 || arr == null){
return;
}
for(int i=1;i<arr.length;i++){
int current = arr[i];
int position = i;
for(int j=i-1;j>=0;j--){
if(arr[j]>arr[j+1]){
arr[j+1] = arr[j];
position -=1;
}else{
break;
}
arr[position] = current;
}
}
}
//測試插入排序
@Test
public void testInsertSort(){
int[] arr = {4,9,5,2,8,6,1};
display(arr);
System.out.println();
insertSort1(arr);
display(arr);
}
/*
* 希爾排序(最小增量排序)
* 算法先將要排序的一組數按某個增量 d(n/2,n為要排序數的個數)分成若干組,
* 每組中記錄的下標相差 d.對每組中全部元素進行直接插入排序,然后再用一個較小的增量(d/2)對它進行分組,
* 在每組中再進行直接插入排序。當增量減到 1 時,進行直接插入排序后,排序完成。
*/
public void ShellSort(int[] arr){
int h = 1;
while(h<arr.length/3){
h = h*3 +1;
}
while(h> 0){
int temp = 0;
for(int i=h;i<arr.length;i++){
temp = arr[i];
int j = i;
while(j>h-1 && arr[j-h]>= temp){
arr[j] = arr[j-h];
j -= h;
}
arr[j]= temp;
}
h = (h-1)/3;
}
}
//測試希爾排序
@Test
public void testShellSort(){
int[] arr={1,54,6,3,78,34,12,45,56,100};
ShellSort(arr);
display(arr);
}
/**
* 簡單選擇排序
* 基本思想:在要排序的一組數中,選出最小的一個數與第一個位置的數交換;
* 然后在剩下的數當中再找最小的與第二個位置的數交換,如此循環到倒數第二個數和最后一個數比較為止。
*/
public void SelectSort(int[] arr){
int k = 0;
int temp = 0;
for(int i=0;i<arr.length-1;i++){
k = i;
for(int j=i;j<arr.length;j++){
if(arr[j]<arr[k]){
k = j;
}
}
temp = arr[i];
arr[i] = arr[k];
arr[k] = temp;
}
}
//測試簡單排序
@Test
public void testSelectSort(){
int[] arr={1,54,6,3,78,34,12,45,56,100};
SelectSort(arr);
display(arr);
}
/**
* 堆排序
* 堆排序是一種樹形選擇排序,是對直接選擇排序的有效改進。
* 堆的定義如下:具有n個元素的序列(h1,h2,...,hn),當且僅當滿足(hi>=h2i,hi>=2i+1)
* 或(hi<=h2i,hi<=2i+1)(i=1,2,...,n/2)時稱之為堆。在這里只討論滿足前者條件的堆。
* 由堆的定義可以看出,堆頂元素(即第一個元素)必為最大項(大頂堆)。
* 完全二叉樹可以很直觀地表示堆的結構。堆頂為根,其它為左子樹、右子樹。
* 初始時把要排序的數的序列看作是一棵順序存儲的二叉樹,調整它們的存儲序,使之成為一個堆,這時堆的根節點的數最大。
* 然后將根節點與堆的最后一個節點交換。然后對前面(n-1)個數重新調整使之成為堆。
* 依此類推,直到只有兩個節點的堆,並對它們作交換,最后得到有 n個節點的有序序列。
* 從算法描述來看,堆排序需要兩個過程,一是建立堆,二是堆頂與堆的最后一個元素交換位置。
* 所以堆排序有兩個函數組成。一是建堆的滲透函數,二是反復調用滲透函數實現排序的函數。
*/
public void HeapSort(int[] arr){
System.out.println("開始排序");
int length = arr.length;
for(int i=0;i<length-1;i++){
buildMaxHeap(arr,length-1-i);
swap(arr,0,length-1-i);
display(arr);
System.out.println();
}
}
private void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
private void buildMaxHeap(int[] arr, int lastIndex) {
for(int i=(lastIndex-1)/2;i>=0;i--){
int k=i;
while(k*2+1<=lastIndex){
int biggerIndex = 2*k+1;
if(biggerIndex<lastIndex){
if(arr[biggerIndex]<arr[biggerIndex+1]){
biggerIndex++;
}
}
if(arr[k]<arr[biggerIndex]){
swap(arr,k,biggerIndex);
k=biggerIndex;
}else{
break;
}
}
}
}
//測試
@Test
public void testHeap(){
int a[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};
display(a);
HeapSort(a);
System.out.println();
display(a);
}
/**
* 冒泡排序
* 在要排序的一組數中,對當前還未排好序的范圍內的全部數,
* 自上而下對相鄰的兩個數依次進行比較和調整,讓較大的數往下沉,
* 較小的往上冒。即:每當兩相鄰的數比較后發現它們的排序與排序要求相反時,
* 就將它們互換。
*/
public void bubbleSort(int[] arr){
for(int i=0;i<arr.length-1;i++){
for(int j=arr.length-1;j>i;j--){
if(arr[j]<arr[j-1]){
int temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
}
}
}
}
//測試冒泡排序
@Test
public void testbubbleSort(){
int[] arr={1,54,6,3,78,34,12,45,56,100};
bubbleSort(arr);
display(arr);
}
/**
*
* 快速排序
* 選擇一個基准元素,通常選擇第一個元素或者最后一個元素,通過一趟掃描,
* 將待排序列分成兩部分,一部分比基准元素小,一部分大於等於基准元素,
* 此時基准元素在其排好序后的正確位置,然后再用同樣的方法遞歸地排序划分的兩部分。
*/
public static int partion(int[] arr,int left,int right,int point){
int leftPtr = left -1;
int rightPtr = right;
while(true){
while(leftPtr <rightPtr && arr[++leftPtr] < point);
while(leftPtr <rightPtr && arr[--rightPtr] > point);
if(leftPtr>=rightPtr){
break;
}else{
int temp = arr[leftPtr];
arr[leftPtr] = arr[rightPtr];
arr[rightPtr] = temp;
}
}
int temp = arr[leftPtr];
arr[leftPtr] = arr[right];
arr[right] = temp;
return leftPtr;
}
public void sort(int arr[],int left,int right){
if(right - left <=0){
return;
}else{
int point = arr[right];
int partion = partion(arr,left,right,point);
sort(arr,left,partion-1);
sort(arr,partion+1,right);
}
}
//測試快速排序
@Test
public void testQuerySort(){
int[] arr = new int[10];
for(int i=0;i<10;i++){
arr[i] = (int)(Math.random()*100);
}
display(arr);
sort(arr,0,arr.length-1);
System.out.println();
display(arr);
}
/**
* 歸並排序
* 歸並(Merge)排序法是將兩個(或兩個以上)有序表合並成一個新的有序表,
* 即把待排序序列分為若干個子序列,每個子序列是有序的。
* 然后再把有序子序列合並為整體有序序列。
*/
public void mergingSort(int[] arr){
}
public void sort1(int[] data, int left, int right) {
// TODO Auto-generatedmethod stub
if(left<right){
//找出中間索引
int center=(left+right)/2;
//對左邊數組進行遞歸
sort1(data,left,center);
//對右邊數組進行遞歸
sort1(data,center+1,right);
//合並
merge(data,left,center,right);
}
}
private void merge(int[] data, int left, int center, int right) {
int [] tmpArr=new int[data.length];
int mid=center+1;
//third 記錄中間數組的索引
int third=left;
int tmp=left;
while(left<=center&&mid<=right){
//從兩個數組中取出最小的放入中間數組
if(data[left]<=data[mid]){
tmpArr[third++]=data[left++];
}else{
tmpArr[third++]=data[mid++];
}
}
//剩余部分依次放入中間數組
while(mid<=right){
tmpArr[third++]=data[mid++];
}
while(left<=center){
tmpArr[third++]=data[left++];
}
//將中間數組中的內容復制回原數組
while(tmp<=right){
data[tmp]=tmpArr[tmp++];
}
System.out.println(Arrays.toString(data));
}
//歸並排序
@Test
public void testmergingSort(){
int[] a={49,38,65,97,76,13,27,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};
sort1(a,0,a.length-1);
}
/**
* 基數排序
* 將所有待比較數值(正整數)統一為同樣的數位長度,數位較短的數前面補零。
* 然后,從最低位開始,依次進行一次排序。這樣從最低位排序一直到最高位排序完成以后,
* 數列就變成一個有序序列。
*/
public void radixSort(int[] array){
int max=array[0];
for(int i=0;i<array.length;i++){
if(array[i]>max){
max = array[i];
}
}
int time=0;
while(max>0){
max/=10;
time++;
}
List<ArrayList> queue = new ArrayList();
for(int i=0;i<10;i++){
ArrayList<Integer> queue1 = new ArrayList<Integer>();
queue.add(queue1);
}
for(int i=0;i<time;i++){
for(int j=0;j<array.length;j++){
int x = array[j]%(int)Math.pow(10,i+1)/(int)Math.pow(10, i);
ArrayList<Integer> queue2 =queue.get(x);
queue2.add(array[j]);
queue.set(x, queue2);
}
int count = 0;
for(int k=0;k<10;k++){
while(queue.get(k).size()>0){
ArrayList<Integer> queue3 = queue.get(k);
array[count] = queue3.get(0);
count++;
}
}
}
}
//基數排序
@Test
public void testRadix(){
int a[]={49,38,65,97,76,13,27,49,78};
display(a);
radixSort(a);
System.out.println();
display(a);
}
//二分查找
public int binarySearch(int[] arr,int value){
int start = 0;
int end = arr.length-1;
while(start<=end){
int middle = (start+end)/2;
if(arr[middle]==value){
return middle;
}else if(arr[middle]>value){
end = middle-1;
}else {
start = middle +1;
}
}
return -1;
}
public int binarySearch2(int[] arr,int start,int end,int value){
while(start<=end){
int middle = (start+end)/2;
if(arr[middle]==value){
return middle;
}else if(arr[middle]>value){
end = middle-1;
binarySearch2(arr,start, end, 3);
}else {
start = middle +1;
binarySearch2(arr,start, end, 3);
}
}
return -1;
}
@Test
public void testBinary(){
int[] arr = {1,2,3,4,5};
display(arr);
int a = binarySearch2(arr,0,arr.length,3);
System.out.println(a);
display(arr);
}
}
