Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void quick(int *A,int left,int right,int k);
  5. void print(int *A);
  6. void swap(int *A,int x,int y);
  7.  
  8. int indexPivot,n,tmp;
  9.  
  10.  
  11. int main()
  12. {
  13. int i,A[50],k;
  14.  
  15. printf("nnn Size??tt");
  16. scanf("%d",&n);
  17.  
  18.  
  19.  
  20. for(i=0;i<n;i++){
  21. printf(" %d. eleMent...t",i+1);
  22. scanf("%d",&A[i]);
  23. }
  24.  
  25. printf("n 1 or 2 ");//another method but 1 is important now,not 2
  26. scanf("%d",&k);
  27.  
  28. printf("n First appearance ");
  29.  
  30.  
  31. print(A);
  32.  
  33. quick(A,0,n-1,k);
  34. printf(" nMedium element: %dn ",A[(n)/2]);
  35.  
  36. print(A);
  37.  
  38. }
  39.  
  40.  
  41.  
  42. //always putting first to pivot
  43.  
  44. void quick(int *A,int left,int right,int k){//k is for another method,not used here
  45. int pivot,i,j;
  46. if(k==1){
  47. indexPivot=left;
  48. pivot=A[indexPivot];
  49. }
  50. /*else{
  51. if(A[left]<A[right]){
  52. if(A[(right+left)/2]>A[right]){
  53.  
  54. indexPivot=right;
  55. }
  56. else{
  57.  
  58. indexPivot=(right+left)/2;
  59. }
  60. }
  61. else{
  62. if(A[(right+left)/2]>A[left]){
  63.  
  64. indexPivot=left;
  65. }
  66. else{
  67. indexPivot=(right+left)/2;
  68. }
  69. }
  70. }
  71. printf("n Pivot: %d",A[indexPivot]);*/
  72.  
  73. while(indexPivot!=(right+left)/2&&left<right){
  74. j=right;
  75. i=left+1;print(A);
  76. while(i<j){
  77. while(pivot<A[j]&&left<j){
  78. j=j-1;
  79. print(A);
  80. }
  81. while(pivot>A[i]&&i<j){
  82. i=i+1;
  83. print(A);
  84. }
  85.  
  86. if(i<j){
  87. swap(A,i,j);
  88. print(A);
  89. i=i+1;
  90. j=j-1;
  91. print(A);
  92. }
  93.  
  94. print(A);
  95. }
  96. //while(A[s]>pivot){i=i-1;}
  97.  
  98. swap(A,indexPivot,j);
  99. indexPivot=j;//pivot goes its place
  100.  
  101.  
  102. print(A);
  103.  
  104. if(indexPivot>(right+left)/2){
  105. quick(A,left,indexPivot-1,k);
  106. print(A);
  107.  
  108. }
  109. if(indexPivot<(right+left)/2){
  110. quick(A,indexPivot+1,right,k);
  111. print(A);
  112.  
  113. }
  114.  
  115. }
  116.  
  117. }
  118.  
  119.  
  120.  
  121.  
  122.  
  123. void print(int *A){
  124. int i;
  125. printf("n ");
  126. for(i=0;i<n;i++){
  127. printf("%d ",A[i]);
  128. }
  129. }
  130.  
  131.  
  132.  
  133.  
  134.  
  135. void swap(int *A,int x,int y){
  136. int tmp;
  137. tmp=A[x];
  138. A[x]=A[y];
  139. A[y]=tmp;
  140. }
  141.  
  142. if(indexPivot>(right+left)/2){
  143. quick(A,left,indexPivot-1,k);
  144. print(A);
  145.  
  146. }
  147. if(indexPivot<(right+left)/2){
  148. quick(A,indexPivot+1,right,k);
  149. print(A);
  150.  
  151. }
  152.  
  153. while(pivot<A[j]&&left<j)
  154.  
  155. while(pivot<A[j]&&i<j)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement