C语言问题求详细解答,谢谢 25
quicksort快速排序#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN100voidprinta...
quicksort 快速排序
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 100
void printarray(int a[],int n)
{
int i;
for (i=0; i<n;)
{
printf("%8d",a[i++]);
if (i % 10 == 0) printf("\n");
}
}
void swap(int a[],int i,int j)
{
int t=a[i];
a[i]=a[j];
a[j]=t;
}
void myqsort(int a[], int left, int right)
{
int i,last;
int m=(left+right)/2;
if (left >= right) return;
swap(a,m,left);
for (i=left+1,last=left; i<=right; i++)
if (a[i]<=a[left])
swap(a,i,++last);
swap(a,left,last);
myqsort(a,left,last-1);
myqsort(a,last+1,right);
}
int main()
{
int a[N];
int i;
srand(time(NULL));
//initialize pseudo number seed
for (i=0; i<N; i++) a[i]=rand() % 10000; //create
random numbers
printf("before quick sorting:\n");
printarray(a,N);
myqsort(a,0,N-1); //quick
sorting
printf("after quick sorting:\n");
printarray(a,N);
return 0;
} 展开
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 100
void printarray(int a[],int n)
{
int i;
for (i=0; i<n;)
{
printf("%8d",a[i++]);
if (i % 10 == 0) printf("\n");
}
}
void swap(int a[],int i,int j)
{
int t=a[i];
a[i]=a[j];
a[j]=t;
}
void myqsort(int a[], int left, int right)
{
int i,last;
int m=(left+right)/2;
if (left >= right) return;
swap(a,m,left);
for (i=left+1,last=left; i<=right; i++)
if (a[i]<=a[left])
swap(a,i,++last);
swap(a,left,last);
myqsort(a,left,last-1);
myqsort(a,last+1,right);
}
int main()
{
int a[N];
int i;
srand(time(NULL));
//initialize pseudo number seed
for (i=0; i<N; i++) a[i]=rand() % 10000; //create
random numbers
printf("before quick sorting:\n");
printarray(a,N);
myqsort(a,0,N-1); //quick
sorting
printf("after quick sorting:\n");
printarray(a,N);
return 0;
} 展开
展开全部
代码贴出,供大家参考指正
能正确编译和运行了,不知可达到你要求,望采纳!
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 100
void printarray(int a[],int n)
{
int i;
for (i=0; i<n;)
{
printf("%8d",a[i++]);
if (i % 10 == 0)
printf("\n");
}
}
void swap(int a[],int i,int j)
{
int t=a[i];
a[i]=a[j];
a[j]=t;
}
void myqsort(int a[], int left, int right)
{
int i,last;
int m=(left+right)/2;
if (left >= right) return;
swap(a,m,left);
for (i=left+1,last=left; i<=right; i++)
if (a[i]<=a[left])
swap(a,i,++last);
swap(a,left,last);
myqsort(a,left,last-1);
myqsort(a,last+1,right);
}
void main(void)
{
int a[N];
int i;
srand(time(NULL)); //initialize pseudo number seed
for (i=0; i<N; i++)
a[i]=rand() % 10000; //create random numbers
printf("before quick sorting:\n");
printarray(a,N);
myqsort(a,0,N-1); //quick sorting
printf("after quick sorting:\n");
printarray(a,N);
}
更多追问追答
追问
什么意思
我是看不懂这个流程,可不可以解答一下
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询