链表文件

package sort;

public class SqList {
    public int LIST_INIT_SIZE = 8;//链表的原始大小
    private int INCREMENT = 1;//链表的增量大小
    private Object[] SqList = null;//链表
    private int curIndex = 0;//当前位置
     /**
     * 初始化链表
     * */
    public void initList(){
        SqList = new Object[LIST_INIT_SIZE];
    }
    
    /**
     * 向链表中插入元素
     * */
    public void insertList(Object o){
        if(curIndex > LIST_INIT_SIZE-1){//判断当前链表是否已经满
            System.out.println("从新分配空间");
            LIST_INIT_SIZE += INCREMENT;
            Object []temp = new Object[LIST_INIT_SIZE];
            for(int i=0; i<curIndex; i++)
            {
                temp[i]=SqList[i];
            }
            SqList = null;
            SqList = temp;
        }
        //链表中如果不让其包含重复元素,则加入这段代码
        /*
        if(isContain(o))
        {
            System.out.println("链表中已包含此元素"+o);
        }else
        {
             
        }
        */
        SqList[curIndex++] = o;
    }
    
    /**
     * 判断链表中是否包含某元素
     * */
    private Boolean isContain(Object o){
        for(int i = 0; i<curIndex; i++){
            if(SqList[i].equals(o))
                return true;
        }
        return false;
    }
    
    /**
     * 删除链表中的某元素
     *
     * 如果包含重复元素都删除
     * */
    public void delete(Object o){
        for(int i = 0; i<curIndex; i++){
            if(SqList[i].equals(o))
            {
                for(int j=i;j<curIndex-1;j++)
                {
                    SqList[j]=SqList[j+1];
                }
                curIndex--;
                continue;
            }
            if(i==curIndex-1)
            {
                System.out.println("不存在此元素"+o);
            }
        }
    }
    
    /**
     * 获取链表中的某个元素
     * */
    public Object getElement(int i)
    {
        if (i < 0 || i > curIndex)
        {
            System.out.println("获取位置超出了链表中元素个数"+curIndex);
        }
        return SqList[i];
    }
    
    /**
     * 打印链表
     * */
    public void print()
    {
        for(int i=0;i<curIndex;i++)
        {
            System.out.print(SqList[i]+"\t");
        }
        System.out.println();
    }
    
    /**
     * 交换链表中的两个元素
     * */
    public void swap(SqList L,int low,int high){
        System.out.println("before swap:low-"+SqList[low]+"  high-"+SqList[high]);
        Object temp = null;
        temp =  SqList[low];
        SqList[low] = SqList[high];
        SqList[high] = temp;
        System.out.println("after swap:low-"+SqList[low]+"  high-"+SqList[high]);
    }
}

快排

package sort;
/*快排:两个指针low和high,枢轴记录的关键字为pivotkey,
 * 首先从high所指位置起向前搜索,找到第一个关键字小于pivotkey的记录和枢轴记录交换,
 * 然后从low所指位置向后搜索,找到第一个关键字大于pivotkey的记录和枢轴记录交换,
 * 重复这两步直到low=high为止*/
public class quickSort {
    public void QuickSort(SqList L){
        QSort(L,0,L.LIST_INIT_SIZE-1);
    }
    
    public void QSort(SqList L,int low,int high){
        int pivot;
        if(low<high){
            pivot = Partition(L,low,high);
            QSort(L,low,pivot-1);
            QSort(L,pivot+1,high);
        }
    }
    
    public int Partition(SqList L,int low,int high){
        System.out.println("start low:"+low+",high:"+high);
        int pivotkey;
        pivotkey = (Integer) L.getElement(low);//用子表的第一个记录作枢纽记录
        System.out.println("pivotkey:"+pivotkey);
        while(low<high){//从表的两端交替向中间扫描
            while(low<high && (Integer)L.getElement(high)>=pivotkey)
                high--;
            System.out.println("1-low:"+low+",high:"+high);
            L.swap(L,low,high);//将比枢轴记录小的记录交换到低端
            while(low<high && (Integer)L.getElement(low)<=pivotkey )
                low++;
            System.out.println("2-low:"+low+",high:"+high);
            L.swap(L,low,high);//将比枢轴记录大的记录交换到高端
        }
        System.out.println("low:"+low+",high:"+high);
        return low;//返回枢轴所在位置        
    }
    public static void main(String[] args) {
        SqList sqList = new SqList();
        sqList.initList();
        sqList.insertList(49);
        sqList.insertList(38);
        sqList.insertList(65);
        sqList.insertList(97);
        sqList.insertList(76);
        sqList.insertList(13);
        sqList.insertList(27);
        sqList.insertList(49);
        sqList.print();
        quickSort quickSort = new quickSort();
        quickSort.QuickSort(sqList);
        sqList.print();
        System.out.println("第2个元素是:"+sqList.getElement(1));
        System.out.println("第4个元素是:"+sqList.getElement(3));
    }
}

java链表实现快排的更多相关文章

  1. 63.如何对单链表进行快排?和数组快排的分析与对比[quicksort of array and linked list]

    [本文链接] http://www.cnblogs.com/hellogiser/p/quick-sort-of-array-and-linked-list.html [题目] 单链表的特点是:单向. ...

  2. Java 排序(快排,归并)

    Java 排序有Java.util.Arrays的sort方法,具体查看JDK API(一般都是用快排实现的,有的是用归并) package yxy; import java.util.Arrays; ...

  3. 记录一个基于Java的利用快排切分来实现快排TopK问题的代码模板

    使用快排切分实现快排和TopK问题的解题模板 import java.util.Arrays; public class TestDemo { public static void main(Stri ...

  4. UVA 1152 4 Values whose Sum is 0 (枚举+中途相遇法)(+Java版)(Java手撕快排+二分)

    4 Values whose Sum is 0 题目链接:https://cn.vjudge.net/problem/UVA-1152 ——每天在线,欢迎留言谈论. 题目大意: 给定4个n(1< ...

  5. Java常见的几种排序算法-插入、选择、冒泡、快排、堆排等

    本文就是介绍一些常见的排序算法.排序是一个非常常见的应用场景,很多时候,我们需要根据自己需要排序的数据类型,来自定义排序算法,但是,在这里,我们只介绍这些基础排序算法,包括:插入排序.选择排序.冒泡排 ...

  6. C语言实现单向链表及其各种排序(含快排,选择,插入,冒泡)

    #include<stdio.h> #include<malloc.h> #define LEN sizeof(struct Student) struct Student / ...

  7. 折半、快排、插入排序的Java实现

    插入排序 import java.util.Arrays; public class InsertionSort { /** * 对数组里面进行插入排序 * 参数1 数组 * 参数2 数组大小 */ ...

  8. 快排+java实现

    import java.util.Arrays; public class QuickSort { //三数取中法.取出不大不小的那个位置 public static int getPivotPos( ...

  9. 待字闺中之快排单向链表;leetcode之Sort List

    题目来源.待字闺中.原创@陈利人 .欢迎大家继续关注微信公众账号"待字闺中" 分析:思路和数据的高速排序一样,都须要找到一个pivot元素.或者节点. 然后将数组或者单向链表划分为 ...

随机推荐

  1. Kubernetes 认证

    openssl genrsa -out ca.key 2048openssl req -x509 -new -nodes -key ca.key -subj "/CN=cluster.loc ...

  2. 取消a标签在移动端点击时的背景颜色

    一.取消a标签在移动端点击时的蓝色 -webkit-tap-highlight-color: rgba(255, 255, 255, 0); -webkit-user-select: none; -m ...

  3. PyCharm 安装指南

    一.官网下载最新版的PyCharm,根据平台选择版本(Liunx,Windows) 地址:https://www.jetbrains.com/pycharm/download/#section=win ...

  4. 非root用户搭建hadoop伪分布式

    0.安装软件列表 jdk-7u25-linux-x64.tar.gz hadoop-2.5.0.tar.gz hadoop-native-64-2.5.0.tar   1.准备Linux环境(root ...

  5. python学习之glob模块

    如何批量获取文件路径 import glob import os def image_proc(): for files in glob.glob('/home/xxx/filename/*.png' ...

  6. 移动端ios电话号码

    <meta name="format-detection" content="telephone=no"> <meta http-equiv= ...

  7. python 基础学习2--编程

    python编程的步骤为: __name__ 指示模块如何被加载:如果模块被导入,__name__的值是模块的名称,如果模块被直接执行,__name__的值是main 变量不用进行声明,直接赋值:无需 ...

  8. CachedRowSet的用法

    String sql="select item_code from xt_dictionary_item where type_id='32' and parent_itemid='0' o ...

  9. CodeForces 703C Chris and Road

    数学,递推. 不知道有没有更加神奇的做法,我是这样想的: 首先,如果多边形完全在$y$轴左侧,那么答案为$\frac{w}{u}$. 剩下的情况就要先判断是否能在车开过之前跑过去,如果跑不过去,要在车 ...

  10. 第八十四节,css布局小技巧及font-awesome图标使用

    css布局小技巧及font-awesome图标使用 图片鼠标放上去遮罩效果,显示文字 当鼠标放上去时 /*最外层div*/ .a{ width: 384px; height: 240px; backg ...