package com.victor.sort.algorithms;
import java.util.ArrayList;
import com.victor.sort.seeds.*;
import com.victor.sort.seeds.Seeds;
/**
* 快速3排序
* @author 黑妹妹牙膏
*
*/
public class Quick3 extends SortAlgorithms {
/**
Algorithm
# choose pivot
swap a[n,rand(1,n)]
# 3-way partition
i = 1, k = 1, p = n
while i < p,
if a[i] < a[n], swap a[i++,k++]
else if a[i] == a[n], swap a[i,--p]
else i++
end
→ invariant: a[p..n] all equal
→ invariant: a[1..k-1] < a[p..n] < a[k..p-1]
# move pivots to center
m = min(p-k,n-p+1)
swap a[k..k+m-1,n-m+1..n]
# recursive sorts
sort a[1..k-1]
sort a[n-p+k+1,n]
Properties
■Not stable
■O(lg(n)) extra space
■O(n2) time, but typically O(n·lg(n)) time
■Adaptive: O(n) time when O(1) unique keys
Discussion
The 3-way partition variation of quick sort has slightly higher overhead compared to the standard 2-way partition version. Both have the same best, typical, and worst case time bounds, but this version is highly adaptive in the very common case of sorting with few unique keys.
The 3-way partitioning code shown above is written for clarity rather than optimal performance; it exhibits poor locality, and performs more swaps than necessary. A more efficient but more elaborate 3-way partitioning method is given in Quicksort is Optimal by Robert Sedgewick and Jon Bentley.
When stability is not required, quick sort is the general purpose sorting algorithm of choice. Recently, a novel dual-pivot variant of 3-way partitioning has been discovered that beats the single-pivot 3-way partitioning method both in theory and in practice.
*/
@Override
protected ArrayList<Integer> doSort(ArrayList<Integer> Alist) {
return sort(Alist,0,Alist.size()-1);
}
private ArrayList<Integer> sort(ArrayList<Integer> Alist,int from,int end) {
if(from >= end) return Alist;
ArrayList<Integer> a = Alist;
int n = end - from;
//choose pivot
java.util.Random rd = new java.util.Random();
int pivot = rd.nextInt(n) + from;
// print(a);
//swap a[n,rand(1,n)]
int temp = a.get(end);
a.set(end, a.get(pivot));
a.set(pivot, temp);
moveMentIncrease();
// System.out.println("pivot:"+pivot);
// print(a);
// 3-way partition
int i=from,k = from,p=end;
while(i<p)
{
if(a.get(i)<a.get(end))
{
//swap a[i++,k++]
temp = a.get(k);
a.set(k, a.get(i));
a.set(i, temp);
moveMentIncrease();
// print(a);
k++;
i++;
// System.out.println("a.get("+i+")<a.get("+end+") swap a[i++,k++]");
// System.out.println("k:"+k+" i:"+i);
}
else
{
int ai = a.get(i);
int aend = a.get(end);
if(ai==aend)
{
// swap a[i,--p]
p--;
temp = a.get(p);
a.set(p, a.get(i));
a.set(i, temp);
moveMentIncrease();
// print(a);
// System.out.println("a.get("+i+")==a.get("+end+") swap a[i,--p]");
// System.out.println("p:"+p+" i:"+i);
}
else
{
i++;
// System.out.println("a.get("+i+")>a.get("+end+")");
// System.out.println("i:"+i);
}
}
}
if(p==from) return a;
//m= min(p-k,n-p+1);
int m = p-k<end-p+1?p-k:end-p+1;
// print(a,k,p,from,end);
for(int swapi=0;swapi<m;swapi++)
{
temp = a.get(k+swapi);
a.set(k+swapi, a.get(end-(m-swapi-1)));
a.set(end-(m-swapi-1), temp);
moveMentIncrease();
}
int fromK = k-1;
int endk = k+end-p+1;
a = sort(a,from,fromK);
a = sort(a,endk,end);
return a;
}
public void print(ArrayList<Integer> a,int k,int p,int from,int end)
{
System.out.println("Items after sorted:");
System.out.println("#######################################");
for(int i=0;i<a.size();i++)
{
boolean selected = false;
if(i==from)
{
//System.out.println("[-"+i+"] : "+a.get(i));
//selected = true;
}
if(i==end)
{
//System.out.println("[+"+i+"] : "+a.get(i));
//selected = true;
}
if(i==k)
{
System.out.println("["+i+"] : "+a.get(i));
selected = true;
}
if(i==p)
{
System.out.println("[p"+i+"] : "+a.get(i));
selected = true;
}
if(!selected) System.out.println(i+": "+a.get(i));
}
System.out.println("#######################################");
}
@Override
public String getName() {
return "Quick3";
}
public static void main(String[] args)
{
Seeds seed1 = new Random();
Seeds seed2 = new NearSorted();
Seeds seed3 = new Reversed();
Seeds seed4 = new FewUniqueKeys();
SortAlgorithms SA = new Quick3();
SA.sort(seed1,800);
SA.sort(seed2,800);
SA.sort(seed3,800);
SA.sort(seed4,800);
//SA.print();
}
}
转载于:https://my.oschina.net/readjava/blog/304442