各种排序和查找算法的实现与比较

排序


        int[] a = new int[]{1,4,6,4,3,2,8,1};  // 冒泡排序
        int temp;
        for (int i = 0; i < a.length; i++){
            for (int j = 0; j < a.length-1-i; j++){
                if (a[j] > a[j+1]){
                    temp = a[j];
                    a[j] = a[j+1];
                    a[j+1] = temp;
                }
            }
        }
 public static void main(String[] args) {   //快速排序
        int[] a = new int[]{1, 4, 6, 4, 3, 2, 8, 1};
        QuickSort(a,0,a.length-1);
        for (int z:a){
            System.out.println(z);
        }
    }
    public static void QuickSort(int[] a, int low, int high){
        if (low < high){   //进行递归
            int pos = Partition(a, low, high);
            QuickSort(a,low,pos-1);
            QuickSort(a,pos+1,high);
        }
    }
    public static int Partition (int[] a, int low, int high){
        int first = a[low];   //一趟排序过程
        while (low < high){
            while (low<high && a[high]>=first){
                --high;
            }
            a[low] = a[high];
            while (low<high && a[low]<=first){
                ++low;
            }
            a[high] = a[low];
        }
        a[low] = first;
        return low;
    }

 public static void main(String[] args) {   //堆排序
        int[] a = new int[]{1, 4, 6, 4, 3, 2, 8, 1};
        System.out.println("排序前:"+ Arrays.toString(a));
        sort(a);
        System.out.println("排序后:"+Arrays.toString(a));
    }
    public static void sort(int[] a){
        for (int i=a.length/2-1; i>=0; i--){ //构建大顶堆
            adjustHeap(a,i,a.length); //从第一个非叶子结点从下至上,从右至左调整结构
        }
        for (int j=a.length-1; j>0; j--){ //调整堆结构+交换堆顶元素与末尾元素
            swap(a,0,j); //将堆顶元素与末尾元素进行交换
            adjustHeap(a,0,j); //重新对堆进行调整
        }
    }
    // 调整大顶堆 (仅是调整过程,建立在大顶堆已构建的基础上)
    public static void adjustHeap(int[] a, int i, int length){
        int temp = a[i]; //取出当前元素
        for (int k=2*i+1; k<length; k=k*2+1){ //从i结点的左子结点开始,也就是2i+1处开始
            if (k+1<length && a[k]<a[k+1]){ //如果左子结点小于右子结点,k指向右子结点
                k++;
            }
            if (a[k] > temp){ //如果子结点大于父节点,将子结点赋值给父节点(不用进行交换)
                a[i] = a[k];
                i = k;
            } else {
                break;
            }
        }
        a[i] = temp;// 将temp放到最终位置
    }
    public static void swap(int[] a, int b, int c){
        int temp = a[b];
        a[b] = a[c];
        a[c] = temp;
    }
来源:https://blog.youkuaiyun.com/qq_36186690/article/details/82505569

直接插入排序算法:

把n个待排序的元素看成一个有序表和一个无序表,开始时有序表中只有一个元素,无序表中有n-1个元素;排序过程即每次从无序表中取出第一个元素,将它插入到有序表中,使之成为新的有序表,重复n-1次完成整个排序过程。
(来自:https://www.cnblogs.com/snowcan/p/6244128.html)

 public static void main(String[] args) {
        int[] a = new int[]{1, 4, 6, 4, 3, 2, 8, 1};
        insertSort(a);
    }
    public static void insertSort(int[] a){
        for (int i=1; i<a.length; i++){ //必须i=1,因为开始从第二个数与第一个数进行比较
            int temp = a[i]; //待比较数值
            //内层循环为待比较数值确定其最终位置
            int j = i - 1;
            for (; j>=0&&a[j]>temp; j--){ //待比较数值比前一位置小,应该插往前一位
                a[j+1] = a[j]; //将大于temp的值整体后移一个单位
            }
            a[j+1] = temp; //这里是j+1是因为上面j--了
        }
    }

直接选择排序:

从待排序的元素集合中选取关键字最小的数据元素并将它与原始数据元素集合中的第一个数据元素交换位置;然后从不包括第一个位置的数据元素集合中选取关键字最小的数据元素并将它与原始数据集合中的第二个数据元素交换位置;如此重复,直到数据元素集合中只剩下一个数据元素为止

public static void selectionSort(int[] a) {
		if (a==null&&a.length<1) {
			return;
			/**
			 * 空数组直接返回
			 **/
		}
		for (int i = 0; i < a.length-1; i++) { // i表示次数,共进行n-1次选择和交换
			int minIndex = i; // 用minIndex表示最小元素的下标
			for (int j = i+1; j < a.length; j++) {// 找到当前排序区间中最小元素的下标
				if (a[minIndex] > a[j]) {// 如果后面的元素小于当前最小元素,则用minIndex记录下后面最小元素的下标
					minIndex = j;//储存最小元素下标
				}
			}
			if (minIndex != i) {// 当最小元素的下标不为i时交换位置
				int temp = a[i];
				a[i] = a[minIndex];
				a[minIndex] = temp;
			}
		}
	}

归并排序

归并排序利用的是分治的思想实现的,对于给定的一组数据,利用递归与分治技术将数据序列划分成为越来越小的子序列,之后对子序列排序,最后再用递归方法将排好序的子序列合并成为有序序列。合并两个子序列时,需要申请两个子序列加起来长度的内存,临时存储新的生成序列,再将新生成的序列赋值到原数组相应的位置。
(来源:https://blog.youkuaiyun.com/xdzhouxin/article/details/80070839)

public class MergeSort {

    public static void merSort(int[] arr,int left,int right){

        if(left<right){
            int mid = (left+right)/2;
            merSort(arr,left,mid);//左边归并排序,使得左子序列有序
            merSort(arr,mid+1,right);//右边归并排序,使得右子序列有序
            merge(arr,left,mid,right);//合并两个子序列 合并两个有序数组
        }
    }
    private static void merge(int[] arr, int left, int mid, int right) {
        int[] temp = new int[right - left + 1];//ps:也可以从开始就申请一个与原数组大小相同的数组,因为重复new数组会频繁申请内存
        int i = left;
        int j = mid+1;
        int k = 0;
        while(i<=mid&&j<=right){
            if (arr[i] < arr[j]) {
                temp[k++] = arr[i++];
            } else {
                temp[k++] = arr[j++];
            }
        }
        while(i<=mid){//将左边剩余元素填充进temp中
            temp[k++] = arr[i++];
        }
        while(j<=right){//将右序列剩余元素填充进temp中
            temp[k++] = arr[j++];
        }
        //将temp中的元素全部拷贝到原数组中
        for (int k2 = 0; k2 < temp.length; k2++) {
            arr[k2 + left] = temp[k2];
        }
    }
    public static void main(String args[]){
        int[] test = {9,2,6,3,5,7,10,11,12};
        merSort(test,0,test.length-1);
        for(int i=0; i<test.length;i++){
            System.out.print(test[i] + " ");
        }
    }

}

查找

折半查找仅使用有序的顺序表

 public static int Binary_Search (int[] a, int key){
        int low = 0, high = a.length-1, mid;
        while (low <= high){
            mid = (low+high)/2;
            if (a[mid]==key){
                return mid;
            } else if (a[mid] > key){
                high = mid - 1;
            } else {
                low = mid + 1;
            }
        }
        return -1;
    }

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值