qsort.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. /*-
  27. * Copyright (c) 1980, 1983, 1990 The Regents of the University of California.
  28. * All rights reserved.
  29. *
  30. * Redistribution and use in source and binary forms, with or without
  31. * modification, are permitted provided that the following conditions
  32. * are met:
  33. * 1. Redistributions of source code must retain the above copyright
  34. * notice, this list of conditions and the following disclaimer.
  35. * 2. Redistributions in binary form must reproduce the above copyright
  36. * notice, this list of conditions and the following disclaimer in the
  37. * documentation and/or other materials provided with the distribution.
  38. * 3. All advertising materials mentioning features or use of this software
  39. * must display the following acknowledgement:
  40. * This product includes software developed by the University of
  41. * California, Berkeley and its contributors.
  42. * 4. Neither the name of the University nor the names of its contributors
  43. * may be used to endorse or promote products derived from this software
  44. * without specific prior written permission.
  45. *
  46. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  47. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  48. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  49. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  50. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  51. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  52. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  53. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  54. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  55. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  56. * SUCH DAMAGE.
  57. */
  58. #if defined(LIBC_SCCS) && !defined(lint)
  59. static char sccsid[] = "@(#)qsort.c 5.9 (Berkeley) 2/23/91";
  60. #endif /* LIBC_SCCS and not lint */
  61. #include <stdlib.h>
  62. #include <sys/types.h>
  63. static void insertion_sort(void* bot, size_t nmemb, size_t size, int (*compar)(const void*, const void*));
  64. static void insertion_sort_r(void* bot, size_t nmemb, size_t size, int (*compar)(const void*, const void*, void*), void* arg);
  65. void qsort(void* bot, size_t nmemb, size_t size, int (*compar)(const void*, const void*))
  66. {
  67. if (nmemb <= 1)
  68. return;
  69. insertion_sort(bot, nmemb, size, compar);
  70. }
  71. void qsort_r(void* bot, size_t nmemb, size_t size, int (*compar)(const void*, const void*, void*), void* arg)
  72. {
  73. if (nmemb <= 1)
  74. return;
  75. insertion_sort_r(bot, nmemb, size, compar, arg);
  76. }
  77. void insertion_sort(void* bot, size_t nmemb, size_t size, int (*compar)(const void*, const void*))
  78. {
  79. int cnt;
  80. unsigned char ch;
  81. char *s1, *s2, *t1, *t2, *top;
  82. top = (char*)bot + nmemb * size;
  83. for (t1 = (char*)bot + size; t1 < top;) {
  84. for (t2 = t1; (t2 -= size) >= bot && compar(t1, t2) < 0;)
  85. ;
  86. if (t1 != (t2 += size)) {
  87. for (cnt = size; cnt--; ++t1) {
  88. ch = *t1;
  89. for (s1 = s2 = t1; (s2 -= size) >= t2; s1 = s2)
  90. *s1 = *s2;
  91. *s1 = ch;
  92. }
  93. } else
  94. t1 += size;
  95. }
  96. }
  97. void insertion_sort_r(void* bot, size_t nmemb, size_t size, int (*compar)(const void*, const void*, void*), void* arg)
  98. {
  99. int cnt;
  100. unsigned char ch;
  101. char *s1, *s2, *t1, *t2, *top;
  102. top = (char*)bot + nmemb * size;
  103. for (t1 = (char*)bot + size; t1 < top;) {
  104. for (t2 = t1; (t2 -= size) >= bot && compar(t1, t2, arg) < 0;)
  105. ;
  106. if (t1 != (t2 += size)) {
  107. for (cnt = size; cnt--; ++t1) {
  108. ch = *t1;
  109. for (s1 = s2 = t1; (s2 -= size) >= t2; s1 = s2)
  110. *s1 = *s2;
  111. *s1 = ch;
  112. }
  113. } else
  114. t1 += size;
  115. }
  116. }