为什么c语言排序函数很慢 c语言排序问题

数据结构(c语言)中快速排序什么时候排序最慢,什么情况下使用快速排序?

当待排序的序列已经有序(不管是升序还是降序),此时快速排序最慢,一般当数据量很大的时候,用快速排序比较好,为了避免原来的序列有序,一般采用改进的快速排序算法,在排序之前随机交换两个元素的位置,就可以达到目的了,有一本书,叫《算法设计、分析与实现:C、C++和java》徐子珊著。可以看看,里面写了很多基本的算法

创新互联专注于企业成都全网营销推广、网站重做改版、玛纳斯网站定制设计、自适应品牌网站建设、H5响应式网站商城系统网站开发、集团公司官网建设、成都外贸网站建设公司、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为玛纳斯等各大城市提供网站开发制作服务。

c语言函数快速排序问题

#include stdio.h

#include stdlib.h

int qksort(int a[],int leftposition,int rightposition);

int main(int argc, char *argv[]) {

int list[]={98,34,56,27,78,73,70,90,28,84,45,85,12,18,54,34,29};

int i;

qksort(list,0,16);

for(i=0;i17;i++){

printf("%d ",list[i]);

}

printf("\n");

return 0;

}

int qksort(int a[],int leftposition,int rightposition){

int pivot,i,j,swap;

if(rightpositionleftposition){

pivot=a[rightposition];

i=leftposition-1;

j=rightposition;

while(1){

while(a[++i]pivot);

while(a[--j]pivot);

if(i=j)break;

swap=a[i];

a[i]=a[j];

a[j]=swap;

}

swap=a[i];

a[i]=a[rightposition];

a[rightposition]=swap;

qksort(a,leftposition,i-1);

qksort(a,i+1,rightposition);

}

return 0;

}

正常的啊:

C语言 程序多次运行后,速度变慢的问题!

My guess would be you are adding while loops when you press B.

From what I understand, you are not using any heap memory, cuz there is no new or malloc being called.

The size of a stack is usually 1M under Visual Studio by default, it canbe changed somewhere in the project property, and it is where you declare variables e.g

int x[10]; char c;

If you declare int x[1024*1024], which is 4MB under a 32bit system, it runs out the stack memory and results in an error, e.g. "Program stops working" and stuff.

If you want to prevent this from happening, you should actually use new or malloc cuz it gives you sufficient space on both physic memory and virtual memory on which you would never run out.

Above is just a brief explanation about stack and heap.

Can you please upload the code, especially for the part that responds to "B", otherwise it would be purely guessing and wouldn't be helpful at all.

请教C语言 快速排序问题

没仔细看你的程序,我按标准原理写了个,你看看

void Quick_Sort(int list[ ], int left, int right) //lfet = 0, right = MAX

{

int s;

int i, j;

int temp;

if(left right)

{

s = list[left];

i = left-1;

j = right + 1;

while(i+1!=j) {

if(list[i+1]=s)

i++;

else if(list[j-1]s)

j--;

else {

temp=list[i+1];

list[++i]=list[j-1];

list[--j]=temp;

}

}

list[left] = list[i];

list[i] = s;

Quick_Sort(list, left, i - 1); //对左边递归

Quick_Sort(list, i + 1, right); //对右边递归

}

}

求解c语言排序链表数据域问题,这个函数使用时程序直接卡死,如图所示

这个代码最大的问题如图,temp是个指针,你在申请完内存后马上让它指向NULL,后面你想往temp拷东西必然程序要崩溃的。我估计你是想初始化为0。你可以用menset函数。

另外建议排序这个确实可以只改变指针指向就可以了没有必要那么多拷贝,交换他们在整个链表的位置。这应该也不算你说的改变了数据结构吧。

再者如果确实要拷贝也可以用memcpy函数一次性拷贝,把整个结构体拷贝过去,这样你的代码可以大大减少。

C语言,产生不相同的随机数过程怎么这么慢?

算法不好。简单的办法就是先生成,最后判断一下是否重复就可以了。

但更重要的是你也不必要更不应该每次产生随机数时都用srand()函数,只要在程序最开始时用一次就可以了,否则反而变得很不随机。

简单的解决办法就是把srand()语句挪到main()函数的开头。

伪随机数的产生原理,其实就是以某个整数为初值,用一个算式不断地迭代运算(rand()函数内部会记下上一次运算的结果)。而srand()就是指定这个初值。

通常我们用srand(time(NULL));语句,就是用当前时间(单位是秒)给rand()的运算设初值,但由于你每次都用这句,所以当时间相同时,运算出来的伪随机数就是相同的。

而由于time()函数的单位是秒,所以程序就一直循环,直到过了大约1秒钟,两次srand(time(NULL));设的初始数字不同时,才可能找到下一个数,而且这个数还是不够随机的。如此一来,你每产生一个新数的时间至少要1秒钟,自然7个数要10秒钟也就很正常了。

你可以在网上找一下rand()函数的具体实现,这对你理解它的运行机理可能会有好处。

这是rand()和srand()在VC中的实现!

static long holdrand = 1L;

/***

*void srand(seed) - seed the random number generator

*

*Purpose:

* Seeds the random number generator with the int given. Adapted from the

* BASIC random number generator.

*

*Entry:

* unsigned seed - seed to seed rand # generator with

*

*Exit:

* None.

*

*Exceptions:

*

*******************************************************************************/

void __cdecl srand (

unsigned int seed

)

{

holdrand = (long)seed;

}

/***

*int rand() - returns a random number

*

*Purpose:

* returns a pseudo-random number 0 through 32767.

*

*Entry:

* None.

*

*Exit:

* Returns a pseudo-random number 0 through 32767.

*

*Exceptions:

*

*******************************************************************************/

int __cdecl rand (

void

)

{

return(((holdrand = holdrand * 214013L + 2531011L) 16) 0x7fff);

}


网站名称:为什么c语言排序函数很慢 c语言排序问题
网页URL:http://myzitong.com/article/hipjpj.html