Advertisement
STANAANDREY

sda exam pb2

Jan 4th, 2024
856
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.08 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define NAME_MAX 25
  6. #define TEL_MAX 10
  7. typedef struct {
  8.     char name[NAME_MAX], tel[TEL_MAX];
  9.     int year, income;
  10. } Person;
  11.  
  12. void swap(Person *a, Person *b) {
  13.     Person aux = *a;
  14.     *a = *b;
  15.     *b = aux;
  16. }
  17.  
  18. void QuickSort(Person v[], int l, int r)
  19. {
  20.     if (l < r)
  21.     {
  22.         int mid = (l + r) / 2;
  23.         swap(&v[l], &v[mid]);
  24.         int i = l, j = r, d = 0;
  25.         while (i < j)
  26.         {
  27.             if (strcmp(v[i].name, v[j].name) > 0 && v[i].year >= 30 && v[j].year >= 30)
  28.             {
  29.                 swap(&v[i], &v[j]);
  30.                 d = 1 - d;
  31.             }
  32.             i += d;
  33.             j -= 1 - d;
  34.         }
  35.         QuickSort(v, l, i - 1);
  36.         QuickSort(v, i + 1, r);
  37.     }
  38. }
  39.  
  40.  
  41. void displayPerson(const Person *const p) {
  42.     printf("{%s %s %d %d}\n", p->name, p->tel, p->year, p->income);
  43. }
  44.  
  45. int main(void) {
  46.     Person p[] = {
  47.         {"ion43", "089743", 31, 2},
  48.         {"ion2", "089743", 1, 2},
  49.         {"john1", "089743", 31, 2},
  50.         {"ion4", "089743", 31, 2},
  51.         {"ion0", "089743", 29, 2},
  52.         {"a", "089744533", 38, 2},
  53.     };
  54.     QuickSort(p, 0, 5);
  55.     for (int i = 0; i < 6; i++) {
  56.         displayPerson(&p[i]);
  57.     }
  58.     return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement