博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
快速排序一(划分)
阅读量:4315 次
发布时间:2019-06-06

本文共 1374 字,大约阅读时间需要 4 分钟。

目标:在一组数a0,a1,a2,a3,a4,a5,a6,a7,a8,a9中尽量取其中间值,以此为pivot,值大于pivot的元素置于pivot左边,反之至于右边。

源代码:

package com.paixu;

public class ArrayPar {

private int nElems;
private long[] arr;
public ArrayPar(int max){
arr = new long[max];
nElems =0;
}
public void insert(long value){
arr[nElems++] = value;
}
public void display(){
for(int i=0;i<nElems;i++)
System.out.print("the" + i + "Elem is " + arr[i] + " " );
}
public int size(){
return nElems;
}
public int partitionIt(int left,int right,int pivot){
int leftPtr = left -1;
int rightPtr = right + 1;
while(true){
while(leftPtr < right && arr[++leftPtr] < pivot)
;
while(rightPtr > left && arr[--rightPtr] > pivot)
;
if(leftPtr >= rightPtr)
break;
else
swap(leftPtr,rightPtr);
}
return leftPtr;
}
public void swap(int leftValue,int rightValue){
long temp;
temp =arr[rightValue];
arr[rightValue] = arr[leftValue];
arr[leftValue] = temp;
}
}

 

 

package com.paixu;

public class PartitionApp {

public static void main(String[] args){

int maxSize = 20;
ArrayPar arr = new ArrayPar(maxSize);
for(int i=0;i<maxSize;i++){
long n = (long)(Math.random() * 99);
arr.insert(n);
}
arr.display();
System.out.println();
int pivor = 50;
int size = arr.size();
int partDex = arr.partitionIt(0, size-1, pivor);
System.out.println("Partition is at index " + partDex);
arr.display();
}

}

转载于:https://www.cnblogs.com/xunmengyoufeng/archive/2012/10/06/2712766.html

你可能感兴趣的文章
使用case语句给字体改变颜色
查看>>
JAVA基础-多线程
查看>>
面试题5:字符串替换空格
查看>>
JSP九大内置对象及四个作用域
查看>>
ConnectionString 属性尚未初始化
查看>>
数据结构-栈 C和C++的实现
查看>>
MySQL基本命令和常用数据库对象
查看>>
poj 1222 EXTENDED LIGHTS OUT(位运算+枚举)
查看>>
进程和线程概念及原理
查看>>
Lucene、ES好文章
查看>>
android 生命周期
查看>>
jquery--this
查看>>
MySQL 5.1参考手册
查看>>
TensorFlow安装流程(GPU加速)
查看>>
OpenStack的容器服务体验
查看>>
【BZOJ 4059】 (分治暴力|扫描线+线段树)
查看>>
BZOJ 1066 蜥蜴(网络流)
查看>>
提高批量插入数据的方法
查看>>
Linux重启Mysql命令
查看>>
前端模块化:RequireJS(转)
查看>>