快速排序

到百度贴吧首页
新闻   网页   贴吧   知道   MP3   图片   视频   百科
    吧内搜索 | 帮助

快速排序

1楼

void quickSort(char* arr,int startPos, int endPos)
{
int i,j;
char ch;
ch=arr[startPos];
i=startPos;
j=endPos;
while(i<j)
{
while(arr[j]>=ch && i<j)--j;
arr[i]=arr[j];
while(arr[i]<=ch && i<j)++i;
arr[j]=arr[i];
}
arr[i]=ch;
if(i-1>startPos) quickSort(arr,startPos,i-1);
if(endPos>i+1) quickSort(arr,i+1,endPos);
}

void main()
{
char ch[]="qwertyuiopasdfghjklzxcvbnm";
quickSort(ch,0,25);
printf("\n%s\n",ch);
}

2楼

程序不正确吧
222.33.200.*

3楼

buzhidao
218.69.134.*

4楼

什么破jb程序,胡写
222.50.13.*

5楼

什么叫快速排序 qsort
#include<string.h>
#include<stdlib.h>
#include<stdio.h>

struct student{
 int xuehao;
 char name[20];
 int chengji;
};

typedef struct student stu;

sortbyxuehao(const void *,const void *);
sortbyname(const void *,const void *);
sortbychengji(const void *,const void *);

void main()
{
 int i;
 stu a[3];
 int choice;
 int (*p)(const void * ,const void *);
 
 printf("please input record:\n");
 printf("xuehao name chengji:\n");
 for(i=0;i<3;i++)
 {
 scanf("%d",&a[i].xuehao);
scanf("%s",a[i].name);
scanf("%d",&a[i].chengji); 
 }
 printf("choice_1: sorted xuehao:\n");
 printf("choice_2: sorted name\n");
 printf("choice_3: sorted chengji\n");

 scanf("%d",&choice);
 while(choice!=0)
 {
 if(choice==1)
 p=sortbyxuehao;
 if(choice==2)
 p=sortbyname;
 if(choice==3)
 p=sortbychengji;

 qsort(a,3,sizeof(stu),p);

 if(choice==1)
 for(i=0;i<3;i++)
 printf("\n%d\t%s\t%d",a[i].xuehao,a[i].name,a[i].chengji);
 if(choice==2)
 for(i=0;i<3;i++)
 printf("\n%s\t%d\t%d",a[i].name,a[i].xuehao,a[i].chengji);
 if(choice==3)
 for(i=0;i<3;i++)
 printf("\n%d\t%s\t%d",a[i].chengji,a[i].name,a[i].xuehao);
 printf("\n");
 scanf("%d",&choice);
 }
 
}

sortbyxuehao(const void *p,const void *q)
{
 stu *x,*y;
 x=(stu*)p;
 y=(stu*)q;

 return (-((*x).xuehao-(*y).xuehao));
}

sortbyname(const void *p,const void *q)
{
 stu *x,*y;
 x=(stu*)p;
 y=(stu*)q;

 return strcmp((*x).name,(*y).name);

}

sortbychengji(const void *p,const void *q)
{
 stu *x,*y;
 x=(stu*)p;
 y=(stu*)q;

 return ((*x).chengji-(*y).chengji);


}

61.191.140.*

6楼

楼上的,那还叫“快速”???
“蜗牛”差不多...
楼主是对的,和书上几乎一样,只不过看不懂!!!

220.170.23.*

7楼

程序是写对了,不过是针对25个字符型的, 我们现在是针对线性表的, 所以要修改好多的内容,不过楼主的C程应该很强,要做到那么的精短我是不可能的!~
221.13.11.*

8楼

搞球不懂!
221.13.11.*

9楼

你们在这里张吧张吧什么呀!
218.3.211.*

10楼

ding
210.29.140.*

11楼

搂主正确,冒泡排序的改进算法,最快时间复杂性可达n*log2n

12楼

看了半天没明白,还是觉得这个好懂,呵,因为是我菜鸟
main()
 {
 char sz[]="qwertyuiopasdfghjklzxcvbnm";
 quickSort(sz,0,25);
 printf("\n%s\n",sz);
 getch();
 }
 void quickSort(char* arr,int startPos, int endPos)

 int i,j,a;
 char ch;
 j=endPos;
 for(i=startPos;j>i;--j)
 {
 for(a=i;a<j;++a)
 {
 if( arr[a]>arr[a+1])
 {
 ch=arr[a+1];
 arr[a+1]=arr[a];
 arr[a]=ch;
 }
 }
 }
}

61.141.158.*

13楼

楼主有一点点错:
void QuickSort(int array[], int low, int high)
{
int i, j, temp;
i = low;
j = high;
temp = array[i];

while (i < j)
{
while (i<j && array[j]>temp) --j;
array[i] = array[j];
while (i<j && array[i]<=temp) ++i;
array[j] = array[i];

 array[i] = temp;

 QuickSort(array, low, i-1);
 QuickSort(array, i+1, high);
}
}

61.141.158.*

14楼

楼上可以实现排序,但不是快速排序法

void QuickSort(int array[], int low, int high)
{
int i, j, temp;
i = low;
j = high;
temp = array[i];

if (i >=j) return;

while (i < j)
{
while (i<j && array[j]>temp) --j;
array[i] = array[j];
while (i<j && array[i]<=temp) ++i;
array[j] = array[i];
}
 array[i] = temp;

QuickSort(array, low, i-1);
QuickSort(array, i+1, high);

}

219.236.31.*

15楼

楼主是对的

16楼

楼主是对的
218.58.249.*

17楼

13楼的你那只是个函数.没有调用根本,连MAIN函数都没有..楼主是对的,是快速排序...你只需要改动QUICKSORT后面两个参数就可以 实现任何单位字符的快速排序,,同时将其打印!!!!我程序编译过,SUCCESS...成功了!!挺容易理解的!!!你多看点书就行了,不要喊人家的程序垃圾!!! 程序做出来当然会稍微复杂,但相对数字的话就不算了,你要是排序1000个的话..你看是程序复杂还是 你自己复杂!!! 总之楼主的就是快速排序法..
221.237.177.*

18楼

建议各位看帖先别忙着回复,copy下来debug一下.稳当些.
别得了好处还骂楼主垃圾.

210.22.29.*

19楼

垃圾才会骂人
61.28.52.*

20楼

一群无聊的人.~!!!!!

21楼

楼主写的没有错,形象的说,快速排序就是一个把白菜按大小分成两堆的过程,选定一个‘标准大小的白菜’,i和j两人从两边一起挑,j挑到小的白菜就把它扔到i那头然后等,i挑到大的白菜就把它扔到j那头然后等,挑到i和j碰头了,把基准白菜放到碰头的地方。这样白菜就分成两堆。分别把这两堆按同样方法处理,最后得到一排按大小排列的白菜:)
 
哈,解释的好幼稚。


 
ch=arr[startPos]; //挑出一颗作为基准的白菜
i=startPos; //开始的白菜
j=endPos; //结束的白菜
while(i<j) //白菜没挑完

while(arr[j]>=ch && i<j)--j; //这棵比标准白菜大,跳过,直到遇到小的
arr[i]=arr[j]; //把这棵小的放到前面
while(arr[i]<=ch && i<j)++i; //跳过比标准小的,直到遇 到大白菜
arr[j]=arr[i]; //把大白菜扔到刚才遇见小白菜的地方 

etc.

210.185.254.*

22楼

不错不错~~快速排序终于算是搞懂了
58.44.131.*

23楼

For(i=0;i<10-1;i++)
{
k=i;
For(j=i+1;j<10;j++)
If(MyVar[j]>MyVar[i])
k=j;
If(k!=i)
{t=MyVar[i];MyVar[i]=MyVar[k];MyVar[k]=t};
}

222.95.174.*

24楼

23:你这是快速排序吗?不要误人。而且代码有误。
for(i=0;i<10-1;i++) 

 k=i; 
 for(j=i+1;j<10;j++) 
 if(MyVar[j]>MyVar[k/*不是i*/]) 
 k=j; 
 if(k!=i) 
 {t=MyVar[i];MyVar[i]=MyVar[k];MyVar[k]=t}; 
}

218.9.166.*

25楼

14楼正解,21楼讲的也对.
快速排序是很有效率也很常用的算法,复杂度在n*log2n到n*n之间.

218.75.144.*

26楼

快速排序的思想是什么?
222.246.52.*

27楼

哈哈 快速排序是这样吗?
222.39.27.*

28楼

什么玩音
222.39.27.*

29楼

dhg

30楼

写了个随即产生N个数,再排序,但是在使用快速排序法时出错。编译已经通过。
程序如下

#include<stdio.h> 
#include<stdlib.h> 

void retArray(int*,int); 
void insertsort (int x[],int n); 
void bubblesort(int x[],int n); 
void selectsort(int x[],int n ); 
void fastsort(int *x,int l,int r); 


int main() 

 int r[100]; 
 int n=0; 
 int i=0; 
 char choice; 
 printf("how many numbers do you want to sort?(numbers<=99)!\n"); 
 scanf("%d",&n); 
 retArray(r,n); 
 printf("OUT:\n"); 
 for(i=0;i<n;i++) 
 { 
 printf("%d\n",r[i]); 
 } 
 while(true) 
 { 
 printf("which kind do you want to sort numbers?insert,bubblesort,select,fast or quit?,I/B/S/F/Q\n"); 
 scanf("%c",&choice); 
 getchar(); 
 if(choice=='I'||choice=='i') 
 { 
 insertsort (r,n); 
 for(i=0;i<n;i++) 
 { 
 printf("%d\n",r[i]); 
 } 
 } 
 if(choice=='B'||choice=='b') 
 { 
 bubblesort(r,n);
 for(i=0;i<n;i++) 
 { 
 printf("%d\n",r[i]); 
 } 
 } 
 if(choice=='S'||choice=='s') 
 { 
 selectsort(r,n);
 for(i=0;i<n;i++) 
 { 
 printf("%d\n",r[i]); 
 } 
 } 
 
 if(choice=='F'||choice=='f') 
 { 
 fastsort(r,r[0],r[n-1]);
 for(i=0;i<n;i++) 
 { 
 printf("%d\n",r[i]); 
 } 
 } 
 
 if(choice=='Q'||choice=='q') 
 { 
 printf("you can safely close the program now!"); 
 getchar(); 
 return 0;
 break;
 } 
 } //while 
 
 }//main() 

void retArray(int *a,int n) 

 int i; 
 srand(100); 
 for(i=0;i<n;i++) 
 { 
 a[i]=rand(); 
 } 
 fflush(stdin); 
 return; 
} //OK!

void insertsort (int *x, int n) 

 int a; 
 int i,j; 
 for (i=0;i<n-1;i++) 
 { 
 a=x[i+1];
 j=i; 
 while(j>-1&& a<x[j]) 
 { 
 x[j+1]=x[j]; 
 j--; 
 } 
 x[j+1]=a; 
 } 
} //OK!
 
void bubblesort(int *x,int n) 

int i,j,flag;
int swap;
for(i=0;i<n-1;i++)
 {
 flag = 1;
 for(j=0;j<n-1;j++)
 if(x[j]>x[j+1])
 {
flag = 1;
swap = x[j];
x[j] = x[j+1];
x[j+1] = swap;
 }
 if(flag==0)return;
 } 
} //OK! 



void selectsort (int *x,int n)
{
 int i,j,small;
 int temp;
 for (i=1;i<n-1;i++)
 {
 small=i;
 for (j=i+1;j<n;j++)
 if (x[j]<x[small])
 { 
 small=j;
 }
 if (small!=i)
 {
 temp = x[i];
 x[i] = x[small];
 x[small] = temp;
 } 
 }
}//ok!

 
 
void fastsort(int *x,int l,int r) 

 int i,j;
 int temp;
 i=l;j=r;
 temp = x[l];
 while(i<j)
 {
 while ( i<j && temp<=x[j])
 j--;
 if(i<j)
 {
 x[i]=x[j];
 i++;
 }
 while (i<j && temp>=x[i])
 i++;
 if(i<j)
 {
 x[j]=x[i];
 j--;
 } 
 }
 x[i]=temp;
 if(l<i)fastsort(x,l,i-1);
 if(i<r)fastsort(x,j+1,r); 
}

发表回复

内 容:
用户名:
  
©2010 Baidu 贴吧协议  意见反馈