链表文件

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. EFI安装Win7

    安装系统之前电脑里最好没有其他系统,安装过程中电脑需重启多次,其他系统会引导电脑开机,无法完成WIN7安装. 一.制作安装分区 1.首先在移动硬盘(U盘)准备一个FAT32分区 一定要FAT32分区, ...

  2. 逆向思维 UVA 11853

    题目大意:紫书175 思路:看书...2333 关键点就是利用已知条件来逆向思考是否能走通,而不是傻傻的从某个点开始出发啊啥的.

  3. Webdriver其他定位方式

    1.下拉框的定位 在遇到select下拉框的选择时,比如: <select id="nr" name="NR"> <option select ...

  4. QDataStream对QVector的序列化

    最近发现QDataStream这个好东东,序列化发送数据很方便,与大家分享一下. 客户端: line.h #ifndef LINE_H #define LINE_H #include <QStr ...

  5. python 学习 有序字典

    自定义创建有序字典类 dict的__getitem__方法 有些不同,想使用自定义__getitem__方法显示查询key的下标:需要研究 #/usr/bin/env python3 # -*- co ...

  6. ie6的png24问题

    解决IE6的PNG透明JS插件 DD_belatedPNG 引:http://www.cnblogs.com/cobby/archive/2012/05/11/2495801.html IE6的PNG ...

  7. Python 第一课笔记

    1.Hello World程序的两种方法     在windows下执行 1.编辑器里输入,不用编译 print("Hello World!") 直接就可以运行      2.可以 ...

  8. html5权威指南:定制input元素

    第十三章:定制Inpur元素,http://www.cnblogs.com/polk6/p/5417921.html#Menu3-New input标签最全面的type属性:http://blog.s ...

  9. html 超链接(a)详细讲解

    a:link : http://www.cnblogs.com/yangfeng/archive/2009/07/25/1530962.html 超级链接 超级链接是网站中使用比较频繁的HTML元素, ...

  10. JS+CSS简单实现DIV遮罩层显示隐藏

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...