package arithmetic;
import java.util.ArrayList;
import java.util.List;
public class Search {
/*
* 插入排序:拿出数组中的每一个元素与这个元素之前的元素进行比较,找到插入位置进行插入操作
*/
private static <AnyType extends Comparable<? super AnyType>> void insertionSort(AnyType[] a){
int j;
int count=0;
for(int i=1;i<a.length;i++){
AnyType tmp=a[i];
for(j=i;j>0 && tmp.compareTo(a[i-1])<0;j--){
a[j]=a[j-1];
count++;
}
a[j]=tmp;
count++;
}
System.out.println(count);
}
/*
* 希尔排序:这里的希尔排序有三层for循环,希尔排序减少了不必要的数组的交换,具体的函数表示太复杂先跳过
* 理解:第一层表示比较的粒度
* 第二层确定比较的范围
* 第三层遍历找到插入位置,这里有点像插入排序
* 总结:算法难理解的话,可以debug查看值的变化查找值的变化规律,从而理解算法。
*/
private static <AnyType extends Comparable<? super AnyType>> void shellSort(AnyType[] a){
int j;
for(int gap=a.length/2;gap>0;gap/=2){
for(int i=gap;i<a.length;i++){
AnyType tmp=a[i];
for(j=i;j>=gap && tmp.compareTo(a[j-gap])<0;j-=gap){
a[j]=a[j-gap];
}
a[j]=tmp;
}
}
}
/**
* 堆排序
* 对于节点i(非根节点) 有左子节点2*i+1 有右子节点2*i+2
* ①得到一个数组,把它构建成一个二叉堆数组 →percDown()
* ②将最大的元素和最后位子的元素互换 size-1 ↑ swapReferences()
* ③循环执行②③ heapSort()
*/
private static int leftChild(int i){
return 2*i+1;
}
private static <AnyType extends Comparable<? super AnyType>> void percDown(AnyType[] a,int i,int n){
int child;
AnyType tmp;
for(tmp=a[i];leftChild(i)<n;i=child){
child=leftChild(i);
if(child!=n-1 && a[child].compareTo(a[child+1])<0){
child++;
}
if(tmp.compareTo(a[child])<0){
a[i]=a[child];
}else{
break;
}
}
a[i]=tmp;
}
private static <AnyType extends Comparable<? super AnyType>> void heapSort(AnyType[] a){
for(int i=a.length/2-1;i>=0;i--){
percDown(a, i, a.length);
}
for(int i=a.length-1;i>0;i--){
swapReferences(a,0,i); //buildHeap
percDown(a, 0, i); //deleteMax
}
}
private static <AnyType extends Comparable<? super AnyType>> void swapReferences(AnyType[] a, int from, int to) {
AnyType tmp=a[from];
a[from]=a[to];
a[to]=tmp;
}
/**
* 归并排序
*/
private static <AnyType extends Comparable<? super AnyType>> void mergeSort(AnyType[] a){
AnyType[] tmpArray=(AnyType[]) new Comparable[a.length];
mergeSort(a,tmpArray,0,a.length-1);
}
private static <AnyType extends Comparable<? super AnyType>> void mergeSort(AnyType[] a, AnyType[] tmpArray, int left, int right) {
if(left<right){
int center=(left+right)/2;
mergeSort(a,tmpArray,left,center);
mergeSort(a,tmpArray,center+1,right);
merge(a,tmpArray,left,center+1,right);
}
}
private static <AnyType extends Comparable<? super AnyType>> void merge(AnyType[] a, AnyType[] tmpArray, int leftPos, int rightPos, int rightEnd) {
int leftEnd=rightPos-1;
int tmpPos=leftPos;
int numElements=rightEnd-leftPos+1;
while(leftPos<=leftEnd && rightPos<=rightEnd){
if(a[leftPos].compareTo(a[rightPos])<0){
tmpArray[tmpPos++]=a[leftPos++];
}else{
tmpArray[tmpPos++]=a[rightPos++];
}
}
while(leftPos<=leftEnd){
tmpArray[tmpPos++]=a[leftPos++];
}
while(rightPos<=rightEnd){
tmpArray[tmpPos++]=a[rightPos++];
}
for(int i=0;i<numElements;i++,rightEnd--){
a[rightEnd]=tmpArray[rightEnd];
}
}
/*
* 快速排序
*/
public static void quickSort(List<Integer> items){
if(items.size()>1){
List<Integer> smaller=new ArrayList<>();
List<Integer> same=new ArrayList<>();
List<Integer> larger=new ArrayList<>();
Integer chosenItem = items.get(items.size()/2);
for(Integer i:items){
if(i<chosenItem){
smaller.add(i);
}else if(i>chosenItem){
larger.add(i);
}else{
same.add(i);
}
}
quickSort(smaller);
quickSort(larger);
items.clear();
items.addAll(smaller);
items.addAll(same);
items.addAll(larger);
}
}
public static void main(String[] args) {
}
}