Quicksort

From Ta Wiki
Revision as of 00:00, 8 March 2019 by Tata (talk | contribs) (Created page with "<pre> #include<stdio.h> int n,seq[100000]; void swap(int x,int y){ int temp=seq[x]; seq[x]=seq[y]; seq[y]=temp; } void quick(int lb,int ub){ if(lb>=ub) retur...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
#include<stdio.h>
int n,seq[100000];

void swap(int x,int y){
    int temp=seq[x];
    seq[x]=seq[y];
    seq[y]=temp;
}

void quick(int lb,int ub){
    if(lb>=ub) return;
    int piv=seq[lb],back=lb,i;
    for(i=lb+1;i<=ub;i++){
        if(seq[i]<piv){
            back++;
            swap(back,i);
        }
    }
    swap(back,lb);
    quick(lb,back-1);
    quick(back+1,ub);
}

int main(){
    printf("Enter numbers of input : ");
    scanf("%d",&n);
    int i;
    printf("Enter %d numbers : ",n);
    for(i=0;i<n;i++){
        scanf("%d",&seq[i]);
    }

    quick(0,n-1);

    printf("\nSorted sequence is : ");
    for(i=0;i<n;i++){
        printf("%d ",seq[i]);
    }
    
    scanf(" ");
    return 0;
}