链表翻转 快速排序

5、  链表翻转
<1>迭代方式
Prev = head;
Curr = head;
If(head==null||head.getNext()==null){
        return head
}
Node tem;//用来存储curr.getNext()指针
while(curr.getNext()!= null){
        tmp = curr.getNext();
curr.getNext().setNext(prev);
prev = curr;
curr = tmp;

}
return curr;//返回新的头结点
2、递归方式
递归是从最后一个节点翻转。
Node reverse (Node head){
        If(head==null||head.getNext()==null){
    return head;
}
Node reHead  = reverse(head.getNext());
head.getNext().setNext(head);
return reHead;
}
/**
 * @Author GJL
 * @Desription
 * @Date 2017/11/22
 * @Modified By:
 **/
public class QuickSort{
    public static void quickSort(int[] a,int low,int high){
        int pos;
        if(low<high){
            pos = partition(a,low,high);
            quickSort(a,low,pos-1);
            quickSort(a,pos+1,high);
        }

    }

    private static int  partition(int[] a, int low, int high) {

        int key = a[high];
        int i = low;
        int index= i-1;//标识key元素的位置
        while (i<high){

            if(a[i]<key){
                index = index+1;
                int tem = a[index];
                a[index] = a[i];
                a[i] = tem;
            }
            i++;
        }
        int m = a[index+1];
        a[index+1] = a[high];
        a[high] = m;



        return index+1;
    }
    public static void showAllInts(int[] a){
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i]);
        }
    }

    public static void main(String[] args) {
        int a [] ={2,1,8,4,6,89,978,0,345};
        QuickSort.quickSort(a,0,8);
        QuickSort.showAllInts(a);
    }

}
//partition函数主要是把数据分成俩部分 还可以这样编写
    private static int partition(int[] a, int low, int high) {
        int key = a[low];

        while(low<high){
            while(low<high && a[high]>=key){
                high--;
            }
            if(low<high){
                a[low++] = a[high];
            }
            while(low<high && a[low]<key){
                low++;
            }
            if(low<high){
                a[high--]= a[low];
            }
        }
        a[low]=key;
    return low;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值