Esempio n. 1
0
def indexSort(ARR, array_history=None):
  """Do not change ARR, return a new sorted version of ARR."""
  N = len(ARR)
  index = range(N)
  for i in range(N):
    j = i
    while j > 0 and __lt__(ARR[index[j]], ARR[index[j-1]]):
      _exch(index, j, j-1)
      if array_history is not None: array_history.add_history(ARR, {j:'*', j-1:'*'})
      j -= 1
  return index
Esempio n. 2
0
def indexSort(ARR, array_history=None):
    """Do not change ARR, return a new sorted version of ARR."""
    N = len(ARR)
    index = range(N)
    for i in range(N):
        j = i
        while j > 0 and __lt__(ARR[index[j]], ARR[index[j - 1]]):
            _exch(index, j, j - 1)
            if array_history is not None:
                array_history.add_history(ARR, {j: '*', j - 1: '*'})
            j -= 1
    return index
Esempio n. 3
0
def indexSort(arr, array_history=None):
    """Do not change arr, return a new sorted version of arr."""
    num_elems = len(arr)
    index = range(num_elems)
    for i in range(num_elems):
        j = i
        while j > 0 and __lt__(arr[index[j]], arr[index[j - 1]]):
            _exch(index, j, j - 1)
            if array_history is not None:
                array_history.add_history(arr, {j: '*', j - 1: '*'})
            j -= 1
    return index
Esempio n. 4
0
def Sort(ARR, array_history=None):
  """Rearranges the array in ascending order, using the natural order."""
  N = len(ARR)
  # 00:57 Everything to the left is in acending order
  #       Everything to the right, we have not seen at all
  for i in range(N):
    j = i
    # Exchange the curr Elem with every element to the left that is > 01:21
    while j > 0 and __lt__(ARR[j], ARR[j-1]): # Iterate from i back towards 0
      if array_history is not None: array_history.add_history(ARR, {j:'*', j-1:'*'})
      _exch(ARR, j, j-1)
      j -= 1
    assert _isSorted(ARR, 0, i)
  assert _isSorted(ARR);
  if array_history is not None: array_history.add_history(ARR, None)
Esempio n. 5
0
def Sort(ARR, array_history=None):
    """Rearranges the array in ascending order, using the natural order."""
    N = len(ARR)
    # 00:57 Everything to the left is in acending order
    #       Everything to the right, we have not seen at all
    for i in range(N):
        j = i
        # Exchange the curr Elem with every element to the left that is > 01:21
        while j > 0 and __lt__(ARR[j],
                               ARR[j - 1]):  # Iterate from i back towards 0
            if array_history is not None:
                array_history.add_history(ARR, {j: '*', j - 1: '*'})
            _exch(ARR, j, j - 1)
            j -= 1
        assert _isSorted(ARR, 0, i)
    assert _isSorted(ARR)
    if array_history is not None: array_history.add_history(ARR, None)
Esempio n. 6
0
def Sort(ARR, array_history=None):
  """Rearranges the array, ARR, in ascending order, using the natural order."""
  # param array_history; For visualization. When true prints ASCII Art demonstrating the sort
  N = len(ARR)
  # Items from i to j-1 are Sorted
  # IN the ith iteration, find the smallest remaining Item above i
  for i in range(N): # MOVE pointer to the right
    min_elem_idx = i # Index of smallest element to the right of pointer i
    # Identify index of min Item right of j
    for j in range(i+1,N):
      if __lt__(ARR[j], ARR[min_elem_idx]):  # COMPARE is counted toward cost
        min_elem_idx = j
    if array_history is not None: array_history.add_history(ARR, {i:'*', min_elem_idx:'*'})
    _exch(ARR, i, min_elem_idx)           # EXCHANGE is counted toward cost
    assert _isSorted(ARR, 0, i)
  assert _isSorted(ARR)
  if array_history is not None: array_history.add_history(ARR, None)
Esempio n. 7
0
def Sort(ARR, array_history=None):
    """Rearranges the array, ARR, in ascending order, using the natural order."""
    # param array_history; For visualization. When true prints ASCII Art demonstrating the sort
    N = len(ARR)
    # Items from i to j-1 are Sorted
    # IN the ith iteration, find the smallest remaining Item above i
    for i in range(N):  # MOVE pointer to the right
        min_elem_idx = i  # Index of smallest element to the right of pointer i
        # Identify index of min Item right of j
        for j in range(i + 1, N):
            if __lt__(ARR[j],
                      ARR[min_elem_idx]):  # COMPARE is counted toward cost
                min_elem_idx = j
        if array_history is not None:
            array_history.add_history(ARR, {i: '*', min_elem_idx: '*'})
        _exch(ARR, i, min_elem_idx)  # EXCHANGE is counted toward cost
        assert _isSorted(ARR, 0, i)
    assert _isSorted(ARR)
    if array_history is not None: array_history.add_history(ARR, None)
Esempio n. 8
0
def Sort(ARR, array_history=None, sort_seq=None):
    """Rearranges the array, ARR, in ascending order, using the natural order."""
    # array_history; Used in tests. When true prints ASCII Art demonstrating the sort
    N = len(ARR)

    # 3x+1 increment sequence:  [1, 4, 13, 40, 121, 364, 1093, ...
    ha = get_sort_seq(N, sort_seq)
    print ha

    for h in reversed(ha):
        # h-sort the array (insertion sort)
        for i in range(h, N):
            j = i
            while j >= h and __lt__(ARR[j], ARR[j - h]):
                if array_history is not None:
                    array_history.add_history(ARR, {j: '*', j - h: '*'})
                _exch(ARR, j, j - h)
                j -= h
        assert _isHsorted(ARR, h)
    assert _isSorted(ARR)
    if array_history is not None:
        array_history.add_history(ARR, None)
Esempio n. 9
0
def Sort(ARR, array_history=None, sort_seq=None):
  """Rearranges the array, ARR, in ascending order, using the natural order."""
  # array_history; Used in tests. When true prints ASCII Art demonstrating the sort
  N = len(ARR)

  # 3x+1 increment sequence:  [1, 4, 13, 40, 121, 364, 1093, ...
  ha = get_sort_seq(N, sort_seq)
  print ha

  for h in reversed(ha):
    # h-sort the array (insertion sort)
    for i in range(h,N):
      j = i
      while j >= h and __lt__(ARR[j], ARR[j-h]):
        if array_history is not None:
          array_history.add_history(ARR, {j:'*', j-h:'*'} )
        _exch(ARR, j, j-h)
        j -= h
    assert _isHsorted(ARR, h)
  assert _isSorted(ARR)
  if array_history is not None:
    array_history.add_history(ARR, None)