Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef int (*compfn)(const void*, const void*);
- struct numbers { int number;
- };
- struct numbers array[10] = { { 1 },
- { 5 },
- { 8 },
- { 4 },
- { 3 },
- { 9 },
- { 2 },
- { 6 },
- { 10 },
- { 7 } };
- void printarray(void);
- int compare(struct numbers *, struct numbers *);
- void main(void)
- {
- char sign;
- while(1)
- {
- printf("Lista przed sortowaniem:\n");
- printarray();
- qsort((void *) &array,
- 10,
- sizeof(struct numbers),
- (compfn)compare );
- printf("\nLista po sortowaniu:\n");
- printarray();
- sign=scanf("%c",&sign);
- if(sign=='z')
- {
- break;
- }
- }
- }
- int compare(struct numbers *elem1, struct numbers *elem2)
- {
- if ( elem1->number < elem2->number)
- return -1;
- else if (elem1->number > elem2->number)
- return 1;
- else
- return 0;
- }
- void printarray(void)
- {
- int i;
- for (i = 0; i < 10; i++)
- printf("%d: Liczba %d to \n",
- i+1, array[i].number);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement