Can you guys help me? about Quick Sort Algorithm

Reply

Join Date: Jul 2004
Posts: 6
Reputation: vienne is an unknown quantity at this point 
Solved Threads: 0
vienne vienne is offline Offline
Newbie Poster

Can you guys help me? about Quick Sort Algorithm

 
0
  #1
Oct 1st, 2004
I'm having a problem with my sorting algorithm.
before I made quick sort which used insertion sort, but I don't want to use this insertion sort. I want to use recursive algorithm.

Also, I want to get new idea, so If you know any quick sort algorithm, reply for me.Please~
:rolleyes:
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,596
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 712
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Can you guys help me? about Quick Sort Algorithm

 
0
  #2
Oct 1st, 2004
Originally Posted by vienne
I'm having a problem with my sorting algorithm.
before I made quick sort which used insertion sort, but I don't want to use this insertion sort. I want to use recursive algorithm.

Also, I want to get new idea, so If you know any quick sort algorithm, reply for me.Please~
:rolleyes:
Good quicksort implementations use insertion sort as the final step. Or do you mean you wrote an insertion sort algorithm and called it quicksort? :rolleyes: The basic recursive algorithm is simple:
  1. void quicksort ( type a, int l, int r )
  2. {
  3. int i;
  4.  
  5. if ( r <= l )
  6. return;
  7.  
  8. i = partition ( a, l, r );
  9.  
  10. quicksort ( a, l, i - 1 );
  11. quicksort ( a, i + 1, r );
  12. }
Because your post was appropriately vague, I'll do the same and not include the implementation of partition.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 6
Reputation: vienne is an unknown quantity at this point 
Solved Threads: 0
vienne vienne is offline Offline
Newbie Poster

Re: Can you guys help me? about Quick Sort Algorithm

 
0
  #3
Oct 1st, 2004
hum..... I know that basic recursive algorithm, but i cannot make good quick sort algorithm.

this is my code. I'm having a problem with this code.
there are three pointer( pivot, left, right)

  1. #include <stdio.h>
  2.  
  3.  
  4. void spilt(int tab[],int,int*);
  5. void quick(int tab[],int,int);
  6. void swap(int*,int*);
  7. void tab_prn(int tab[],int,char*);
  8. main()
  9. {
  10. int tab[11]= {0,26,5,37,1,61,11,59,15,48,19};
  11. tab_prn(tab,10,"source");
  12. quick(tab,1,10);
  13. tab_prn(tab,10,"result");
  14. }
  15.  
  16. void quick(tab,p,q)
  17. int tab[11],p,q;
  18. {
  19. int j=q+1;
  20. if(p<=q) return;
  21. spilt(tab,p,&j);
  22. tab_prn(tab,10," step");
  23. quick(tab,p,j-1);
  24. quick(tab,j+1,q);
  25.  
  26. }
  27.  
  28. void spilt(tab,m,up);
  29. int tab[],m,*up;
  30. {
  31. int low=m,b=tab[m];
  32. while(1)
  33. {
  34. do low++; while(tab[low]
  35. do --*up; while(tab[*up] >v);
  36. if(low<*up) swap(tab+low,tab+*up);
  37. else break;
  38. }
  39. tab[m]=tab[*up];
  40. tab[*up]=v;
  41. }
  42.  
  43. void swap(i,j)
  44. int *i,*j;
  45. {
  46. int t;
  47. t=*i;
  48. *i=*j;
  49. *j=t;
  50. }
  51. void tab_prn(tab,n,title)
  52. int tab[11],n;
  53. char *title;
  54. {
  55. int i;
  56. printf("%7s",title);
  57. for(i=2;i<=n;i++)
  58. printf("%3d",tab[i]);
  59. printf("\n");
  60. }

Base on the pivot, I have to sort left side and right side.
The Left side is smaller than the pivot, and the right side is lagger than the pivot. I guess that you guys have more ease way.
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,036
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 130
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Re: Can you guys help me? about Quick Sort Algorithm

 
0
  #4
Oct 1st, 2004
This program that I wrote should be of help to you: http://www.daniweb.com/techtalkforums/thread1689.html
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/daniweb
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,596
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 712
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Can you guys help me? about Quick Sort Algorithm

 
0
  #5
Oct 2nd, 2004
>but i cannot make good quick sort algorithm
There are three immediate improvements to the basic algorithm. First, you want to come up with a partitioning scheme that finds a pivot point as close to the median as possible so that the recursive branching is balanced. Second, you want to avoid recursing for small sets but adding a cutoff when the partitions are small. Lastly, you can partition three or more ways and then work out an elegant handling of duplicate values.

The first improvement is usually made by either choosing a random pivot as cscgal did, or by choosing three values in the partition and using the median of those three as the pivot. You could use more than three, but that means extra work and extra time, just like calling a random number generator. Both are undesirable in an efficient sorting routine. The second improvement is usually made by setting a cutoff in the recursive path and then using insertion sort to finalize the routine. Because insertion sort is zippy on almost sorted files, this increases quicksort's speed.

The last improvement is complicated and I don't see it much, so unless large amounts of duplicate values are a problem for you, you can ignore it.

The most obvious problem with your code aside from the syntax errors in spilt is that you mix prototypes with old style function definitions. This is a huge no-no. Let K&R C die and use proper ISO C function definitions please. For help on the actual algorithm, start by throwing away that awful book you're using. The code rarely works without a great deal of tweaking. :rolleyes: The text is wonderful, but the code just sucks.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 6
Reputation: vienne is an unknown quantity at this point 
Solved Threads: 0
vienne vienne is offline Offline
Newbie Poster

Re: Can you guys help me? about Quick Sort Algorithm

 
0
  #6
Oct 5th, 2004
Originally Posted by cscgal
This program that I wrote should be of help to you: http://www.daniweb.com/techtalkforums/thread1689.html

Thank you Nanue and cscgal! I got idea! and this is the perfect program~
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC