算法例子(快速排序和希尔排序)

本文主要介绍一下必须要记住的查找法和排序法。

1.二分查找法和快速排序法例子的思想是类似的

2.希尔排序法和插入排序法的思想是类似的


package javatest.suanfa;


public class Shuzupaixu {


/**
* 插入数组排序
* @param arr
*/
public static void insertionSort(int[] arr){
int in,out;
for(out=1;out<arr.length;out++){
int temp = arr[out];
in = out;
while(in>0&&arr[in-1]>temp){
arr[in] = arr[in-1];
in--;
}
arr[in] = temp;
}
}

/**
* 希尔排序算法
* @param arr
*/
private static void shellSort(int[] arr) {
        int inner, outer;
        int temp;
        int h = 1;
        int nElem = arr.length;
        while (h <= nElem / 3) {
            h = h * 3 + 1;
        }
        while (h > 0) {
            for (outer = h; outer < nElem; outer++) {
                temp = arr[outer];
                inner = outer;
                while (inner > h - 1 && arr[inner - h] >= temp) {
                    arr[inner] = arr[inner - h];
                    inner -= h;
                }
                arr[inner] = temp;
            }
            h = (h - 1) / 3;
        }
    }

/**
* 快速排序
* @param arr
* @param left
* @param right
*/
    private static void recQuickSort(int arr [] ,int left,int right){
        if(right - left <= 0){
            return;
        }else{
            int pivot = arr[right];//一般使用数组最右边的元素作为枢纽
            int partition = partitionIt(arr, left, right, pivot);
            recQuickSort(arr, left, partition-1);
            recQuickSort(arr, partition+1, right);
        }
    }




    //划分
    private static int partitionIt(int[] arr ,int left,int right,int pivot){
        int leftPtr = left - 1;
        //int rightPtr = right + 1;
        int rightPtr = right ; //使用最右边的元素作为枢纽,划分时就要将最右端的数据项排除在外
        while(true){
            while(arr[++leftPtr] < pivot);
            while(rightPtr > 0 && arr[--rightPtr] > pivot);


            if(leftPtr >= rightPtr){
                break;
            }else{
                //交换leftPtr和rightPtr位置的元素
                int temp = arr[leftPtr];
                arr[leftPtr] = arr[rightPtr];
                arr[rightPtr] = temp;
            }
        }
        //交换leftPtr和right位置的元素
        int temp = arr[leftPtr];
        arr[leftPtr] = arr[right];
        arr[right] = temp;
        return leftPtr;//返回枢纽位置
    }


/**
* 二分查找法
* @param arr
* @param start
* @param end
* @param searchKey
* @return
*/
public static int resursiveFind(int[] arr,int start,int end,int searchKey){
if(start<=end){
int middle = (start+end) >> 1;
if(searchKey == arr[middle]){
return middle;
}else if(searchKey< arr[middle]){
return resursiveFind(arr,start,middle-1,searchKey);

}else{
return resursiveFind(arr,middle+1,end,searchKey);
}

}else {
return -1;
}
}

public static void main(String[] args) {
// TODO Auto-generated method stub

int[] test1 = {7,6,3,9,8};

insertionSort(test1);

for(int out = 0;out<test1.length;out++){
System.out.println(test1[out]);

}

int[] test2 = {1,3,7,9};

System.out.print(resursiveFind(test2,0,3,9));


//希尔排序测试
int[] test3 = {5,4,11,9,66,54,4,6,45,65,34};

shellSort(test3);

for(int out = 0;out<test3.length;out++){
System.out.println(test3[out]);
}

//希尔排序测试
int[] test4 = {5,4,11,9,66,54,4,6,45,65,34};

recQuickSort(test4,0,test4.length-1);

}


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值