#
#
# Insertion Sort Technique
#
#
def sort( arr ) :
n = len ( arr)
for i in range ( 1 , n) :
aux = arr[ i ]
j = i - 1
while j >= 0 and arr[ j ] >= aux:
arr[ j + 1 ] = arr[ j ]
j -= 1
arr[ j + 1 ] = aux
#
# Show Freq function
#
def show_freq( arr ) :
n = len ( arr)
k = 0
freq = [ 0 ] * ( n+1 )
vec = [ 0 ] * ( n+1 )
vec[ k] = arr[ k]
freq[ k] = 1
for i in range ( n - 1 ) :
if arr[ i ] == arr[ i + 1 ] :
freq[ k] += 1
else :
k += 1
vec[ k ] = arr[ i + 1 ]
freq[ k ] = 1
for i in range ( k + 1 ) :
if freq[ i ] == 1 :
print ( "Element %d apare o singura data" %( vec[ i] ) )
else :
print ( "Element %d apare de %d ori" %( vec[ i] , freq[ i] ) )
#
#
# Main Function
#
#
def main( ) :
arr = [ 5 , 4 , 3 , 2 , 1 , 2 , 13 , 41 , 5 , 6 , 41 , 17 , 8 , 19 , 0 , -1 , 2 , 5 , 1 ]
print ( arr )
sort( arr )
show_freq( arr )
main( )
IwojCiMgSW5zZXJ0aW9uIFNvcnQgVGVjaG5pcXVlCiMKIwoKZGVmIHNvcnQoIGFyciApOgoKCW4gPSBsZW4oYXJyKQoKCWZvciBpIGluIHJhbmdlKDEsIG4pOgoKCQlhdXggPSBhcnJbIGkgXQoKCQlqID0gaSAtIDEKCgkJd2hpbGUgaiA+PSAwIGFuZCBhcnJbIGogXSA+PSBhdXg6CgoJCQlhcnJbIGogKyAxIF0gPSBhcnJbIGogXQoKCQkJaiAtPSAxCgoJCWFyclsgaiArIDEgXSA9IGF1eAkKCiMKIyBTaG93IEZyZXEgZnVuY3Rpb24KIwkJCgpkZWYgc2hvd19mcmVxKCBhcnIgKToKCgluID0gbGVuKGFycikKCglrID0gMAoKCWZyZXEgPSBbMF0gKiAobisxKQoKCXZlYyA9IFswXSAqIChuKzEpCgoJdmVjW2tdID0gYXJyW2tdCgoJZnJlcVtrXSA9IDEKCglmb3IgaSBpbiByYW5nZSggbiAtIDEgKToKCgkJaWYgYXJyWyBpIF0gPT0gYXJyWyBpICsgMSBdOgkJCQoKCQkJZnJlcVtrXSArPSAxCgkJZWxzZToKCgkJICAgIGsgKz0gMQoKCQkgICAgdmVjWyBrIF0gPSBhcnJbIGkgKyAxIF0KCgkJICAgIGZyZXFbIGsgXSA9IDEKCglmb3IgaSBpbiByYW5nZSggayArIDEgKToKCgkJaWYgZnJlcVsgaSBdID09IDE6CgoJCQlwcmludCgiRWxlbWVudCAlZCBhcGFyZSBvIHNpbmd1cmEgZGF0YSIlKHZlY1tpXSkpCgkJZWxzZToKCQkJcHJpbnQoIkVsZW1lbnQgJWQgYXBhcmUgZGUgJWQgb3JpIiUodmVjW2ldLCBmcmVxW2ldKSkKCiMKIwojIE1haW4gRnVuY3Rpb24KIwojCgkJCQpkZWYgbWFpbigpOgoKCWFyciA9IFs1LDQsMywyLDEsMiwxMyw0MSw1LDYsNDEsMTcsOCwxOSwwLC0xLDIsNSwxXQoKCXByaW50KCBhcnIgKQoJCglzb3J0KCBhcnIgKQoKCXNob3dfZnJlcSggYXJyICkKCm1haW4oKQk=
stdout
[5, 4, 3, 2, 1, 2, 13, 41, 5, 6, 41, 17, 8, 19, 0, -1, 2, 5, 1]
Element -1 apare o singura data
Element 0 apare o singura data
Element 1 apare de 2 ori
Element 2 apare de 3 ori
Element 3 apare o singura data
Element 4 apare o singura data
Element 5 apare de 3 ori
Element 6 apare o singura data
Element 8 apare o singura data
Element 13 apare o singura data
Element 17 apare o singura data
Element 19 apare o singura data
Element 41 apare de 2 ori