fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct book
  5. {
  6. char title[15];
  7. char author[15];
  8. int price;
  9. int pages;
  10. };
  11.  
  12. void quicksort(struct book* mas, int first, int last)
  13. {
  14. int f = first, l = last;
  15. int mid = mas[(f + l) / 2].price; //вычисление опорного элемента
  16. do
  17. {
  18. while (mas[f].price < mid) f++;
  19. while (mas[l].price > mid) l--;
  20. if (f <= l) //перестановка элементов
  21. {
  22. struct book count = mas[f];
  23. mas[f] = mas[l];
  24. mas[l] = count;
  25. f++;
  26. l--;
  27. }
  28. } while (f < l);
  29. if (first < l) quicksort(mas, first, l);
  30. if (f < last) quicksort(mas, f, last);
  31. }
  32.  
  33. int main()
  34. {
  35. struct book libry[5] = {
  36. { "Title 1", "Author1", 100, 200 },
  37. { "Title 2", "Author2", 900, 300 },
  38. { "Title 3", "Author3", 200, 400 },
  39. { "Title 4", "Author4", 800, 500 },
  40. { "Title 5", "Author5", 500, 600 }
  41. };
  42.  
  43. quicksort(libry,0,4);
  44.  
  45. for(int i = 0; i < 5; i++)
  46. {
  47. printf("\n %d. %s ", i + 1, libry[i].author);
  48. printf("%s %d %d", libry[i].title, libry[i].price, libry[i].pages);
  49. }
  50. }
  51.  
Success #stdin #stdout 0.01s 5432KB
stdin
Standard input is empty
stdout
 1. Author1 Title 1 100 200
 2. Author3 Title 3 200 400
 3. Author5 Title 5 500 600
 4. Author4 Title 4 800 500
 5. Author2 Title 2 900 300