符合中小企业对网站设计、功能常规化式的企业展示型网站建设
本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...
商城网站建设因基本功能的需求不同费用上面也有很大的差别...
手机微信网站开发、微信官网、微信商城网站...
本人特地给你编的代码
创新互联建站是一家专注于网站设计、成都做网站与策划设计,红山网站建设哪家好?创新互联建站做网站,专注于网站建设10年,网设计领域的专业建站公司;建站业务涵盖:红山等地区。红山做网站价格咨询:028-86922220
亲测
public class QuickSort {
public static int Partition(int a[],int p,int r){
int x=a[r-1];
int i=p-1;
int temp;
for(int j=p;j=r-1;j++){
if(a[j-1]=x){
// swap(a[j-1],a[i-1]);
i++;
temp=a[j-1];
a[j-1]=a[i-1];
a[i-1]=temp;
}
}
//swap(a[r-1,a[i+1-1]);
temp=a[r-1];
a[r-1]=a[i+1-1];
a[i+1-1]=temp;
return i+1;
}
public static void QuickSort(int a[],int p,int r){
if(pr){
int q=Partition(a,p,r);
QuickSort(a,p,q-1);
QuickSort(a,q+1,r);
}
}
public static void main(String[] stra){
int a[]={23,53,77,36,84,76,93,13,45,23};
QuickSort(a,1,10);
for (int i=1;i=10;i++)
System.out.println(a[i-1]);
}
}
快速排序:
package org.rut.util.algorithm.support;
import org.rut.util.algorithm.SortUtil;
/**
* @author treeroot
* @since 2006-2-2
* @version 1.0
*/
public class QuickSort implements SortUtil.Sort{
/* (non-Javadoc)
* @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
*/
public void sort(int[] data) {
quickSort(data,0,data.length-1);
}
private void quickSort(int[] data,int i,int j){
int pivotIndex=(i+j)/2;
//swap
SortUtil.swap(data,pivotIndex,j);
int k=partition(data,i-1,j,data[j]);
SortUtil.swap(data,k,j);
if((k-i)1) quickSort(data,i,k-1);
if((j-k)1) quickSort(data,k+1,j);
}
/**
* @param data
* @param i
* @param j
* @return
*/
private int partition(int[] data, int l, int r,int pivot) {
do{
while(data[++l]pivot);
while((r!=0)data[--r]pivot);
SortUtil.swap(data,l,r);
}
while(lr);
SortUtil.swap(data,l,r);
return l;
}
}
改进后的快速排序:
package org.rut.util.algorithm.support;
import org.rut.util.algorithm.SortUtil;
/**
* @author treeroot
* @since 2006-2-2
* @version 1.0
*/
public class ImprovedQuickSort implements SortUtil.Sort {
private static int MAX_STACK_SIZE=4096;
private static int THRESHOLD=10;
/* (non-Javadoc)
* @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
*/
public void sort(int[] data) {
int[] stack=new int[MAX_STACK_SIZE];
int top=-1;
int pivot;
int pivotIndex,l,r;
stack[++top]=0;
stack[++top]=data.length-1;
while(top0){
int j=stack[top--];
int i=stack[top--];
pivotIndex=(i+j)/2;
pivot=data[pivotIndex];
SortUtil.swap(data,pivotIndex,j);
//partition
l=i-1;
r=j;
do{
while(data[++l]pivot);
while((r!=0)(data[--r]pivot));
SortUtil.swap(data,l,r);
}
while(lr);
SortUtil.swap(data,l,r);
SortUtil.swap(data,l,j);
if((l-i)THRESHOLD){
stack[++top]=i;
stack[++top]=l-1;
}
if((j-l)THRESHOLD){
stack[++top]=l+1;
stack[++top]=j;
}
}
//new InsertSort().sort(data);
insertSort(data);
}
/**
* @param data
*/
private void insertSort(int[] data) {
int temp;
for(int i=1;idata.length;i++){
for(int j=i;(j0)(data[j]data[j-1]);j--){
SortUtil.swap(data,j,j-1);
}
}
}
}
public class quickSort {
public quickSort() {
}
public void printA(int[] a) {
for (int i = 0; i a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
}
public void chooseSort(int[] a, int left, int right) {
int smallest;
int flagIndex = 0;
int forSwap;
boolean flag;
for (int i = left; i right; i++) {
smallest = a[i];
// System.out.println("first" + smallest);
flagIndex = i;
flag = false;
for (int j = i + 1; j = right; j++) {
if (a[j] smallest) {
smallest = a[j];
flagIndex = j;
flag = true;
// System.out.println(smallest + " " + flagIndex);
}
}
if(flag){
forSwap = a[i];
a[i] = smallest;
a[flagIndex] = forSwap;
// System.out.println(smallest);
// printA(a);
// printA(a);
}
}
}
public void quickSort(int[] a, int left, int right) {
int index;
// printA(a);
if (left right right - left 10) { //可以优化如果数组元素小于10就用选择排序
index = partition(a, left, right);
quickSort(a, left, index - 1);
quickSort(a, index + 1, right);
} else {
chooseSort(a, left, right);
}
}
public int partition(int[] a, int left, int right) {
int result = getMiddle(a[left], a[right], a[(int) ((left + right) / 2)]);
int flagIndex;
if (result == 1) {
flagIndex = left;
} else if (result == 2) {
flagIndex = right;
} else {
flagIndex = (int) ((left + right) / 2);
}
int lowIndex, highIndex;
lowIndex = left - 1;
highIndex = right + 1;
int compareValue = a[flagIndex];
int k = a[left];
a[left] = compareValue;
a[flagIndex] = k;
// System.out.println(compareValue);
while (lowIndex + 1 != highIndex) {
if (a[lowIndex + 1] = compareValue) {
lowIndex++;
} else if (a[highIndex - 1] = compareValue) {
highIndex--;
} else {
k = a[lowIndex + 1];
a[++lowIndex] = a[highIndex - 1];
a[--highIndex] = k;
}
}
// printA(a);
a[left] = a[lowIndex];
a[lowIndex] = compareValue;
return lowIndex;
}
public int getMiddle(int a, int b, int c) {
if (a = b) {
if (b = c) {
return 2;
} else {
if (a = c) {
return 3;
} else {
return 1;
}
}
} else {
if (c = b) {
return 2;
} else {
if (a = c) {
return 1;
} else {
return 3;
}
}
}
// return 0;
}
}
一趟快速怕序的具体做法是:附设两个指针low和high,他们的初值分别为low和high,设枢轴记录的关键字为privotkey,则首先从high所指位置向前搜索找到第一个关键字小于pivotkey的记录和枢轴记录互相交换,然后从low所指向的位置起向后搜索,找到第一个关键字大于privotkey的记录和枢轴记录互相交换,重复这两步直至low==high位置.
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class 快速排序_1 {
public static void main(String[] args) throws InterruptedException {
int test[] = {15,23,56,7,13,52,20,7};
new 快速排序_1().qSort(test, 0, test.length-1);
for(int k:test) System.out.println(k);
}
public void qSort(int []array,int low,int high){
if(low
int privot=partition(array,low,high);
qSort(array,low,privot-1);
qSort(array,privot+1,high);
}
}
public int partition(int [] array,int low,int high){
/**
* 选择 low位置 作为曲轴(支点)
*/
int pivot=array[low];
int temp=0;
/**
* 如果 low
*/
while(low
/**
* 先从 high端 开始判断
*/
while(low=pivot) high--;
/**
* 进行 置换操作
*/
if(low
array[low]=array[high];
low++;
}
/**
* 从 low 端判断
*/
while(low
/**
* 进行 置换操作
*/
if(low
array[high]=array[low];
high--;
}
}
array[low]=pivot;
return low;
}
}
参考代码如下,可以按需求自己修改
import java.util.Date;
public class SortThread {
public static void main(String[] args) {
//产生一个随机数组
int[] ary = getArray();
//启动冒泡排序线程
new Thread(new MaoPao(ary)).start();
//启动快速排序线程
new Thread(new KuaiSu(ary)).start();
}
private static int[] getArray() {
//建议数字n不要超过1百万,十万左右就好了
int n = (int) (Math.random()*1000000)+11;
int[] ary= new int[n];
System.out.println("n的值是" + n);
for (int i = 0; i ary.length; i++) {
ary[i] = (int) (Math.random()*100000);
}
return ary;
}
}
//冒泡排序
class MaoPao implements Runnable {
int[] ary;
public MaoPao(int[] ary) {
this.ary = ary;
}
@Override
public void run() {
long st = System.currentTimeMillis();
System.out.println(new Date() + "冒泡排序线程:开始执行排序");
for (int i = 0; i ary.length - 1; i++) {
for (int j = 0; j ary.length - i - 1; j++) {
if (ary[j] ary[j + 1]) {
int temp = ary[j];
ary[j] = ary[j + 1];
ary[j + 1] = temp;
}
}
}
long et = System.currentTimeMillis();
System.out.println(new Date() + "冒泡排序线程完成排序,耗费时间" + (et - st) + "毫秒");
for (int i = 0; i ary.length; i++) {
System.out.println(ary[i]+" ");
}
}
}
//快速排序
class KuaiSu implements Runnable {
int[] ary;
public KuaiSu(int[] ary) {
this.ary = ary;
}
@Override
public void run() {
long st = System.currentTimeMillis();
System.out.println(new Date() + "快速排序线程:开始执行排序");
quickSort(ary, 1, ary.length);
long et = System.currentTimeMillis();
System.out.println(new Date() + "快速排序线程排序完成,耗费时间" + (et - st) + "毫秒");
for (int i = 0; i ary.length; i++) {
System.out.println(ary[i]+" ");
}
}
public static int Partition(int a[], int p, int r) {
int x = a[r - 1];
int i = p - 1;
int temp;
for (int j = p; j = r - 1; j++) {
if (a[j - 1] = x) {
i++;
temp = a[j - 1];
a[j - 1] = a[i - 1];
a[i - 1] = temp;
}
}
temp = a[r - 1];
a[r - 1] = a[i + 1 - 1];
a[i + 1 - 1] = temp;
return i + 1;
}
public static void quickSort(int a[], int p, int r) {
if (p r) {
int q = Partition(a, p, r);
quickSort(a, p, q - 1);
quickSort(a, q + 1, r);
}
}
}