62,616
社区成员
发帖
与我相关
我的任务
分享
import java.util.Random;
class QuickSort3{
private int[] source;
public QuickSort3(int[] source){
this.source = source;
}
public void sort(){
recQuickSort(0,source.length-1);
}
private void recQuickSort(int left, int right){
int size = right - left + 1;
if (size < 10){//当子数组长度为9时使用插入排序
insertionSort(left, right);
} else {
int median = medianOf3(left, right);
int partition = partition(left, right, median);
recQuickSort(left, partition - 1);
recQuickSort(partition + 1, right);
}
}
private int medianOf3(int left, int right){
int center = (left + right) / 2;
if (source[left] > source[center])
swap(left, center);
if (source[left] > source[right])
swap(left, right);
if (source[center] > source[right])
swap(center, right);
swap(center, right-1);
return source[right-1];
}
private int partition(int left, int right, int pivot){
int leftPtr = left;
int rightPtr = right - 1;
while (true){
while (source[++leftPtr] < pivot);
while (source[--rightPtr] > pivot);
if (leftPtr > rightPtr){
break;
} else {
swap(leftPtr, rightPtr);
}
}
swap(leftPtr, right - 1);
return leftPtr;
}
private void swap(int idx1, int idx2){
int temp = source[idx1];
source[idx1] = source[idx2];
source[idx2] = temp;
}
private void insertionSort(int left, int right){//插入排序法
int temp;
for (int i=left+1; i<right+1; i++){
int j;
temp = source[i];
for (j=i-1; j>-1; j--){
if (temp < source[j])
source[j] = source[j+1];
else
break;
}
}
}
public static void main(String args[]){
int[] source = new int[10000];
QuickSort3 qs3;
Random random = new Random();
for (int i=0; i<source.length; i++){
source[i] = random.nextInt(10000);
}
qs3 = new QuickSort3(source);
long s = System.currentTimeMillis();
qs3.sort();
long e = System.currentTimeMillis();
for (int i: source){
System.out.print(i+",");
}
System.out.println("耗时:"+(e-s)/1000.0+"秒");
}
}
class QuickSort{
private int[] source;
public QuickSort(int[] source){
this.source = source;
}
public void sort(){
recQuickSort(0,source.length-1);
}
private void recQuickSort(int left, int right){
if (left >= right){
return;
} else {
int pivot = source[right];//选择子数组的最右端为枢纽
int partition = partition(left, right, pivot);
recQuickSort(left, partition-1);
recQuickSort(partition+1, right);
}
}
private int partition(int left, int right, int pivot){//划分算法
int leftIdx = left - 1;
int rightIdx = right ;
while (true){
while (source[++leftIdx] < pivot);
while (rightIdx > 0 && source[--rightIdx] >= pivot);
if(leftIdx < rightIdx){
swap(leftIdx, rightIdx);
} else {
break;
}
}
swap(leftIdx, right);
return leftIdx;
}
private void swap(int idx1, int idx2){
int temp = source[idx1];
source[idx1] = source[idx2];
source[idx2] = temp;
}
public static void main(String[] args){
int[] n = new int[6046]; //超过6046溢出
for (int i=0; i<6046; i++){
n[i] = 6046-i;
}
QuickSort qs = new QuickSort(n);
qs.sort();
for (int i: n){
System.out.print(i+",");
}
}
}
import java.util.Random;
class QuickSort2{
private int[] source;
public QuickSort2(int[] source){
this.source = source;
}
public void sort(){
recQuickSort(0,source.length-1);
}
private void recQuickSort(int left, int right){
int size = right - left + 1;
if (size <= 3){
manualSort(left, right);
} else {
int median = medianOf3(left, right);
int partition = partition(left, right, median);
recQuickSort(left, partition - 1);
recQuickSort(partition + 1, right);
}
}
private int medianOf3(int left, int right){//用三数据项取中法选择枢纽
int center = (left + right) / 2;
if (source[left] > source[center])
swap(left, center);
if (source[left] > source[right])
swap(left, right);
if (source[center] > source[right])
swap(center, right);
swap(center, right-1);
return source[right-1];
}
private int partition(int left, int right, int pivot){
int leftPtr = left;
int rightPtr = right - 1;
while (true){
while (source[++leftPtr] < pivot);
while (source[--rightPtr] > pivot);//这里不能加等号,否则数组索引有可能越界。
if (leftPtr > rightPtr){
break;
} else {
swap(leftPtr, rightPtr);
}
}
swap(leftPtr, right - 1);
return leftPtr;
}
private void swap(int idx1, int idx2){
int temp = source[idx1];
source[idx1] = source[idx2];
source[idx2] = temp;
}
private void manualSort(int left, int right){//用手动排序为长度为3或3以下的子数组进行排序
int size = right - left + 1;
switch (size){
case 2:
if (source[left] > source[right])
swap (left, right);
break;
case 3:
if (source[left] > source[left + 1])
swap(left, left + 1);
if (source[left] > source[right])
swap(left, right);
if (source[left + 1] > source[right])
swap(left + 1, right);
break;
}
}
public static void main(String args[]){
int[] source = new int[10000];
QuickSort2 qs2;
Random random = new Random();
for (int i=0; i<source.length; i++){
source[i] = random.nextInt(source.length);
}
qs2 = new QuickSort2(source);
long s = System.currentTimeMillis();
qs2.sort();
long e = System.currentTimeMillis();
for (int i: source){
System.out.print(i+",");
}
System.out.println("耗时:"+(e-s)/1000.0+"秒");
}
}
package sort;
public class SortDemo {
public static void main(String args[]){
int[] bigArr = new int[100000];
for(int i=0;i<100000;i++)
bigArr[i] = (int)(Math.random()*400000);
Sort.insertSort(bigArr, 100000);
//Sort.quickSort(0,99999,bigArr);
//display result, optional
//for(int i=0;i<bigArr.length;i++)
// System.out.print(bigArr[i]+",");
System.out.println("Done");
}
}
class Sort{
public static void swap(int x,int y,int[] target){
int temp = target[x];
target[x] = target[y];
target[y] = temp;
}
public static void insertSort(int[] target,int length){
for(int i=1;i<length;i++)
for(int j=i;j>0&&target[j-1]>target[j];j--)
swap(j-1,j,target);
}
public static void quickSort(int low, int high, int[] target){
if(high<=low)
return;
int temp = target[low];
int i = low, j=high+1;
while(true){
do{
i++;
}while(i<=high&&target[i]<temp);
do{
j--;
}while(target[j]>temp);
if(i>j)
break;
swap(i,j,target);
}
swap(low,j,target);
quickSort(low,j-1,target);
quickSort(j+1,high,target);
}
}
import java.util.Random;
class Shell{
public void sort(int[] source){
int h = 1;
int temp;
while (h*3+1<source.length+1){
h = h*3+1;
}
while (h != 0){
for(int i=0; i<h; i++){
for (int j=i; j<source.length-h; j+=h){
temp = source[j+h];
int k;
for (k=j; k>i-h; k-=h){
if (temp<source[k]){
source[k+h] = source[k];
} else {
break;
}
}
source[k+h] = temp;
}
}
h = (h-1)/3;
}
}
public static void main(String[] args){
Shell shell = new Shell();
Random random = new Random();
int[] test = new int[10000];
for (int i=0; i<test.length; i++){
test[i] = random.nextInt(10000);
}
for(int i : test){
System.out.print(i+",");
}
System.out.println();
long s = System.currentTimeMillis();
shell.sort(test);
long e = System.currentTimeMillis();
for(int i : test){
System.out.print(i+",");
}
System.out.println("\n"+(e-s)/1000.0+"秒");
}
}
import java.util.Random;
public class JoSort {
public void sort(int[] n){
for (int i = 0; i < n.length; i += 2){
for (int j = 0; j < n.length - 1; j += 2){
if (n[j] > n[j + 1]){
n[j] = n[j + 1] + n[j];
n[j + 1] = n[j] - n[j + 1];
n[j] = n[j] - n[j + 1];
}
}
for (int j = 1; j < n.length - 1; j += 2){
if (n[j] > n[j + 1]){
n[j] = n[j + 1] + n[j];
n[j + 1] = n[j] - n[j + 1];
n[j] = n[j] - n[j + 1];
}
}
}
}
public static void main(String[] args){
int[] n = new int[10000];
Random r = new Random();
for (int i = 0; i < n.length; i++){
n[i] = r.nextInt(10000);
}
JoSort js = new JoSort();
long s = System.currentTimeMillis();
js.sort(n);
long e = System.currentTimeMillis();
for (int i = 0; i < n.length - 1; i++){
System.out.print(n[i]+",");
}
System.out.println(n[n.length-1]);
System.out.println("奇偶排序耗时:"+(e - s) / 1000.0 + "秒");
}
}