fork download
  1. #
  2. #
  3. # Insertion Sort Technique
  4. #
  5. #
  6.  
  7. def sort( arr ):
  8.  
  9. n = len(arr)
  10.  
  11. for i in range(1, n):
  12.  
  13. aux = arr[ i ]
  14.  
  15. j = i - 1
  16.  
  17. while j >= 0 and arr[ j ] >= aux:
  18.  
  19. arr[ j + 1 ] = arr[ j ]
  20.  
  21. j -= 1
  22.  
  23. arr[ j + 1 ] = aux
  24.  
  25. #
  26. # Show Freq function
  27. #
  28.  
  29. def show_freq( arr ):
  30.  
  31. n = len(arr)
  32.  
  33. k = 0
  34.  
  35. freq = [0] * (n+1)
  36.  
  37. vec = [0] * (n+1)
  38.  
  39. vec[k] = arr[k]
  40.  
  41. freq[k] = 1
  42.  
  43. for i in range( n - 1 ):
  44.  
  45. if arr[ i ] == arr[ i + 1 ]:
  46.  
  47. freq[k] += 1
  48. else:
  49.  
  50. k += 1
  51.  
  52. vec[ k ] = arr[ i + 1 ]
  53.  
  54. freq[ k ] = 1
  55.  
  56. for i in range( k + 1 ):
  57.  
  58. if freq[ i ] == 1:
  59.  
  60. print("Element %d apare o singura data"%(vec[i]))
  61. else:
  62. print("Element %d apare de %d ori"%(vec[i], freq[i]))
  63.  
  64. #
  65. #
  66. # Main Function
  67. #
  68. #
  69.  
  70. def main():
  71.  
  72. arr = [5,4,3,2,1,2,13,41,5,6,41,17,8,19,0,-1,2,5,1]
  73.  
  74. print( arr )
  75.  
  76. sort( arr )
  77.  
  78. show_freq( arr )
  79.  
  80. main()
Success #stdin #stdout 0.03s 9160KB
stdin
Standard input is empty
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