直接插入排序:将待排序数据插入已排好序的数据中合适位置;
希尔排序不稳定!!!
BinaryInsertSort.java (二分插入排序)
package com.camunda.annotation;
public class BinaryInsertSort {
public static void main(String[] args) {
int[] a={49,38,65,97,176,213,227,49,78,34,12,164,11,18,1};
System.out.println("排序之前:");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+" ");
}
//二分插入排序
sort(a);
System.out.println();
System.out.println("排序之后:");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+" ");
}
}
//二分法插入
private static void sort(int[] a) {
for (int i = 0; i < a.length; i++) {
int temp = a[i];
int left = 0;
int right = i-1;
int mid = 0;
//确定要插入的位置
while(left<=right){
//先获取中间位置
mid = (left+right)/2;
if(temp<a[mid]){
//如果值比中间值小,让right左移到中间下标-1
right = mid-1;
}else{
//如果值比中间值大,让left右移到中间下标+1
left = mid+1;
}
}
for (int j = i-1; j >= left; j--) {
//以左下标为标准,在左位置前插入该数据,左及左后边全部后移
a[j+1] = a[j];
}
if(left != i){
//左位置插入该数据
a[left] = temp;
}
}
}
}
运行结果:
排序之前:49 38 65 97 176 213 227 49 78 34 12 164 11 18 1
排序之后:1 11 12 18 34 38 49 49 65 78 97 164 176 213 227
HeapSort.java (堆排序)
package com.dn.sort;
//堆排序
public class HeapSort { //(1)
public static void main(String[] args) {
HeapSort heapSort = new HeapSort();
int[] array = { 19, 8, 27, 6, 35, 14, 3, 12, 1, 0, 9, 10, 7 };
System.out.println("Before heap:");
heapSort.printArray(array);
heapSort.heapSort(array);
System.out.println("After heap sort:");
heapSort.printArray(array);
}
public void heapSort(int[] array) {
if (array == null || array.length <= 1) {
return;
}
buildMaxHeap(array);//建立最大堆
for (int i = array.length - 1; i >= 1; i--) {
//最大的在0位置,那么开始沉降,这样每交换一次最大的值就丢到最后了
exchangeElements(array, 0, i);
//继续获取0位置最大值
maxHeap(array, i, 0);
}
}
//(2) //建立最大堆
private void buildMaxHeap(int[] array) {
if (array == null || array.length <= 1) {
return;
}
int half = (array.length-1) / 2;
for (int i = half; i >= 0; i--) {
maxHeap(array, array.length, i);
}
}
private void maxHeap(int[] array, int heapSize, int index) {
int left = index * 2 + 1;
int right = index * 2 + 2;
int largest = index;
if (left < heapSize && array[left] > array[index]) {
largest = left;
}
if (right < heapSize && array[right] > array[largest]) {
largest = right;
}
if (index != largest) {
exchangeElements(array, index, largest);
maxHeap(array, heapSize, largest);
}
}
//(3)
public void printArray(int[] array) {
System.out.print("{");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i]);
if (i < array.length - 1) {
System.out.print(", ");
}
}
System.out.println("}");
}
public void exchangeElements(int[] array, int index1, int index2) {
int temp = array[index1];
array[index1] = array[index2];
array[index2] = temp;
}
}
运行结果:
Before heap:
{19, 8, 27, 6, 35, 14, 3, 12, 1, 0, 9, 10, 7}
After heap sort:
{0, 1, 3, 6, 7, 8, 9, 10, 12, 14, 19, 27, 35}
HeerSort.java (希尔排序)
package com.camunda.annotation;
//不稳定
public class HeerSort {
//希尔排序
public static void main(String[] args) {
int[] a={49,38,65,97,76,13,27,49,78,34,12,64,1,33,85,29};
System.out.println("排序之前:");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+" ");
}
//希尔排序
System.out.println();
int d = a.length/2;
while(true){
for(int i=0;i<d;i++){
for(int j=i;j+d<a.length;j+=d){
int temp;
if(a[j]>a[j+d]){
temp=a[j];
a[j]=a[j+d];
a[j+d]=temp;
}
}
}
if(d==1){break;}
d--;
}
System.out.println();
System.out.println("排序之后:");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+" ");
}
}
}
运行结果:
排序之前:49 38 65 97 76 13 27 49 78 34 12 64 1 33 85 29
排序之后:1 12 13 27 29 33 34 38 49 49 64 65 76 78 85 97
InsertSort.java (插入排序)
package com.camunda.annotation;
public class InsertSort {
/**
* 直接插入排序
* @param args
*/
public static void main(String[] args) {
int[] a={49,38,65,97,76,13,27,49,78,34,12,64,1};
System.out.println("排序之前:");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+" ");
}
//直接插入排序
for (int i = 1; i < a.length; i++) {
//待插入元素
int temp = a[i];
int j;
for (j = i-1; j>=0; j--) {
//将大于temp的往后移动一位
if(a[j]>temp){
a[j+1] = a[j];
}else{
break;
}
}
a[j+1] = temp;//插入进来
}
System.out.println();
System.out.println("排序之后:");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+" ");
}
}
}
运行结果:
排序之前:49 38 65 97 76 13 27 49 78 34 12 64 1
排序之后:1 12 13 27 34 38 49 49 64 65 76 78 97
SelertSort.java (选择排序)
package com.camunda.annotation;
public class SelertSort {
public void selectSort(int[] array) {
int min;
int tmp = 0;
for (int i = 0; i < array.length; i++) {
min = array[i];
for (int j = i; j < array.length; j++) {
if (array[j] < min) {
min = array[j];//最小值
tmp = array[i];
array[i] = min;
array[j] = tmp;
}
}
}
for(int num:array){
System.out.println(num);
}
}
public static void main(String [] args){
SelertSort selertSort = new SelertSort();
selertSort.selectSort(new int[]{9,4,2,6,7,3,10,33,88,1,17});
}
}
运行结果:
1
2
3
4
6
7
9
10
17
33
88
QuickSort.java (快速排序)
package com.camunda.annotation;
public class QuickSortDemo {
public void quick(int [] a){
if(a.length>0){
quickSort(a,0,a.length-1);
}
}
/**
* 快速排序
* @param a
* @param i
* @param j
*/
private void quickSort(int[] a, int low, int high) {
if(low<high){
int middle = getMiddle(a,low,high);
quickSort(a, 0, middle-1);
quickSort(a,middle+1,high);
}
}
/**
* 获取中间下标
* @param a
* @param low
* @param high
* @return
*/
private int getMiddle(int[] a, int low, int high) {
int temp = a[low];//基准元素
while(low<high){
while(low<high&&a[high]>=temp){
high--;
}
a[low] = a[high];
while(low<high&&a[low]<=temp){
low++;
}
a[high] = a[low];
}
a[low] = temp;//插入到排序后正确的位置
return low;
}
public static void main(String[] args){
QuickSortDemo quickSort = new QuickSortDemo();
int [] a = {19,2,3,90,67,33,-7,24,3,56,34,5};
quickSort.quick(a);
for(int num :a){
System.out.println(" "+num);
}
}
}
运行结果:
-7
2
3
3
5
19
24
33
34
56
67
90
BinarySearch.java(二分查找)
package com.camunda.annotation;
public class BinarySearchDemo02 {
/**
* 递归的方式
* @param elem
* @param array
* @param low
* @param high
* @return
*/
public int binarySearch(int elem,int [] array,int low,int high){
if(low>high){
return -1;
}
int middle = (low+high)/2;
if(array[middle] == elem){
System.out.println("找到对应元素值,下标为:"+middle);
return middle;
}
if(array[middle]<elem){
//找右边
return binarySearch(elem, array, middle+1, high);
}
if(array[middle]>elem){
//找左边
return binarySearch(elem, array, low, middle-1);
}
return -1;
}
/**
* 非递归
* @param args
*/
public int directBinarySearch(int[] array,int elem){
int low = 0;
int high = array.length-1;
while(low<=high){
int middle = (low+high)/2;
if(elem>array[middle]){
//右边找
low = middle+1;
}else if(elem<array[middle]){
high = middle - 1;
}else{
System.out.println("找到相应元素,下标为:"+middle);
return middle;
}
}
return -1;
}
public static void main(String[] args){
BinarySearchDemo02 binarySearch = new BinarySearchDemo02();
int [] array = {10,23,4,3,2,5,1,2,623,92,23,23,234,2,34,234,234,2,10};
BasicSort basicSort = new BasicSort();
basicSort.sort(array);
for(int n:array){
System.out.print(" "+n);
}
// binarySearch.binarySearch(5, array, 0, array.length-1);
binarySearch.directBinarySearch(array, 2);
}
}
运行结果:
1 2 2 2 2 3 4 5 10 10 23 23 23 34 92 234 234 234 623找到相应元素,下标为:4