
    ch                         d Z ddlT ddlZ G d de          Z G d de          Zd Zd	 Zd
 Z	d Z
d ZddZd Zd Zd Zd Zd Zd Zd ZdZdZedk    r e            Z e             dS dS )a  

         sorting_animation.py

A minimal sorting algorithm animation:
Sorts a shelf of 10 blocks using insertion
sort, selection sort and quicksort.

Shelfs are implemented using builtin lists.

Blocks are turtles with shape "square", but
stretched to rectangles by shapesize()
 ---------------------------------------
       To exit press space button
 ---------------------------------------
    )*Nc                   &    e Zd Zd Zd Zd Zd ZdS )Blockc                     || _         t                              | dd           |                                  |                     |dz  dd           |                     d           |                                  d S )NsquareF)shapevisibleg      ?   black)sizeTurtle__init__pu	shapesize	fillcolorst)selfr   s     ,  /home/conda/feedstock_root/build_artifacts/python-split_1761172146731/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehol/lib/python3.11/turtledemo/sorting_animate.pyr   zBlock.__init__   sk    	He<<<			tcz3***w					    c                 0    |                      d           d S )Nredr   r   s    r   glowz
Block.glow    s    ur   c                 0    |                      d           d S )Nr   r   r   s    r   unglowzBlock.unglow#   s    wr   c                 6    d                     | j                  S )NzBlock size: {0})formatr   r   s    r   __repr__zBlock.__repr__&   s     ''	222r   N)__name__
__module____qualname__r   r   r   r    r   r   r   r      sP                 3 3 3 3 3r   r   c                   2    e Zd Zd Zd Zd Zd Zd Zd ZdS )Shelfc                 "    || _         d| _        dS )z.create a shelf. y is y-position of first blockijN)yx)r   r'   s     r   r   zShelf.__init__,   s    r   c                    |                                 \  }}}|dz  dz  }|                    | j        |z              |                    | j        dt          |           z  z              |                     |           d S )Nr
      "   )r   setyr'   setxr(   lenappend)r   dwidth_y_offsets        r   pushz
Shelf.push1   sq    kkmmq!19r>	tv !!!	tvSYY&'''Ar   c                 ~    | |d          D ]1}|                                 \  }}|                    |dz
             2d S Nr+   posr-   r   ibxposr2   s        r   _close_gap_from_izShelf._close_gap_from_i9   M    abb 	 	AeeggGD!FF4"9	 	r   c                 ~    | |d          D ]1}|                                 \  }}|                    |dz              2d S r6   r7   r9   s        r   _open_gap_from_izShelf._open_gap_from_i>   r>   r   c                     t                               | |          }|                                 |                    d           |                     |           |S )N   )listpopr   r,   r=   )r   keyr;   s      r   rD   z	Shelf.popC   sJ    HHT3		ss###r   c                 J   |                      |           t                              | ||           |                    | j        d|z  z              |                                \  }}}|dz  dz  }|                    | j        |z              |                                 d S )Nr+   r
   r*   )	r@   rC   insertr-   r(   r   r,   r'   r   )r   rE   r;   r1   r2   r3   s         r   rG   zShelf.insertJ   s    c"""D#q!!!	tvS !!!kkmmq!19r>	tv !!!	




r   N)	r    r!   r"   r   r4   r=   r@   rD   rG   r#   r   r   r%   r%   *   sn          
    
  
      r   r%   c                 >   t          |           }t          d|          D ]|}|}|dk    rI| |         j        | |dz
           j        k     r*|dz
  }|dk    r| |         j        | |dz
           j        k     *|                     ||                     |                     }d S )N   r   r.   ranger   rG   rD   )shelflengthr:   holes       r   isortrO   T   s    ZZF1f ) )Qhh58=5?+???!8D Qhh58=5?+???T599Q<<((((
Fr   c                    t          |           }t          d|dz
            D ]g}|}t          |dz   |          D ] }| |         j        | |         j        k     r|}!||k    r)|                     ||                     |                     hd S )Nr   rI   rJ   )rL   rM   jiminr:   s        r   ssortrS   ]   s    ZZF1fqj!! - -q1uf%% 	 	AQx}uT{///199LLEIIdOO,,,- -r   c                 l   | |         }|                      ||                     |                     |}t          ||          D ]F}| |         j        |j        k     r.|                      ||                     |                     |dz   }G|                      ||                     |                     |S NrI   )rG   rD   rK   r   )rL   leftrightpivot_indexpivotstore_indexr:   s          r   	partitionr[   g   s    +E	LL		+..///K4 * *8=5:%%LLeiill333%/K	LLeii..///r   c                     ||k     r>|}t          | |||          }t          | ||dz
             t          | |dz   |           d S d S rU   )r[   qsort)rL   rV   rW   rX   pivot_new_indexs        r   r]   r]   r   s^    e||#E4DDeT?Q.///e_q(%00000	 |r   c                     t                       t                       t          t          d                    } t	          j        |            t          |           D ]v\  }}t          |t          t                              D ]N}t          |         j	        |dz   k    r3t          
                    |t                              |                     Owt          t                     t          t          d           t                       d S )N
   rI   line)disable_keysclearrC   rK   randomshuffle	enumerater.   sr   rG   rD   	show_textinstructions1instructions2enable_keys)targetr:   trQ   s       r   	randomizero   y   s    NNN	GGG%))__F
N6&!! & &1q#a&&!! 	& 	&AtyAE!!AEE!HH%%%	& mm!$$$$MMMMMr   c                 Z    d|z  }t          dd|z
             t          | dd           d S )Nr*   r   icenter)Courier   bold)alignfont)gotowrite)textrb   s     r   ri   ri      s;    9D4$;	$h%<======r   c                     t                       t                       t          d           t          t                     t                       t          t
                     t          t          d           t                       d S )NzSelection SortrI   ra   )rc   rd   ri   rS   rh   rj   rk   rl   r#   r   r   start_ssortr{      e    NNN	GGG	!HHH	GGGmm!$$$$MMMMMr   c                     t                       t                       t          d           t          t                     t                       t          t
                     t          t          d           t                       d S )NzInsertion SortrI   ra   )rc   rd   ri   rO   rh   rj   rk   rl   r#   r   r   start_isortr~      r|   r   c                  >   t                       t                       t          d           t          t          dt          t                    dz
             t                       t          t                     t          t          d           t                       d S )N	Quicksortr   rI   ra   )	rc   rd   ri   r]   rh   r.   rj   rk   rl   r#   r   r   start_qsortr      su    NNN	GGGk	!QA
	GGGmm!$$$$MMMMMr   c                      t          d          ad} | D ])}t                              t          |                     *d S )Ni8)
   r
      	   rI      r`            )r%   rh   r4   r   )valsr:   s     r   
init_shelfr      sF    dA*D  	uQxx r   c                      t          d d           t          d d           t          d d           t          d d           d S )Nrh   r:   qr)onkeyr#   r   r   rc   rc      sH    	$	$	$	$r   c                      t          t          d           t          t          d           t          t          d           t          t          d           t          t
          d           d S )Nr:   rh   r   r   space)r   r~   r{   r   ro   byer#   r   r   rl   rl      sX    	+s	+s	+s	)S	#wr   c                  &   t                                                       t                       t                       t	                       t          t                     t          t          d           t                       t                       dS )NrI   ra   	EVENTLOOP)
	getscreenclearscreenhtpenupr   ri   rj   rk   rl   listenr#   r   r   mainr      sj    KKDDD%'''LLLmm!$$$$MMM
HHH;r   zApress i for insertion sort, s for selection sort, q for quicksortz spacebar to quit, r to randomize__main__)r   )__doc__turtlere   r   r   rC   r%   rO   rS   r[   r]   ro   ri   r{   r~   r   r   rc   rl   r   rj   rk   r    msgmainloopr#   r   r   <module>r      s}         3 3 3 3 3F 3 3 3(( ( ( ( (D ( ( (T  - - -	 	 	1 1 1  > > > >
               T2Z
$&&CHJJJJJ r   