排序算法总结

http://www.sorting-algorithms.com

七大算法总结(只是跑过这一个例子,如有不足,欢迎指正):

代码如下:



public class Sort {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] array = {4,1,5,6,9,3,7};
		print(array);
		System.out.println();
		//insert(array);
		//select(array);
		//shell(array);
		//bubble(array);
		//quick(array,0,array.length-1);
		//merge(array,0,array.length-1);
		heapSort(array);
		print(array);
		

	}
	//insert sort algorithm
	public static void insert(int[] array){
		int length = array.length;
		for(int i=2;i<length;i++)
			for(int j=i;j>=1;j--){
				
				if(array[j]<array[j-1]){
					swap(array,j,j-1);
				}
			}
		
		return ;
	}
	//select sort algorithm
	public static void select(int[] array){
		int length = array.length;
		for(int i=0;i<length;i++){
			int k = i;
			for(int j=i+1;j<length;j++){
				if(array[j]<array[k])
					k = j;
			}
			swap(array,i,k);
		}
	}
	//shell sort algorithm
	public static void shell(int[] array){
		int length = array.length;
		int d = length/2;
		while(d>0){
			for(int i=d;i<length;i++){
				for(int j=i-d;j>=0;j-=d)
					if(array[j]>array[j+d])
						swap(array,j,j+d);
			}
			d=d/2;
		}
	}
	//bubble sort algorithm
	public static void bubble(int[] array){
		int length = array.length;
		for(int i=0;i<length-1;i++)
			for(int j=i+1;j<length;j++)
				if(array[i]>array[j])
					swap(array,i,j);
	}
	//quick sort algorithm
	public static void quick(int[] array, int left, int right){
		 int i, j, temp;
	        i = left;
	        j = right;
	        if (left > right)
	            return;
	        temp = array[left];
	        while (i != j)
	        {
	            while (array[j] >= temp && j > i)
	                j--;
	            if (j > i)
	                array[i++] = array[j];
	            while (array[i] <= temp && j > i)
	                i++;
	            if (j > i)
	                array[j--] = array[i];

	        }
	        array[i] = temp;
	        quick(array, left, i - 1);
	        quick(array, i + 1, right);
	}
	//merge sort algorithm
	public static void merge(int[] array, int first, int last){
		 if (first < last)  
		    {  
		        int mid = (first + last) / 2;  
		        merge(array, first, mid);    //左边有序  
		        merge(array, mid + 1, last); //右边有序  
		        mergeArray(array, first, mid, last); //再将二个有序数列合并  
		    }  
	}
	public static void mergeArray(int[] array, int first, int mid, int last ){
		int[] temp = new int[array.length];
		int i = first, m = mid;
		int j = mid+1, n = last;
		int k = 0;
		while (i <= m && j <= n)  
	    {  
	        if (array[i] <= array[j])  
	            temp[k++] = array[i++];  
	        else  
	            temp[k++] = array[j++];  
	    }  
	      
	    while (i <= m)  
	        temp[k++] = array[i++];  
	      
	    while (j <= n)  
	        temp[k++] = array[j++];  
	      
	    for (i = 0; i < k; i++)  
	        array[first + i] = temp[i];  
	}
	//heap sort algorithm
	
	//需要调整的节点是 array[i]
	public static void adjustHeap(int[] array, int i){
		int largest = i;
		int length = array.length;
		int leftIndex = leftChild(i);
		int rightIndex = rightChild(i);
	
		while (leftIndex < length || rightIndex < length) {
			// find out which is larger between right and left child
			if (leftIndex < length && array[leftIndex] > array[largest]) {
				largest = leftIndex;
			} 
			if(rightIndex < length && array[rightIndex] > array[largest])
				largest = rightIndex;
			if(i!= largest){
				swap(array,i,largest);
				//下次迭代时新的父节点和字节点
				i = largest;
				leftIndex = leftChild(i);
				rightIndex = rightChild(i);
			}
			else 
				break;
		}	
	}
	public static void buildHeap(int[] array){
		int i ;
		int begin = array.length/2-1; //最后一个非叶子节点
		for(i=begin; i>=0;i--)
			adjustHeap(array,i);
	}
	public static void heapSort(int[] array){
		int length = array.length;
		buildHeap(array);
		while(length > 1){
			//交换堆的第一个与最后一个元素
			swap(array,0,length-1);
			length--;
			//调整堆,使其变成大顶堆
			adjustHeap(array,0);
		}
	}
	public static int leftChild(int i){
		return (i*2+1);
	}
	public static int rightChild(int i){
		return (i*2+2);
	}
	
	
	//print the result
	public static void print(int[] array){
		for(int i=0;i<array.length;i++)
			System.out.print(array[i]+"  ");
		return;
	}
	//swap two numbers
	public static void swap(int[] array, int i, int j){
		int temp = 0;
		temp = array[i];
		array[i] = array[j];
		array[j] = temp;
	}

}


你可能感兴趣的:(Java代码,排序算法)