Example #1
0
def _merge_index(arr, index, aux, low, mid, high):

    # copy to aux[]
    for k in range(low, high + 1):
        aux[k] = index[k]

    # Maintain 3 variables:
    #   i: Current entry on left-half
    #   j: Current entry on left-half
    #   k: Current entry in the sorted result

    # merge back to arr[]
    i = low  # Start LEFT-HAND-POINTER  at left-most side of Left-sided  list
    j = mid + 1  # Start RIGHT-HAND-POINTER at left-most side of Right-sided list
    for k in range(low, high + 1):
        # If left-half list has been completely examined
        if i > mid:
            index[k] = aux[j]
            j += 1
        # If right-half list has been completely examined
        elif j > high:
            index[k] = aux[i]
            i += 1
        # If current value at right-half < current value at right-half, copy smaller val.
        elif __lt__(arr[aux[j]], arr[aux[i]]):
            index[k] = aux[j]
            j += 1
        else:
            index[k] = aux[i]
            i += 1
Example #2
0
def merge(a, aux, lo, mid, hi):  # 05:00-06:00
    """Merge 2 sorted arrays into 1 sorted array."""
    assert _isSorted(a, lo,
                     mid)  # precondition: a[lo .. mid]   are sorted subarrays
    assert _isSorted(a, mid + 1,
                     hi)  # precondition: a[mid+1 .. hi] are sorted subarrays
    aux[lo:hi + 1] = a[lo:hi + 1]  # copy to aux[]
    # merge back to a[] in sorted order
    i = lo  # index of sorted a[lo .. mid]   ( left-half)
    j = mid + 1  # index of sorted a[mid+1 .. hi] (right-half)
    for k in range(lo, hi + 1):  # k is current entry in the sorted result
        if i > mid:
            a[k] = aux[j]
            j += 1  # this copying is unnecessary
        elif j > hi:
            a[k] = aux[i]
            i += 1  # j ptr is exhausted
        elif __lt__(aux[j], aux[i]):
            a[k] = aux[j]
            j += 1
        else:
            a[k] = aux[i]
            i += 1
    #_vis_merge(a, aux, lo, mid, hi)
    assert _isSorted(a, lo, hi)  # postcondition: a[lo .. hi] is sorted
Example #3
0
def merge(arr, aux, low, mid, high):  # 05:00-06:00
    """Merge 2 sorted arrays into 1 sorted array."""
    assert _isSorted(
        arr, low, mid)  # precondition: arr[low .. mid]   are sorted subarrays
    assert _isSorted(
        arr, mid + 1,
        high)  # precondition: arr[mid+1 .. high] are sorted subarrays
    aux[low:high + 1] = arr[low:high + 1]  # copy to aux[]
    # merge back to arr[] in sorted order
    i = low  # index of sorted arr[low .. mid]   ( left-half)
    j = mid + 1  # index of sorted arr[mid+1 .. high] (right-half)
    ## print('RRRRRRRRRRRRRRRRR', range(low, high+1), aux[j], aux[i])
    for k in range(low, high + 1):  # k is current entry in the sorted result
        # i ptr is exhausted  - this copying is unnecessary
        if i > mid:
            arr[k] = aux[j]
            j += 1
        # j ptr is exhausted
        elif j > high:
            arr[k] = aux[i]
            i += 1
        elif __lt__(aux[j], aux[i]):
            arr[k] = aux[j]
            j += 1
        else:
            arr[k] = aux[i]
            i += 1
    # _vis_merge(arr, aux, low, mid, high)
    assert _isSorted(arr, low,
                     high)  # postcondition: arr[low .. high] is sorted
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
Example #5
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
Example #6
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
Example #7
0
def merge(a, aux, lo, mid, hi): # 05:00-06:00
  """Merge 2 sorted arrays into 1 sorted array."""
  assert _isSorted(a, lo, mid)    # precondition: a[lo .. mid]   are sorted subarrays
  assert _isSorted(a, mid+1, hi)  # precondition: a[mid+1 .. hi] are sorted subarrays
  aux[lo:hi+1] = a[lo:hi+1] # copy to aux[]
  # merge back to a[] in sorted order
  i = lo     # index of sorted a[lo .. mid]   ( left-half)
  j = mid+1  # index of sorted a[mid+1 .. hi] (right-half)
  for k in range(lo, hi+1): # k is current entry in the sorted result
    if   i > mid:                a[k] = aux[j]; j += 1 # this copying is unnecessary
    elif j > hi:                 a[k] = aux[i]; i += 1 # j ptr is exhausted
    elif __lt__(aux[j], aux[i]): a[k] = aux[j]; j += 1
    else:                        a[k] = aux[i]; i += 1
  #_vis_merge(a, aux, lo, mid, hi)
  assert _isSorted(a, lo, hi) # postcondition: a[lo .. hi] is sorted
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)
Example #9
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)
Example #10
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)
Example #11
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)
Example #12
0
def _merge_index(a, index, aux, lo, mid, hi):

  # copy to aux[]
  for k in range(lo, hi+1):
    aux[k] = index[k]

  # Maintain 3 variables:
  #   i: Current entry on left-half
  #   j: Current entry on left-half
  #   k: Current entry in the sorted result

  # merge back to a[]
  i = lo     # Start LEFT-HAND-POINTER  at left-most side of Left-sided  list
  j = mid+1  # Start RIGHT-HAND-POINTER at left-most side of Right-sided list
  for k in range(lo, hi+1):
    # If left-half list has been completely examined
    if   i > mid:                     index[k] = aux[j]; j += 1
    # If right-half list has been completely examined
    elif j > hi:                      index[k] = aux[i]; i += 1
    # If current value at right-half < current value at right-half, copy smaller val.
    elif __lt__(a[aux[j]], a[aux[i]]): index[k] = aux[j]; j += 1
    else:                             index[k] = aux[i]; i += 1
Example #13
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)
Example #14
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)
Example #15
0
def _isHsorted(a, h):
    """is the array h-sorted?"""
    for i in range(h, len(a)):
        if __lt__(a[i], a[i - h]):
            return False
    return True
Example #16
0
def _isHsorted(a, h):
  """is the array h-sorted?"""
  for i in range(h,len(a)):
    if __lt__(a[i], a[i-h]): 
      return False
  return True