C# 快速排序算法的实现
我从这本书中找到了快速排序算法 这就是算法C# 快速排序算法的实现,c#,algorithm,quicksort,C#,Algorithm,Quicksort,我从这本书中找到了快速排序算法 这就是算法 QUICKSORT (A, p, r) if p < r q = PARTITION(A, p, r) QUICKSORT(A, p, q-1) QUICKSORT(A, q+1, r) PARTITION(A, p, r) x=A[r] i=p-1 for j = p to r - 1 if A <= x i = i + 1 exchange A[i] with A[j] exchang
QUICKSORT (A, p, r)
if p < r
q = PARTITION(A, p, r)
QUICKSORT(A, p, q-1)
QUICKSORT(A, q+1, r)
PARTITION(A, p, r)
x=A[r]
i=p-1
for j = p to r - 1
if A <= x
i = i + 1
exchange A[i] with A[j]
exchange A[i+1] with A[r]
return i + 1
快速排序(A、p、r)
如果p快速排序
永远不会停止使用长度为0的子列表进行自身递归
更改此项:
if (low < high)
pivot_loc = partition(input, low, high);
quicksort(input, low, pivot_loc - 1);
quicksort(input, pivot_loc + 1, high);
if(低<高)
pivot_loc=分区(输入、低、高);
快速排序(输入,低位,枢轴位置-1);
快速排序(输入,枢轴位置+1,高);
为此:
if (low < high) {
pivot_loc = partition(input, low, high);
quicksort(input, low, pivot_loc - 1);
quicksort(input, pivot_loc + 1, high);
}
if(低<高){
pivot_loc=分区(输入、低、高);
快速排序(输入,低位,枢轴位置-1);
快速排序(输入,枢轴位置+1,高);
}
除了Deestan的答案,您还有以下错误:
for (int j = low; j < high-1; j++)
for(int j=low;j
应该是:
for (int j = low; j < high; j++)
for(int j=low;j
以防您需要一些较短的快速排序代码:
IEnumerable<int> QuickSort(IEnumerable<int> i)
{
if (!i.Any())
return i;
var p = (i.First() + i.Last) / 2 //whichever pivot method you choose
return QuickSort(i.Where(x => x < p)).Concat(i.Where(x => x == p).Concat(QuickSort(i.Where(x => x > p))));
}
IEnumerable快速排序(IEnumerable i)
{
如果(!i.Any())
返回i;
var p=(i.First()+i.Last)/2//选择哪种枢轴方法
返回快速排序(i.Where(x=>xx==p).Concat(快速排序(i.Where(x=>x>p)));
}
当然,用任何合适的方法获取p(pivot)。一个简单的快速排序实现
使用系统;
使用System.Collections.Generic;
使用System.Linq;
名称空间算法
{
类快速排序
{
公共void QuickSortMethod()
{
var input=System.Console.ReadLine();
字符串[]sInput=input.Split(“”);
int[]iInput=Array.ConvertAll(sInput,int.Parse);
QuickSortNow(输入,0,输入长度-1);
对于(int i=0;iif(iInput[i]这是快速排序算法的最短实现(无StackOverflowException
)
IEnumerable快速排序(IEnumerable i),其中T:IComparable
{
如果(!i.Any())返回i;
var p=i.ElementAt(new Random().Next(0,i.Count()-1));
返回快速排序(i.Where(x=>x.CompareTo(p)<0)).Concat(i.Where(x=>x.CompareTo(p)==0)).Concat(快速排序(i.Where(x=>x.CompareTo(p)>0));
}
快速排序的简单通用C#实现,可以使用第一个值、最后一个值或任何其他中间值作为pivot
using System;
namespace QuickSort
{
class Program
{
static void Main(string[] args)
{
int[] arInt = { 6, 4, 2, 8, 4, 5, 4, 5, 4, 5, 4, 8, 11, 1, 7, 4, 13, 5, 45, -1, 0, -7, 56, 10, 57, 56, 57, 56 };
GenericQuickSort<int>.QuickSort(arInt, 0, arInt.Length - 1);
string[] arStr = { "Here", "Is", "A", "Cat", "Really", "Fast", "And", "Clever" };
GenericQuickSort<string>.QuickSort(arStr, 0, arStr.Length - 1); ;
Console.WriteLine(String.Join(',', arInt));
Console.WriteLine(String.Join(',', arStr));
Console.ReadLine();
}
}
class GenericQuickSort<T> where T : IComparable
{
public static void QuickSort(T[] ar, int lBound, int uBound)
{
if (lBound < uBound)
{
var loc = Partition(ar, lBound, uBound);
QuickSort(ar, lBound, loc - 1);
QuickSort(ar, loc + 1, uBound);
}
}
private static int Partition(T[] ar, int lBound, int uBound)
{
var start = lBound;
var end = uBound;
var pivot = ar[uBound];
// switch to first value as pivot
// var pivot = ar[lBound];
while (start < end)
{
while (ar[start].CompareTo(pivot) < 0)
{
start++;
}
while (ar[end].CompareTo(pivot) > 0)
{
end--;
}
if (start < end)
{
if (ar[start].CompareTo(ar[end]) == 0)
{
start++;
}
else
{
swap(ar, start, end);
}
}
}
return end;
}
private static void swap(T[] ar, int i, int j)
{
var temp = ar[i];
ar[i] = ar[j];
ar[j] = temp;
}
}
}
使用系统;
名称空间快速排序
{
班级计划
{
静态void Main(字符串[]参数)
{
int[]arInt={6,4,2,8,4,5,4,4,5,4,8,11,1,7,4,13,5,45,-1,0,-7,56,10,57,56};
GenericQuickSort.QuickSort(arInt,0,arInt.Length-1);
string[]arStr={“这里”、“是”、“A”、“猫”、“真的”、“快”、“和”、“聪明”};
GenericQuickSort.QuickSort(arStr,0,arStr.Length-1);
Console.WriteLine(String.Join(',,arInt));
Console.WriteLine(String.Join(',,arStr));
Console.ReadLine();
}
}
类GenericQuickSort,其中T:IComparable
{
公共静态void快速排序(T[]ar,int-lBound,int-uBound)
{
if(lBound0)
{
结束--;
}
如果(开始<结束)
{
if(ar[start].CompareTo(ar[end])==0
{
启动++;
}
其他的
{
掉期(应收、开始、结束);
}
}
}
返回端;
}
私有静态无效交换(T[]ar,int i,int j)
{
var-temp=ar[i];
ar[i]=ar[j];
ar[j]=温度;
}
}
}
输出:
-7,-1,0,1,2,4,4,4,4,5,5,6,7,8,8,10,11,13,45,56,56,56,57,57
A、 还有,猫,聪明,快,在这里,真的
这里需要注意的一件重要事情是,这个优化的简单代码正确地处理了重复项。我尝试了几个发布的快速排序代码。这些代码没有给出正确的结果(整数数组)输入或挂起,例如and。因此,如果作者还想使用处理重复的代码,这将是一个很好的参考。我不理解问题的最后一部分-打印内容-因为您的代码没有显示您实际调用的位置quicksort
。为什么不在以后的下一条语句中打印呢r调用快速排序
?因为我的代码有不定式循环,所以我在这里不知道
using System;
using System.Collections.Generic;
using System.Linq;
namespace AlgorithmsMadeEasy
{
class QuickSort
{
public void QuickSortMethod()
{
var input = System.Console.ReadLine();
string[] sInput = input.Split(' ');
int[] iInput = Array.ConvertAll(sInput, int.Parse);
QuickSortNow(iInput, 0, iInput.Length - 1);
for (int i = 0; i < iInput.Length; i++)
{
Console.Write(iInput[i] + " ");
}
Console.ReadLine();
}
public static void QuickSortNow(int[] iInput, int start, int end)
{
if (start < end)
{
int pivot = Partition(iInput, start, end);
QuickSortNow(iInput, start, pivot - 1);
QuickSortNow(iInput, pivot + 1, end);
}
}
public static int Partition(int[] iInput, int start, int end)
{
int pivot = iInput[end];
int pIndex = start;
for (int i = start; i < end; i++)
{
if (iInput[i] <= pivot)
{
int temp = iInput[i];
iInput[i] = iInput[pIndex];
iInput[pIndex] = temp;
pIndex++;
}
}
int anotherTemp = iInput[pIndex];
iInput[pIndex] = iInput[end];
iInput[end] = anotherTemp;
return pIndex;
}
}
}
/*
Sample Input:
6 5 3 2 8
Calling Code:
QuickSort qs = new QuickSort();
qs.QuickSortMethod();
*/
Code Implemented with Iteration With last element as Pivot
<code>https://jsfiddle.net/zishanshaikh/5zxvwoq0/3/ </code>
function quickSort(arr,l,u) {
if(l>=u)
{
return;
}
var pivot=arr[u];
var pivotCounter=l;
for(let i=l;i<u;i++)
{
if(arr[i] <pivot )
{
var temp= arr[pivotCounter];
arr[pivotCounter]=arr[i] ;
arr[i]=temp;
pivotCounter++;
}
}
var temp2= arr[pivotCounter];
arr[pivotCounter]=arr[u] ;
arr[u]=temp2;
quickSort(arr,pivotCounter+1,u);
quickSort(arr,0,pivotCounter-1);
}
<code>https://jsfiddle.net/zishanshaikh/exL9cdoe/1/</code>
Code With first element as Pivot
//Logic For Quick Sort
function quickSort(arr,l,u) {
if(l>=u)
{
return;
}
var pivot=arr[l];
var pivotCounter=l+1;
for(let i=l+1;i<u;i++)
{
if(arr[i] <pivot )
{
var temp= arr[pivotCounter];
arr[pivotCounter]=arr[i] ;
arr[i]=temp;
pivotCounter++;
}
}
var j=pivotCounter-1;
var k=l+1;
while(k<=j)
{
var temp2= arr[k-1];
arr[k-1]=arr[k] ;
arr[k]=temp2;
k++;
}
arr[pivotCounter-1]=pivot;
quickSort(arr,pivotCounter,u);
quickSort(arr,0,pivotCounter-2);
}
IEnumerable<T> QuickSort<T>(IEnumerable<T> i) where T :IComparable
{
if (!i.Any()) return i;
var p = i.ElementAt(new Random().Next(0, i.Count() - 1));
return QuickSort(i.Where(x => x.CompareTo(p) < 0)).Concat(i.Where(x => x.CompareTo(p) == 0)).Concat(QuickSort(i.Where(x => x.CompareTo(p) > 0)));
}
using System;
namespace QuickSort
{
class Program
{
static void Main(string[] args)
{
int[] arInt = { 6, 4, 2, 8, 4, 5, 4, 5, 4, 5, 4, 8, 11, 1, 7, 4, 13, 5, 45, -1, 0, -7, 56, 10, 57, 56, 57, 56 };
GenericQuickSort<int>.QuickSort(arInt, 0, arInt.Length - 1);
string[] arStr = { "Here", "Is", "A", "Cat", "Really", "Fast", "And", "Clever" };
GenericQuickSort<string>.QuickSort(arStr, 0, arStr.Length - 1); ;
Console.WriteLine(String.Join(',', arInt));
Console.WriteLine(String.Join(',', arStr));
Console.ReadLine();
}
}
class GenericQuickSort<T> where T : IComparable
{
public static void QuickSort(T[] ar, int lBound, int uBound)
{
if (lBound < uBound)
{
var loc = Partition(ar, lBound, uBound);
QuickSort(ar, lBound, loc - 1);
QuickSort(ar, loc + 1, uBound);
}
}
private static int Partition(T[] ar, int lBound, int uBound)
{
var start = lBound;
var end = uBound;
var pivot = ar[uBound];
// switch to first value as pivot
// var pivot = ar[lBound];
while (start < end)
{
while (ar[start].CompareTo(pivot) < 0)
{
start++;
}
while (ar[end].CompareTo(pivot) > 0)
{
end--;
}
if (start < end)
{
if (ar[start].CompareTo(ar[end]) == 0)
{
start++;
}
else
{
swap(ar, start, end);
}
}
}
return end;
}
private static void swap(T[] ar, int i, int j)
{
var temp = ar[i];
ar[i] = ar[j];
ar[j] = temp;
}
}
}