Example #1
0
    def sort(self, n):
        """ Sorts the first n strings of array self.a. Falls back to
        insertion sort for partitions with fewer than
        INS_SORT_THRESHOLD elements. During partitioning swaps items
        that are equal to the pivot to the beginning and end of the
        range and finally swaps them to their correct place before
        sorting the partitions.

        stack     - stores the partitions to be sorted and the corresponding depths
        L,R       - limits of the current partition (L inclusive, R exclusive)
        p         - pivot index, randomly selected from [L,R[
        pivot     - pivot value
        l, r      - sliding indexes. ch(i) <= pivot for i in [L, l[; r mirrors this
        lEq, rEq  - the indexes for the next equal-to-pivot items on the left and right side
        """
        stack = [(0, n, 0)]
        while len(stack) > 0:
            L, R, self.d = stack.pop()
            n = R - L
            if n < INS_SORT_THRESHOLD:
                insertion.sort(self.data, self.a, self.d, L, R)
                continue
            if n < 2:
                continue
            p = L + int(random() * n)
            p = L
            self.swap(L, p)
            lEq = l = L + 1
            rEq = r = R - 1
            pivot = self.ch(L)
            while True:
                while l <= r and self.ch(l) <= pivot:
                    if self.ch(l) == pivot:
                        self.swap(lEq, l)
                        lEq += 1
                    l += 1
                while l <= r and self.ch(r) >= pivot:
                    if self.ch(r) == pivot:
                        self.swap(rEq, r)
                        rEq -= 1
                    r -= 1
                if l > r:
                    break
                self.swap(l, r)
                l += 1
                r -= 1
            c = min(lEq - L, l - lEq)
            self.nswap(L, l - c, c)
            c = min(R - 1 - rEq, rEq - r)
            self.nswap(l, R - c, c)
            stack.append((L, L + l - lEq, self.d))
            if pivot != EOS:
                stack.append((L + l - lEq, l + (R - 1 - rEq), self.d + 1))
            stack.append((R - (rEq - r), R, self.d))
Example #2
0
    def sort(self, n):
        """ Sorts the first n strings of array self.a. Falls back to
        insertion sort for partitions with fewer than
        INS_SORT_THRESHOLD elements. During partitioning swaps items
        that are equal to the pivot to the beginning and end of the
        range and finally swaps them to their correct place before
        sorting the partitions.

        stack     - stores the partitions to be sorted and the corresponding depths
        L,R       - limits of the current partition (L inclusive, R exclusive)
        p         - pivot index, randomly selected from [L,R[
        pivot     - pivot value
        l, r      - sliding indexes. ch(i) <= pivot for i in [L, l[; r mirrors this
        lEq, rEq  - the indexes for the next equal-to-pivot items on the left and right side
        """
        stack = [(0, n, 0)]
        while len(stack) > 0:
            L, R, self.d = stack.pop()
            n = R - L
            if n < INS_SORT_THRESHOLD:
                insertion.sort(self.data, self.a, self.d, L, R)
                continue
            if n < 2:
                continue
            p = L + int(random() * n)
            p = L
            self.swap(L, p)
            lEq = l = L + 1
            rEq = r = R - 1
            pivot = self.ch(L)
            while True:
                while l <= r and self.ch(l) <= pivot:
                    if self.ch(l)  == pivot:
                        self.swap(lEq, l)
                        lEq += 1
                    l += 1
                while l <= r and self.ch(r) >= pivot:
                    if self.ch(r) == pivot:
                        self.swap(rEq, r)
                        rEq -= 1
                    r -= 1
                if l > r:
                    break
                self.swap(l, r)
                l += 1
                r -= 1
            c = min(lEq - L, l - lEq)
            self.nswap(L, l - c, c)
            c = min(R - 1 - rEq, rEq - r)
            self.nswap(l, R - c, c)
            stack.append((L, L + l - lEq, self.d))
            if pivot != EOS:
                stack.append((L + l - lEq, l + (R - 1 - rEq), self.d + 1))
            stack.append((R - (rEq - r), R, self.d))
Example #3
0
def main():
    list = [10,1,12,23,44,5,67,27,8,9,10,11]
    list_b = [10,1,12,23,44,5,67,27,8,9,10,11]
    list_c = [1,2,4,5,3]

    print(insertion.sort(list))
    print(list_b)
    print("Sorted! ")
Example #4
0
def sort(array):
    step = len(array) // 2
    while step != 0:
        for offset in range(step):
            sorted = insertion.sort(array[offset::step])
            for i in range(len(sorted)):
                array[step * i + offset] = sorted[i]
        step //= 2
    return sorted
def test_sort():
    assert sort([1, 2, 3]) == [1, 2, 3]
    assert sort([3, 2, 1]) == [1, 2, 3]
    assert sort([1, 3, 2]) == [1, 2, 3]
    assert sort([3, 3, 3]) == [3, 3, 3]
    assert sort([1]) == [1]
    assert sort([]) == []
Example #6
0
 if j == 0:
     x = setupRandom(seed, i)
 elif j == 1:
     x = setupConst(i)
 elif j == 2:
     x = setupDesc(i)
 elif j == 3:
     x = setupAsc(i)
 elif j == 4:
     x = setupA(i)
 #print(x) # tablica przed posortowaniem
 before = round(time.time_ns() / (10**9), 10)
 if s == 0:
     x = bubble.sort(x)
 elif s == 1:
     x = insertion.sort(x)
 elif s == 2:
     x = selection.sort(x)
 elif s == 3:
     x = counting.sort(x, i, maxNumber)
 elif s == 4:
     x = heap.sort(x)
 elif s == 5:
     x = quickRight.sort(x, 0, i - 1)
 elif s == 6:
     x = quickRandom.sort(x, 0, i - 1)
 elif s == 7:
     x = shell.sort(x)
 elif s == 8:
     x = merge.sort(x, 0, len(x) - 1)
 after = round(time.time_ns() / (10**9), 10)
Example #7
0
    def test_reversed(self):
        self.actual.reverse()

        insertion.sort(self.actual)
        self.assertEqual(list(sorted(self.actual)), self.actual)
Example #8
0
    def test_oneoff(self):
        self.actual[0], self.actual[1] = self.actual[1], self.actual[0]

        insertion.sort(self.actual)
        self.assertEqual(list(sorted(self.actual)), self.actual)
Example #9
0
 def test_sorted(self):
     insertion.sort(self.actual)
     self.assertEqual(list(sorted(self.actual)), self.actual)
Example #10
0
    def test_random(self):
        random.shuffle(self.actual)

        insertion.sort(self.actual)
        self.assertEqual(list(sorted(self.actual)), self.actual)
Example #11
0
def choose_order():
  while True:
    try:
      order = input('[0] ascend,  [1] descend -> ')
      if order == 0 or order == 1:
        return order
      else:
        print 'Invalid input!!'
    except ValueError:
      print 'ValueError! Please try again!'

def sort_order(order='ascend'):
  if order == 'ascend':
    return lambda x, y: x > y
  elif order == 'descend':
    return lambda x, y: x < y

if __name__ == '__main__':
  A = input_data()
  if choose_order() == 0:
    order = sort_order('ascend')
  else:
    order = sort_order('descend')

  print 'Selection: ' + str(selection.sort(copy.deepcopy(A), order))
  print 'Insertion: ' + str(insertion.sort(copy.deepcopy(A), order))
  print '    Shell: ' + str(shell.sort(copy.deepcopy(A), order))
  print '    Merge: ' + str(merge.sort(copy.deepcopy(A), 0, len(A)-1, order))
  print '     Bogo: ' + str(bogo.sort(copy.deepcopy(A), order))
Example #12
0
from insertion import sort
file = input("Indique la ubicacion del archivo: ")
f=open(file, "r")

A = []
f1 = f.readlines()
for line in f1:
	A.append(int(line))

print("Antes -> "+str(A))
sort(A, len(A) )
print("Despues -> "+str(A))
Example #13
0
 def test_sort(self):
     list = generate_list(1000)
     x = insertion.sort(list)
     self.assertTrue(x == sorted_list(list))
import bubble
import selection
import insertion


array = [1, 3, 20, 18, 7, 50, 2]

print('Sorting using BubbleSort Algorithm:')
bubble.sort(array)

print('Sorting using SelectionSort Algorithm')
selection.sort(array)

print('Sorting using InsertionSort Algorithm')
insertion.sort(array)
Example #15
0
            order = input('[0] ascend,  [1] descend -> ')
            if order == 0 or order == 1:
                return order
            else:
                print 'Invalid input!!'
        except ValueError:
            print 'ValueError! Please try again!'


def sort_order(order='ascend'):
    if order == 'ascend':
        return lambda x, y: x > y
    elif order == 'descend':
        return lambda x, y: x < y


if __name__ == '__main__':
    A = input_data()
    if choose_order() == 0:
        order = sort_order('ascend')
    else:
        order = sort_order('descend')

    print 'Selection: ' + str(selection.sort(copy.deepcopy(A), order))
    print 'Insertion: ' + str(insertion.sort(copy.deepcopy(A), order))
    print '    Shell: ' + str(shell.sort(copy.deepcopy(A), order))
    print '    Merge: ' + str(
        merge.sort(copy.deepcopy(A), 0,
                   len(A) - 1, order))
    print '     Bogo: ' + str(bogo.sort(copy.deepcopy(A), order))