
    Ng                     ^    d Z d Zd Zd Zd Zd Zd Zd Zd Ze	d	k    r e             d
S d
S )z
This module provides a variety of list sorting algorithms, to
illustrate the many different algorithms (recipes) for solving a
problem, and how to analyze algorithms experimentally.
c                     d}t          t          |           dz
            D ]W}|}t          |dz   t          |                     D ]}| |         | |         k     r|}|dz  }| |         | |         c| |<   | |<   X|S )z
    Selection Sort: scan the list to find its smallest element, then
    swap it with the first element.  The remainder of the list is one
    element smaller; apply the same method to this list, and so on.
           rangelen)acountiminjs        J/var/www/html/ai-engine/env/lib/python3.11/site-packages/nltk/misc/sort.py	selectionr      s     E3q66A: 	$ 	$q1uc!ff%% 	 	Ataf}}QJEEtQsV#!L    c                    d}t          t          |           dz
            D ]^}t          t          |           |z
  dz
            D ]9}| |dz            | |         k     r"| |dz            | |         c| |<   | |dz   <   |dz  }:_|S )aG  
    Bubble Sort: compare adjacent elements of the list left-to-right,
    and swap them if they are out of order.  After one pass through
    the list swapping adjacent items, the largest item will be in
    the rightmost position.  The remainder is one element smaller;
    apply the same method to this list, and so on.
    r   r   r   )r   r	   r
   r   s       r   bubbler   0   s     E3q66A:  s1vvzA~&& 	 	AQx!A$!"1q51Q4!aAh
	 Lr   c                    d}dx}}g }|t          |           k     r|t          |          k     r~|dz  }| |         ||         k    r!|                    | |                    |dz  }n |                    ||                    |dz  }|t          |           k     r|t          |          k     ~|t          |           k    r|||d          z  }n|| |d          z  }||fS Nr   r   )r   append)bcr	   r
   r   r   s         r   _merge_listsr   F   s    EIA
A
c!ff**SVV
Q41Q4<<HHQqTNNNFAAHHQqTNNNFA c!ff**SVV 	CFF{{	QqrrU
	QqrrU
e8Or   c                     d}t          |           dk    rft          |           dz  }| d|         }| |d         }t          |          }t          |          }t          ||          \  }}|| dd<   ||z   |z   }|S )ze
    Merge Sort: split the list in half, and sort each half, then
    combine the sorted halves.
    r   r      N)r   merger   )	r   r	   midpointr   r   count_bcount_cresultcount_as	            r   r   r   Y   s    
 E
1vvzzq66Q;ixiLhiiL((((&q!,,!!!'!G+Lr   c                 V   | |         }|}|dz   }d}	 ||k     r|dz  }| |         |k    rn||k     ||k    r|dz  }||k     s| |         |k    rn||k    | |         | |         c| |<   | |<   |dz  }||k    rnf| |         | |         c| |<   | |<   | |         | |         c| |<   | |<   ||fS )Nr   r    )r   lrpr
   r   r	   s          r   
_partitionr%   p   s    	!A	A	AAE!eeFAtqyy !ee !eeFA1uu!		 !ee qT1Q4
!ad
66 1qtJAaD!A$1qtJAaD!A$e8Or   c                     d}||k     rBt          | ||          \  }}|t          | ||dz
            z  }|t          | |dz   |          z  }|S r   )r%   _quick)r   r"   r#   r	   ss        r   r'   r'      s^    E1uuaA&&51a!e$$$1q5!$$$Lr   c                 D    t          | dt          |           dz
            S r   )r'   r   )r   s    r   quickr*      s    !QA
###r   c            	      R   ddl m}  dD ]}t          t          |                    } | |           t	          |          } | |           t          |          } | |           t          |          } | |           t          |          }t          d|||||fz             d S )Nr   )shuffle)
      2   d      i  i  z=size=%5d:  selection=%8d,  bubble=%8d,  merge=%6d,  quick=%6d)	randomr,   listr   r   r   r   r*   print)r,   sizer   count_selectioncount_bubblecount_mergecount_quicks          r   demor:      s    1 
 
t 	


#A,,


ayy


Ahh


AhhO_lKMN	
 	
 	
 	

 
r   __main__N)
__doc__r   r   r   r   r%   r'   r*   r:   __name__r!   r   r   <module>r>      s      6  ,  &  .  .  $ $ $
 
 
, zDFFFFF r   