| 
									
										
										
										
											2008-06-13 02:51:46 -04:00
										 |  |  | USING: locals sequences kernel math ;
 | 
					
						
							|  |  |  | IN: sorting.insertion | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <PRIVATE
 | 
					
						
							| 
									
										
										
										
											2008-07-18 20:22:59 -04:00
										 |  |  | :: insert ( seq quot: ( elt -- elt' ) n -- )
 | 
					
						
							| 
									
										
										
										
											2008-06-13 02:51:46 -04:00
										 |  |  |     n zero? [ | 
					
						
							| 
									
										
										
										
											2009-08-13 20:21:44 -04:00
										 |  |  |         n n 1 - [ seq nth quot call ] bi@ >= [ | 
					
						
							|  |  |  |             n n 1 - seq exchange
 | 
					
						
							|  |  |  |             seq quot n 1 - insert | 
					
						
							| 
									
										
										
										
											2008-06-13 02:51:46 -04:00
										 |  |  |         ] unless
 | 
					
						
							| 
									
										
										
										
											2008-07-18 20:22:59 -04:00
										 |  |  |     ] unless ; inline recursive
 | 
					
						
							| 
									
										
										
										
											2008-06-13 02:51:46 -04:00
										 |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : insertion-sort ( seq quot -- )
 | 
					
						
							|  |  |  |     ! quot is a transformation on elements | 
					
						
							|  |  |  |     over length [ insert ] with with each ; inline
 |