Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- void quick(int *A,int left,int right,int k);
- void print(int *A);
- void swap(int *A,int x,int y);
- int indexPivot,n,tmp;
- int main()
- {
- int i,A[50],k;
- printf("nnn Size??tt");
- scanf("%d",&n);
- for(i=0;i<n;i++){
- printf(" %d. eleMent...t",i+1);
- scanf("%d",&A[i]);
- }
- printf("n 1 or 2 ");//another method but 1 is important now,not 2
- scanf("%d",&k);
- printf("n First appearance ");
- print(A);
- quick(A,0,n-1,k);
- printf(" nMedium element: %dn ",A[(n)/2]);
- print(A);
- }
- //always putting first to pivot
- void quick(int *A,int left,int right,int k){//k is for another method,not used here
- int pivot,i,j;
- if(k==1){
- indexPivot=left;
- pivot=A[indexPivot];
- }
- /*else{
- if(A[left]<A[right]){
- if(A[(right+left)/2]>A[right]){
- indexPivot=right;
- }
- else{
- indexPivot=(right+left)/2;
- }
- }
- else{
- if(A[(right+left)/2]>A[left]){
- indexPivot=left;
- }
- else{
- indexPivot=(right+left)/2;
- }
- }
- }
- printf("n Pivot: %d",A[indexPivot]);*/
- while(indexPivot!=(right+left)/2&&left<right){
- j=right;
- i=left+1;print(A);
- while(i<j){
- while(pivot<A[j]&&left<j){
- j=j-1;
- print(A);
- }
- while(pivot>A[i]&&i<j){
- i=i+1;
- print(A);
- }
- if(i<j){
- swap(A,i,j);
- print(A);
- i=i+1;
- j=j-1;
- print(A);
- }
- print(A);
- }
- //while(A[s]>pivot){i=i-1;}
- swap(A,indexPivot,j);
- indexPivot=j;//pivot goes its place
- print(A);
- if(indexPivot>(right+left)/2){
- quick(A,left,indexPivot-1,k);
- print(A);
- }
- if(indexPivot<(right+left)/2){
- quick(A,indexPivot+1,right,k);
- print(A);
- }
- }
- }
- void print(int *A){
- int i;
- printf("n ");
- for(i=0;i<n;i++){
- printf("%d ",A[i]);
- }
- }
- void swap(int *A,int x,int y){
- int tmp;
- tmp=A[x];
- A[x]=A[y];
- A[y]=tmp;
- }
- if(indexPivot>(right+left)/2){
- quick(A,left,indexPivot-1,k);
- print(A);
- }
- if(indexPivot<(right+left)/2){
- quick(A,indexPivot+1,right,k);
- print(A);
- }
- while(pivot<A[j]&&left<j)
- while(pivot<A[j]&&i<j)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement