Example #1
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
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(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 #5
0
def Sort(a, array_history=None): # 09:30
  """Rearranges the array in ascending order, using the natural order."""
  # At most N lg N compares and 6 N lg N array accesses to sort any array of size N
  aux = [None for i in range(len(a))] # Create aux outside _sort to avoid extensive costs.
  _add_history(array_history, a, aux) # Record initial state of arrays
  _sort(a, aux, lo=0, hi=len(a)-1, array_history=array_history)
  assert _isSorted(a)
Example #6
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 #7
0
def Sort(arr, array_history=None):  # 09:30
    """Rearranges the array in ascending order, using the natural order."""
    # At most N lg N compares and 6 N lg N array accesses to sort any array of size N
    aux = [None] * len(
        arr)  # Create aux outside _sort to avoid extensive costs.
    _add_history(array_history, arr, aux)  # Record initial state of arrays
    _sort(arr, aux, low=0, high=len(arr) - 1, array_history=array_history)
    assert _isSorted(arr)
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 #9
0
def Sort(pq):
  N = len(pq)
  for k in range(N/2, 0, -1):
    _sink(pq, k, N)
  while (N > 1):
    _exch(pq, 1, N)
    N -= 1
    _sink(pq, 1, N)
  assert _isSorted(pq)
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(a, array_history=None): # N lg N
  """Rearranges the array, a, in ascending order, using the natural order."""
  N = len(a)
  aux = [None for i in range(N)]
  _add_history(array_history, a, aux) # Record initial state of arrays
  sarr_sz = 1
  # First nested loop is "Size of the sub-array" executed only lg N times (lg N passes)
  while sarr_sz < N: # i.e. for (int sz=1; sz<N; sz=sz+sz)
    lo = 0
    while lo < N-sarr_sz: # i.e. for (int lo = 0; lo < N-sz; lo += sz+sz)
      mid = lo + sarr_sz - 1
      hi  = min(lo + sarr_sz + sarr_sz - 1, N-1)
      merge(a, aux, lo, mid, hi)
      _add_history(array_history, a, aux, (lo, hi, mid))
      lo += sarr_sz+sarr_sz
    sarr_sz = sarr_sz+sarr_sz # Double the size of the sub-array until we get to N
  assert _isSorted(a)
Example #12
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 #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)