Friday, January 13, 2012

Struct sort with Algorithm sort/builtin quick sort

#include<iostream>
#include<algorithm>


using namespace std;


typedef struct
{
    int a,b;
}st;


st ar[100];


bool comp(st x,st y)
{
    if(x.a > y.a)
        return false;
    if(x.b < y.b)
        return false;
    return true;
}


int main()
{
    int i,test;
    while(scanf("%d",&test)==1)
    {
        for(i=0;i<test;i++)
        {
            scanf("%d%d",&ar[i].a,&ar[i].b);
        }
        sort(ar,ar+test,comp);


        printf("saiful\n");
        for(i=0;i<test;i++)
        {
            printf("%d %d\n",ar[i].a,ar[i].b);
        }
    }
    return 0;
}


#################################################################



#include<stdio.h>
#include<string.h>


#include<stdlib.h>


typedef struct{


char c[100];
}word;
word words[20];


int comp(void const *A, void const *B)
{
word *a = (word*)A;
word *b = (word*)B;
return (strcmp(a->c,b->c));
}


int main()
{
int n,i;
while( scanf("%d",&n)==1)
    {
for(i=0;i<n;i++)
            scanf (" %s",words[i].c);


qsort(words,n,sizeof(word),comp);


for(i=0;i<n;i++)
            printf("%s\n",words[i].c);
}
return 0;
}



No comments:

Post a Comment