插入排序:
public static void sort(int[] array){
for(int i=1;i<array.length;i++){
int temp=array[i];
int j=i;
for(;j>0&&array[j-1]>temp;j--){
array[j]=array[j-1];
}
array[j]=temp;
}
}
选择排序:
/**
* 其时间复杂度为O(n2)
*
*/
public static void sort(int[] array){
int length=array.length;
for(int i=0;i<length;i++){
int minIndex=i;
for(int j=i+1;j<length;j++){
if(array[j]<array[minIndex]){
minIndex=j;
}
}
if(minIndex!=i){
int temp=array[minIndex];
array[minIndex]=array[i];
array[i]=temp;
}
}
}
堆建立与排序
```
/**
*
* 堆:
* 1、插入操作:
* 树的高度为log(n+1),时间复杂度为O(logn)
* 2、删除操作:
* 树的高度为log(n+1),时间复杂度为O(logn)
* 3、堆排序:
* 进行n次插入,时间复杂度为O(nlogn)
* 遍历删除的时间n*logn,时间复杂度为O(nlogn)
* 平均时间复杂度为:O(nlogn)
*/
public class Test {
public static void main(String[] args) {
Heap heap=new Heap(20);
heap.insert(2);
heap.insert(4);
heap.insert(9);
heap.insert(3);
heap.insert(5);
heap.printAll();
heap.delete();
heap.printAll();
heap.sort();
}
}
class Heap{
private int[] element;
public Heap(int maxsize){
element=new int[maxsize+1];
element[0]=0;
}
public boolean isEmpty(){
return element[0]==0;
}
public boolean isFull(){
return element[0]==element.length-1;
}
public void insert(int value){
if(isFull()){
throw new IndexOutOfBoundsException("大顶堆已满");
}
if(isEmpty()){
element[1]=value;
}else{
int i=element[0]+1;
while(i!=1&&value>element[i/2]){
element[i]=element[i/2];
i/=2;
}
element[i]=value;
}
element[0]++;
}
public int delete(){
if(isEmpty()){
throw new IndexOutOfBoundsException("大顶堆已空");
}
int deleteElement=element[1];
element[1]=element[element[0]];
element[0]--;
int temp=element[1];
int parent=1;
int child=2;
while(child<=element[0]){
if(child<element[0]&&element[child]<element[child+1]){
child++;
}
if(temp>element[child]){
break;
}else{
element[parent]=element[child];
parent=child;
child*=2;
}
}
element[parent]=temp;
return deleteElement;
}
public void printAll(){
for(int i=1;i<element[0]+1;i++){
System.out.print(element[i]);
if(i!=element[0]){
System.out.print(",");
}
}
System.out.println();
}
public void sort(){
if(isEmpty()){
throw new RuntimeException("堆为空");
}
int size=element[0];
for(int i=0;i<size;i++){
int deleteElement=delete();
element[element[0]+1]=deleteElement;
}
for(int i=1;i<size+1;i++){
System.out.print(element[i]);
if(i!=size){
System.out.print(",");
}
}
}
}
归并排序:
/**
*
* 归并排序是稳定排序,它也是一种十分高效的排序,能利用完全二叉树特性的排序一般性能都不会太差。
* java中Arrays.sort()采用了
一种名为TimSort的排序算法,就是归并排序的优化版本。
* 每次合并操作的平均时间复杂度为O(n),
* 总的平均时间复杂度为O(nlogn)。而且,归并排序的最好,最坏,平均时间复杂度均为O(nlogn)。
*
* 参考:: <http://www.cnblogs.com/chengxiao/>
*/
public class Test {
public static void main(String[] args) {
int []arr = {9,8,7,6,5,4,3,2,1};
MergeSort m=new MergeSort();
m.sort(arr);
System.out.println(Arrays.toString(arr));
}
}
class MergeSort {
public static void sort(int []arr){
int []temp = new int[arr.length];
sort(arr,0,arr.length-1,temp);
}
private static void sort(int[] arr,int left,int right,int []temp){
if(left<right){
int mid = (left+right)/2;
sort(arr,left,mid,temp);
sort(arr,mid+1,right,temp);
merge(arr,left,mid,right,temp);
}
}
private static void merge(int[] arr,int left,int mid,int right,int[] temp){
int i = left;
int j = mid+1;
int t = 0;
while (i<=mid && j<=right){
if(arr[i]<=arr[j]){
temp[t++] = arr[i++];
}else {
temp[t++] = arr[j++];
}
}
while(i<=mid){
temp[t++] = arr[i++];
}
while(j<=right){
temp[t++] = arr[j++];
}
t = 0;
while(left <= right){
arr[left++] = temp[t++];
}
}