A few sorting routines

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Narue Narue is offline Offline Oct 31st, 2004, 5:26 pm |
0
Insertion sort, Selection sort, Bubble sort, Shell sort, Quicksort, and Heapsot. All optimized and ready to be experimented with. This is the framework for a Java application that speed tests various sorting algorithms (because there's usually little need to write one's own in production programs). Several popular algorithms were left out because I didn't feel like implementing them. Included in this list are counting sorts, radix sorts, merge sorts, and the introsort. All are interesting, but I get bored quickly.
Quick reply to this message  
Java Syntax
  1. package scratchpad;
  2.  
  3. import java.math.*;
  4.  
  5. class SortInsertion {
  6. // Insertion sort
  7. public static <T extends Comparable>
  8. void insertion_sort ( T[] list, int size )
  9. {
  10. for ( int i = 1; i < size; i++ ) {
  11. T save = list[i];
  12. int j = i;
  13.  
  14. while ( j >= 1 && save.compareTo ( list[j - 1] ) < 0 ) {
  15. list[j] = list[j - 1];
  16. --j;
  17. }
  18.  
  19. list[j] = save;
  20. }
  21. }
  22.  
  23. // Shell sort
  24. public static <T extends Comparable>
  25. void shell_sort ( T[] list, int size )
  26. {
  27. int h = 1;
  28.  
  29. while ( h <= size / 9 ) {
  30. h = 3 * h + 1;
  31. }
  32.  
  33. while ( h > 0 ) {
  34. for ( int i = h; i < size; i++ ) {
  35. T save = list[i];
  36. int j = i;
  37.  
  38. while ( j >= h && save.compareTo ( list[j - h] ) < 0 ) {
  39. list[j] = list[j - h];
  40. j -= h;
  41. }
  42.  
  43. list[j] = save;
  44. }
  45.  
  46. h /= 3;
  47. }
  48. }
  49. }
  50.  
  51. class SortExchange {
  52. // Bubble sort
  53. public static <T extends Comparable>
  54. void bubble_sort ( T[] list, int size )
  55. {
  56. int bound = size - 1;
  57.  
  58. while ( bound > 0 ) {
  59. int new_bound = 0;
  60.  
  61. for ( int i = 0; i < bound; i++ ) {
  62. if ( list[i].compareTo ( list[i + 1] ) > 0 ) {
  63. T save = list[i];
  64. list[i] = list[i + 1];
  65. list[i + 1] = save;
  66.  
  67. new_bound = i;
  68. }
  69. }
  70.  
  71. bound = new_bound;
  72. }
  73. }
  74.  
  75. // Quicksort
  76. private static <T extends Comparable>
  77. void quicksort ( T[] list, int l, int r )
  78. {
  79. int[] stack = new int[50];
  80. int m, top = 0;
  81. T pivot;
  82.  
  83. while ( true ) {
  84. while ( r > l + 10 ) {
  85. // Median of three partition
  86. m = ( l + r ) / 2;
  87.  
  88. if ( list[l].compareTo ( list[m] ) > 0 ) {
  89. T save = list[l]; list[l] = list[m]; list[m] = save;
  90. }
  91. if ( list[l].compareTo ( list[r] ) > 0 ) {
  92. T save = list[l]; list[l] = list[r]; list[r] = save;
  93. }
  94. if ( list[m].compareTo ( list[r] ) > 0 ) {
  95. T save = list[m]; list[m] = list[r]; list[r] = save;
  96. }
  97.  
  98. if ( r - l <= 2 ) {
  99. break;
  100. }
  101.  
  102. pivot = list[m];
  103. list[m] = list[r - 1];
  104. list[r - 1] = pivot;
  105.  
  106. int i = l;
  107. int j = r - 1;
  108.  
  109. while ( true ) {
  110. while ( list[++i].compareTo ( pivot ) < 0 ) {
  111. // No body
  112. }
  113. while ( list[--j].compareTo ( pivot ) > 0 ) {
  114. // No body
  115. }
  116.  
  117. if ( j < i ) {
  118. break;
  119. }
  120.  
  121. T save = list[i];
  122. list[i] = list[j];
  123. list[j] = save;
  124. }
  125.  
  126. pivot = list[i];
  127. list[i] = list[r - 1];
  128. list[r - 1] = pivot;
  129.  
  130. // Simulated recursive calls
  131. if ( j - l > r - i ) {
  132. stack[top++] = l;
  133. stack[top++] = j;
  134. l = i + 1;
  135. }
  136. else {
  137. stack[top++] = i + 1;
  138. stack[top++] = r;
  139. r = j;
  140. }
  141. }
  142.  
  143. if ( top == 0 ) {
  144. break;
  145. }
  146.  
  147. r = stack[--top];
  148. l = stack[--top];
  149. }
  150. }
  151.  
  152. public static <T extends Comparable>
  153. void quicksort ( T[] list, int size )
  154. {
  155. quicksort ( list, 0, size - 1 );
  156. SortInsertion.insertion_sort ( list, size );
  157. }
  158. }
  159.  
  160. class SortSelection {
  161. // Selection sort
  162. public static <T extends Comparable>
  163. void selection_sort ( T[] list, int size )
  164. {
  165. for ( int i = 0; i < size - 1; i++ ) {
  166. int min = i;
  167.  
  168. for ( int j = i + 1; j < size; j++ ) {
  169. if ( list[j].compareTo ( list[min] ) < 0 ) {
  170. min = j;
  171. }
  172. }
  173.  
  174. if ( min != i ) {
  175. T save = list[i];
  176. list[i] = list[min];
  177. list[min] = save;
  178. }
  179. }
  180. }
  181.  
  182. // Heapsort
  183. private static <T extends Comparable>
  184. void push_down ( T[] list, int l, int r )
  185. {
  186. int i, j;
  187.  
  188. for ( i = l; ( j = i * 2 + 1 ) <= r; i = j ) {
  189. if ( j + 1 <= r && list[j + 1].compareTo ( list[j] ) > 0 ) {
  190. ++j;
  191. }
  192.  
  193. T save = list[i];
  194. list[i] = list[j];
  195. list[j] = save;
  196. }
  197.  
  198. while ( true ) {
  199. j = ( i - 1 ) / 2;
  200.  
  201. if ( j < l || j == i || list[j].compareTo ( list[i] ) > 0 ) {
  202. break;
  203. }
  204.  
  205. T save = list[i];
  206. list[i] = list[j];
  207. list[j] = save;
  208.  
  209. i = j;
  210. }
  211. }
  212.  
  213. public static <T extends Comparable>
  214. void heapsort ( T[] list, int size )
  215. {
  216. if ( size-- < 2 ) {
  217. return;
  218. }
  219.  
  220. int i;
  221.  
  222. for ( i = ( size - 1 ) / 2; i >= 0; i-- ) {
  223. push_down ( list, i, size );
  224. }
  225.  
  226. while ( size > 0 ) {
  227. T save = list[size];
  228. list[size] = list[0];
  229. list[0] = save;
  230.  
  231. push_down ( list, 0, --size );
  232. }
  233. }
  234. }
  235.  
  236. public class Main {
  237. public static void main ( String[] args )
  238. {
  239. Integer[] a = new Integer[20];
  240.  
  241. for ( int i = 0; i < 20; i++ ) {
  242. a[i] = (int)( Math.random() * 100 );
  243. }
  244.  
  245. System.out.println ( "Before: " );
  246. for ( int i : a ) {
  247. System.out.print ( i + " " );
  248. }
  249. System.out.println ( "" );
  250.  
  251. // Insert your sorting method of choice here
  252.  
  253. System.out.println ( "After: " );
  254. for ( int i : a ) {
  255. System.out.print ( i + " " );
  256. }
  257. System.out.println ( "" );
  258. }
  259. }
0
cscgal cscgal is offline Offline | Oct 31st, 2004
Thank you very much for this!
 
0
majestic0110 majestic0110 is offline Offline | Apr 2nd, 2008
Very nice. Much better (and cleaner) than my version!
 
 

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC