我正在尝试用 C 语言实现快速排序,但排序没有按预期进行。
#include <stdio.h>
void quickSort(int arr[], int length);
void qSrtRec(int arr[], int low, int high);
void swap(int* x, int* y);
int partition(int arr[], int low, int high);
int main() {
int arr[] = { 5,3,7,0,1,4,8,9,6,2 }; // test array
quickSort(arr, sizeof(arr) / sizeof(arr[0]));
for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
printf("%d, ", arr[i]);
printf("\n");
return 0;
}
void swap(int* x, int* y) {
*x += *y;
*y = *x - *y;
*x -= *y; …Run Code Online (Sandbox Code Playgroud) 我想问一个普遍的问题.
我正在比较C,Java和Haskell上的排序运行时间.提供相同级别的优化(当然可能会有所不同),哪种语言应该具有最快的运行时间,哪一种最慢(理论上)?他们都将阅读相同的文本文件并按字母顺序排序单词.如果我能得到一个很好的深度解释,并且非常感激.
谢谢〜
#include <iostream>
using namespace std;
int t,n,k,m,i,j;
int l[100002],r[100002],c[100002],a[100002],total=0;
int swap( int *a, int *b)
{
int temp=*a;
*a=*b;
*b=temp;
}
int pivot( int l, int h)
{
int x=c[h],i=l,j=l-1,temp;
for(i=l;i<h;i++)
{
if(c[i]<x)
{
j++;
swap(&c[i],&c[j]);
swap(&r[i],&r[j]);
swap(&l[i],&l[j]);
}
}
j++;
swap(&c[h],&c[j]);
swap(&l[h],&l[j+1]);
swap(&r[h],&r[j+1]);
return j;
}
int quick( int l, int h)
{
int p;
if(l<h)
{
p=pivot(l,h);
quick(l,p-1);
quick(p+1,h);
}
}
int main() {
cin>>t;
while(t--)
{
total=0;
cin>>n>>k>>m;
for(i=1;i<=n;i++)
{
cin>>a[i];
total+=a[i];
}
for(i=1;i<=m;i++)
cin>>l[i]>>r[i]>>c[i];
quick(1,m);
for(i=1;i<=m;i++) …Run Code Online (Sandbox Code Playgroud) 我写了这段代码:
class test {
public static void main(String args[]) {
int array[] = {1,2,3,4,5,6};
int i = 0;
int b = 0;
int c = 0;
method.dog(i, b, c, array);
}
}
public class method {
static void dog (int i, int b, int c, int array[]) {
if (array[i] <= array[c]) {
if (c == (int) array.length +1 ) {
int y = array[i];
array[i] = array[b];
array[b] = y;
if (b == array.length +1) return;
else b++; i …Run Code Online (Sandbox Code Playgroud)