Java 快速排序分区
我有以下数组:Java 快速排序分区,java,quicksort,Java,Quicksort,我有以下数组: int[] arr = { 19, 4, 2, 3, 9, 2, 10, 2, 7, 12, 5, 16, 8, 3, 11, 14, 0, 5 }; 现在我使用quicksort的分区来使用pivot元素7对数组进行分区: public static void partition(int[] arr, int low, int high) { int pivot = arr[low + (high - low) / 2]; int i = low;
int[] arr = { 19, 4, 2, 3, 9, 2, 10, 2, 7, 12, 5, 16, 8, 3, 11, 14, 0, 5 };
现在我使用quicksort的分区来使用pivot元素7对数组进行分区:
public static void partition(int[] arr, int low, int high) {
int pivot = arr[low + (high - low) / 2];
int i = low;
int j = high;
while (i <= j) {
// If the current value from the left list is smaller then the pivot
// element then get the next element from the left list
while (arr[i] < pivot) {
i++;
}
// If the current value from the right list is larger then the pivot
// element then get the next element from the right list
while (arr[j] > pivot) {
j--;
}
// If we have found a values in the left list which is larger then
// the pivot element and if we have found a value in the right list
// which is smaller then the pivot element then we exchange the
// values.
// As we are done we can increase i and j
if (i <= j) {
swap(arr, i, j);
i++;
j--;
}
}
}
我认为除了枢轴元素以外的每个元素都必须位于右侧。但是为什么12是7?< /P> < P> A<代码>分区< /Cuff> C++中的函数如下:< /P>
while (first!=last) {
while (pred(*first)) {
++first;
if (first==last) return first;
}
do {
--last;
if (first==last) return first;
} while (!pred(*last));
swap (*first,*last);
++first;
}
return first;
First
和last
是指向数组中元素的迭代器,类似于i
和j
变量pred
是predicate的缩写,可以是i这个实现不能保证您所期望的。它只需执行以下操作(前提是更改为arr[i]为快速排序方法创建一个类
package com.Ramesh;
public class QuickSort {
public void sort(int[] a,int left,int right){
if(left<right)
{
int partition=getPartition(a, left, right);
sort(a,left,partition-1);
sort(a,partition+1,right);
}
}
public int getPartition(int[] a,int l,int r)
{
int pivot=a[l];
int left=l;
int right=r;
while(left<right)
{
while(a[left]<pivot){
left++;
}
while(a[right]>pivot){
right--;
}
if(left<right){
int temp=a[left];
a[left]=a[right];
a[right]=temp;
}
}
return right;
}
}
package com.Ramesh;
公共类快速排序{
公共void排序(int[]a,int左,int右){
如果(左)
// Note. <= is the opposite of >
// Which logically is the same as
// pred is the opposite of !pred
while (i != j) {
while (i <= midpoint) {
++i;
if (i == j) return i;
}
do {
--j;
if (i == j) return i;
} while (i > midpoint);
swap (i, j);
++i;
}
return i;
...
for (int i = 0; i < get_midpoint(...); i++)
for (int i = get_midpoint; i < end_of_array; i++)
package com.Ramesh;
public class QuickSort {
public void sort(int[] a,int left,int right){
if(left<right)
{
int partition=getPartition(a, left, right);
sort(a,left,partition-1);
sort(a,partition+1,right);
}
}
public int getPartition(int[] a,int l,int r)
{
int pivot=a[l];
int left=l;
int right=r;
while(left<right)
{
while(a[left]<pivot){
left++;
}
while(a[right]>pivot){
right--;
}
if(left<right){
int temp=a[left];
a[left]=a[right];
a[right]=temp;
}
}
return right;
}
}
import java.util.Scanner;
public class Execute {
private int[] a;
private int len;
public int[] getA() {
return a;
}
public void setA(int[] a) {
this.a = a;
}
public int getLen() {
return len;
}
public void setLen(int len) {
this.len = len;
}
public static void main(String[] args) {
Execute ex=new Execute();
ex.takeInput();
QuickSort q=new QuickSort();
q.sort(ex.getA(),0,ex.getLen()-1);
System.out.println("Printing the the Sorted Object");
System.out.println(ex);
}
public void takeInput()
{
Scanner s1=new Scanner(System.in);
System.out.println("Please enter the no of element to be sorted");
len=s1.nextInt();
a=new int[len];
System.out.println("Pls enter the elements");
for(int i=0;i<len;i++){
a[i]=s1.nextInt();
}
}
@Override
public String toString(){
StringBuffer s=new StringBuffer("");
for(int i=0;i<this.len;i++){
s.append(this.a[i]+"\n");
}
return s.toString();
}
}