链表文件

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. echarts x轴或y轴文本字体颜色改变

    1:x轴文本字体颜色改变 xAxis : [ { type : 'category', data : ['<30','30-','40-','50-','60-','>=70'], axi ...

  2. Little Puzzlers–List All Anagrams in a Word

    The Solution A major hint is in the fact we are given a dictionary.  Because we are given this dicti ...

  3. selenium+python在Windows的环境搭建

    1 python下载安装 python早已安装,不再多说.因为开发使用的python2.7,所以同样使用2.7 2 打开Powershell, 输入python -m pip install sele ...

  4. HDU 5860 Death Sequence(递推)

    HDU 5860 Death Sequence(递推) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5860 Description You ...

  5. Mongo组合索引优化

    包含了等值测试.排序及范围过滤查询的索引建立方法: 1. 等值测试 在索引中加入所有需要做等值测试的字段,任意顺序. 2. 排序字段(多排序字段的升/降序问题 ) 根据查询的顺序有序的向索引中添加字段 ...

  6. webservice底层使用Socket进行网络调用

    服务端代码(其实tomcat的原理也是这样): 客户端代码:

  7. hdu 1384 Intervals

    差分约束系统. 求最小值,用最长路来解决. #include<cstdio> #include<cstring> #include<cmath> #include& ...

  8. FZU 1502 Letter Deletion(DP)

    Description You are given two words (each word consists of upper-case English letters). Try to delet ...

  9. Class.forName()

    主要功能 Class.forName(xxx.xx.xx)返回的是一个类 Class.forName(xxx.xx.xx)的作用是要求JVM查找并加载指定的类, 也就是说JVM会执行该类的静态代码段 ...

  10. hdu_5790_Prefix(trie+主席树)

    题目链接:hdu_5790_Prefix 题意: 给你n个字符串,字符串总长度不超过10W,然后给你一个区间,问你这个区间的字符串不相同的前缀有多少个. 题解: 由于z与上一个答案有关,所以强制在线, ...