码迷,mamicode.com
首页 > 其他好文 > 详细

所有的排序、查找算法

时间:2014-08-01 00:07:30      阅读:295      评论:0      收藏:0      [点我收藏+]

标签:java   排序算法   

import javax.mail.Part;

/**
 *   顺序查找、排序
 *
 * @author 曾修建
 * @version 创建时间:2014-7-30 下午04:15:10
 */
public class SequentialSearch {


	public static void main(String[] args) {
		Integer[] a = {1,2,3,4,5,7,6,88};


		//二分查找非递归
		System.out.println( "二分查找非递归实现   位置  :  "+ Binary_Seach(a,0));
		//二分查找递归实现
		System.out.println("二分查找递归实现     :" +Binary(a,0,a.length,1));
		//插值查找
		System.out.println(" 插值查找位置   :  "+Insert_Search(a,3));

		//斐波拉契查找

		//稠密索引  (家里放的东西记录到笔记本上)/
		//分块索引(图书馆的书)
		//倒排索引
		Integer[] a_sort = {1,33,3,14,5,71,6,88};
		//希尔排序
		Shell_Sort(a_sort);
		//冒泡排序
		Bubble_Sort(a_sort);
		//插入排序
		Insert_Sort(a_sort);
		//简单选择排序
		Select_Sort(a_sort);
		//快速排序
		Quick_Sort(a_sort);
		//堆排序
        Heap_Sort(a_sort);
		//归并排序
		Merge_Sort(a_sort);

		for (int i = 0; i < a.length; i++) {
			if (a[i]==8) {
				System.out.println("找到了");
			}
		}
		int i=0;
		if (a[0]==1) {
			System.out.println("找到了");
			//return 0;
		}
		a[0]=1;
		while (a[i]!=1) {
			i++;
		}
		//return i;
	}
	//堆排序
	 public static void Heap_Sort(Integer[] array) {  
        // 对数组进行筛选,建成一个大顶堆  
        double len = array.length - 1;  
        for (int i = (int) Math.floor(len/2); i > 0; i--) {  
            heapAdjust(array, i, array.length - 1);  
        }  
        for (int i = array.length - 1; i > 0; i--) {  
            // 将堆顶元素与最后一个元素调换位置,即输出最大值  
           // swap(array, 1, i);  
        	int temp;  
            temp = array[1];  
            array[1] = array[i];  
            array[i] = temp;
            // 将最后一位剔出,数组最大下标变为i-1。自队顶至叶子进行调整,形成一个新堆,此过程称为筛选  
            heapAdjust(array, 1, i - 1);  
        }  
        System.err.println();  
        System.err.println("堆排序 :");  
        for (int i = 1; i < array.length; i++) {  
            System.err.print(array[i] + "   ");  
        } 
    }  
  
    // 建堆函数,认为【s,m】中只有 s  
    // 对应的关键字未满足大顶堆定义,通过调整使【s,m】成为大顶堆=====================================================  
    public static void heapAdjust(Integer[] array, int s, int m) {  
        // 用0下标元素作为暂存单元  
        array[0] = array[s];  
        // 沿孩子较大的结点向下筛选  
        for (int j = 2 * s; j <= m; j *= 2) {  
            // 保证j为较大孩子结点的下标,j < m 保证 j+1 <= m ,不越界  
            if (j < m && array[j] < array[j + 1]) {  
                j++;  
            }  
            if (!(array[0] < array[j])) {  
                break;  
            }  
            // 若S位较小,应将较大孩子上移  
            array[s] = array[j];  
            // 较大孩子的值变成S位的较小值,可能引起顶堆的不平衡,故对其所在的堆进行筛选  
            s = j;  
        }  
        // 若S位较大,则值不变;否则,S位向下移动至2*s、4*s、。。。  
        array[s] = array[0];  
    }  
  
    // 交换函数=====================================================  
    public static void swap(Integer[] array, int i, int j) {  
        int temp;  
        temp = array[i];  
        array[i] = array[j];  
        array[j] = temp;  
    }  
	
	//归并排序
	public static void Merge_Sort(Integer []a) {
		if (a.length>0) {
			M_Sort(a,a,0,a.length-1);
		}
		System.err.println();
		System.err.println("归并排序 :  ");
		for (int i = 0; i < a.length; i++) {
			System.err.print(a[i]+"   ");
		}
	}
	//归并排序实现
	public static void M_Sort(Integer []a,Integer []td, int low , int high) {//拆分
		int mid ;
		Integer [] td2 = new Integer[a.length];
		if (low == high) {
			td[low] = a[high]; 
		}
		
		else {
			mid =(low+high)/2;
			M_Sort(a, td2, low, mid);
			M_Sort(a, td2, mid+1, high);
			Merge(a, td, low, mid, high);
		}
	}
	//归并排序合并
	public static void  Merge(Integer []a , Integer []td ,int low , int mid, int high) {
		int m , n , k;
		for ( m = low ,n = mid ,k=low ; m < mid && n < high ; k++) {
			td[k] = a[m] < a[n] ? a[m++]:a[n++];
		}
		if (m < mid) {
			for (int i = 0; i < mid-m; i++) {
				td[k+i] = a[m+i];
			}
		}
		if (n<high) {
			for (int i = 0; i < high-n; i++) {
				td[k+i] = a[n+i];
			}
		}
		System.err.println();
		System.err.println("归并排序 :  ");
		for (int i = 0; i < td.length; i++) {
			System.err.print(td[i]+"   ");
		}
	}
	
	//快速排序
	public  static void Quick_Sort(Integer a[]) {
		if (a.length>0) {
			Sort(a,0,a.length-1);
		}
		System.err.println();
		System.err.println("快速排序 :  ");
		for (int i = 0; i < a.length; i++) {
			System.err.print(a[i]+"   ");
		}
	}
	//快排实现
	public static void Sort(Integer []a, int low, int high) {

		if (low<high) {
			int position = Part(a , low , high);
			Sort(a, low, position-1);
			Sort(a, position+1, high);
		}
		

	}
	//快排方法
	public static int Part(Integer []a,int low , int high){
		int i = low , j = high ,po = a[low]; 
		while (i<j) {
			while (i<j && po < a[j]) {
				j--;
			}

			a[i] = a[j];//比中轴小的记录移到低端  

			while (i<j && po >a[i]) {
				i++;
			}
			a[j] = a[i];//比中轴大的记录移到高端  


		}
	//	a[low] =po;   //中轴记录到尾  
		return i;
	}
	//简单选择排序
	public static void Select_Sort(Integer []a) {
		for (int i = 0; i < a.length; i++) {
			int min =i;
			for (int j = i+1; j < a.length; j++) {
				if (a[min] > a[j]) {
					min = j;
				}
			}
			if (min!=i) {
				int temp = a[min];
				a[min] = a[i];
				a[i] = temp;
			}
		}
		System.err.println();
		System.err.println("简单选择 :  ");
		for (int i = 0; i < a.length; i++) {
			System.err.print(a[i]+"   ");
		}

	}
	//插入排序
	public static void  Insert_Sort(Integer []a) {
		for (int i = 1; i < a.length; i++) {
			if (a[i]<a[i-1]) {
				for (int j = i; j >=0; j--) {
					if (a[i]<a[j]) {
						int temp = a[i];
						a[i] = a[j];
						a[j] = temp;
					}
				}
			}
		}
		System.err.println();
		System.err.println("插入 :  ");
		for (int i = 0; i < a.length; i++) {
			System.err.print(a[i]+"   ");
		}
	}

	//希尔排序
	public static void  Shell_Sort(Integer []a) {
		int step =a.length;
		do {
			step = step/3+1;
			for (int i = step+1; i < a.length; i++) {
				if (a[i]<a[i-step]) {
					int temp = a[i];
					a[i] = a[i-step];
					a[i-step] =temp;
				}
			}
		} while (step>1);
		System.err.println();
		System.err.println("希尔  :  ");
		for (int i = 0; i < a.length; i++) {
			System.err.print(a[i]+"   ");
		}

	}

	//冒泡排序
	public static void Bubble_Sort(Integer []a ) {
		for (int i = 1; i < a.length; i++) {
			for (int j = a.length-1; j >=i; j--) {
				if (a[j]<a[j-1]) {
					int temp = a[j];
					a[j] = a[j-1];
					a[j-1] = temp;
				}
			}
		}
		System.err.println();
		System.err.println("冒泡  :  ");
		for (int i = 0; i < a.length; i++) {
			System.err.print(a[i]+"   ");
		}
	}

	//插值查找
	public static int  Insert_Search(Integer []a , int k) {
		int low = 0 , high = a.length-1 , mid=-1;
		while (low<=high) {
			mid = low+((k-a[low])/(a[high]-a[low]))*(high-low);
			if (a[mid] == k) {
				return mid;
			}
			else if (a[mid] > k) {
				high = mid-1;
			}
			else {
				low = mid+1;
			}
		}
		return -1;
	}

	//递归实现二分查找
	public static int  Binary(Integer []a , int low , int high ,int k) {
		int mid = (low+high)/2;
		if (a[mid] == k) {
			return mid;
		}
		else if (a[mid] > k) {
			return Binary(a, low , mid-1, k);
		}
		else {
			return Binary(a, mid + 1, high, k);
		}
	}

	//非递归实现二分查找
	public static int Binary_Seach(Integer[] a , int k){
		int low = 0 , high = a.length , mid=-1;
		while (low<=high) {
			mid = (low+high)/2;
			if (a[mid]==k) {
				return mid;
			}
			else if (a[mid]>k) {
				high = mid-1;
			}
			else {
				low = mid+1;
			}
		}
		return -1;
	}
}


所有的排序、查找算法,布布扣,bubuko.com

所有的排序、查找算法

标签:java   排序算法   

原文地址:http://blog.csdn.net/wintersweetzeng/article/details/38323655

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!