java链表实现快排
链表文件
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链表实现快排的更多相关文章
- 63.如何对单链表进行快排?和数组快排的分析与对比[quicksort of array and linked list]
[本文链接] http://www.cnblogs.com/hellogiser/p/quick-sort-of-array-and-linked-list.html [题目] 单链表的特点是:单向. ...
- Java 排序(快排,归并)
Java 排序有Java.util.Arrays的sort方法,具体查看JDK API(一般都是用快排实现的,有的是用归并) package yxy; import java.util.Arrays; ...
- 记录一个基于Java的利用快排切分来实现快排TopK问题的代码模板
使用快排切分实现快排和TopK问题的解题模板 import java.util.Arrays; public class TestDemo { public static void main(Stri ...
- 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< ...
- Java常见的几种排序算法-插入、选择、冒泡、快排、堆排等
本文就是介绍一些常见的排序算法.排序是一个非常常见的应用场景,很多时候,我们需要根据自己需要排序的数据类型,来自定义排序算法,但是,在这里,我们只介绍这些基础排序算法,包括:插入排序.选择排序.冒泡排 ...
- C语言实现单向链表及其各种排序(含快排,选择,插入,冒泡)
#include<stdio.h> #include<malloc.h> #define LEN sizeof(struct Student) struct Student / ...
- 折半、快排、插入排序的Java实现
插入排序 import java.util.Arrays; public class InsertionSort { /** * 对数组里面进行插入排序 * 参数1 数组 * 参数2 数组大小 */ ...
- 快排+java实现
import java.util.Arrays; public class QuickSort { //三数取中法.取出不大不小的那个位置 public static int getPivotPos( ...
- 待字闺中之快排单向链表;leetcode之Sort List
题目来源.待字闺中.原创@陈利人 .欢迎大家继续关注微信公众账号"待字闺中" 分析:思路和数据的高速排序一样,都须要找到一个pivot元素.或者节点. 然后将数组或者单向链表划分为 ...
随机推荐
- wcf测试工具
WCF测试工具-WcfStorm WCF测试工具-WcfStorm http://www.wcfstorm.com/wcf/home.aspx WcfStorm is a dead-simple, ...
- 【Python之路】第三篇--Python基本数据类型
运算符 1.算数运算: # 在py2的 取整除运算中 9//2 = 4.0 # 引入 from __future__ import division 9//2 = 4.5 # py3中不需要! 2.比 ...
- css 样式那些事
1. input placeholder 的颜色修改 ::-moz-placeholder { color: #f3d999; } ::-webkit-input-placeholder { ...
- mongo数据库备份与恢复
备份:mongodump -h xx --port 27017 -u user -p pass -d database -o D:\backup\database 恢复:mongorestore -h ...
- 小团队git开发模式
实验室要使用Git进行代码管理,但是git非常复杂,各种开发模式也是层出不穷.作为新手的偶们很是发囧啊!!网上搜了一下,发现很多并不适合我们小团队运作(它本身就是为Linux内核管理而开发的分布式代码 ...
- .Net cxy 提高效率
Visual Studio Visual Studio Productivity Power tool: VS 专业版的效率工具. Web Essentials: 提高开发效率,能够有效的帮助开发人员 ...
- CocoaPods 报错 [!] Error installing JSONModel
pod install p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #34bd26 } span.s1 { } ...
- HDU 1008 u Calculate e
Problem Description A simple mathematical formula for e is where n is allowed to go to infinity. Thi ...
- POJ 2485 Highway(Prim+邻接矩阵)
( ̄▽ ̄)" //求最短总路径中的最大边长,Prim还需要一个Max变量 #include<iostream> #include<cstdio> #include&l ...
- csu oj Infected Computer 1427
#include <iostream> #include <algorithm> #include <stdio.h> #define max 20005 #def ...