Ejemplo n.º 1
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))
Ejemplo n.º 2
0
 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)
 #print(x) # tablica po sortowaniu
 tmpTime += ';' + str(after - before)
Ejemplo n.º 3
0
    def test_reversed(self):
        self.actual.reverse()

        selection.sort(self.actual)
        self.assertEqual(list(sorted(self.actual)), self.actual)
Ejemplo n.º 4
0
import sys, random, time
from random import randint
from time import time
import selection
import burbuja

size = 200

## Creación de la matriz
M1 = []

for i in range(size):
    M1.append(random.randint(1, 20))

M2 = M1

t_inicial_1 = time()
selection.sort(M1)
t_final_1 = time()

t_selection = t_final_1 - t_inicial_1

t_inicial_2 = time()
burbuja.sort(M2)
t_final_2 = time()

t_burbuja = t_final_2 - t_inicial_2

print "El tiempo de ejecucion del algoritmo de la burbuja es", t_burbuja
print "El tiempo de ejecucion del algoritmo de seleccion es", t_selection
Ejemplo n.º 5
0
 def test_sorted(self):
     selection.sort(self.actual)
     self.assertEqual(list(sorted(self.actual)), self.actual)
Ejemplo n.º 6
0
    def test_oneoff(self):
        self.actual[0], self.actual[1] = self.actual[1], self.actual[0]

        selection.sort(self.actual)
        self.assertEqual(list(sorted(self.actual)), self.actual)
Ejemplo n.º 7
0
    def test_random(self):
        random.shuffle(self.actual)

        selection.sort(self.actual)
        self.assertEqual(list(sorted(self.actual)), self.actual)
Ejemplo n.º 8
0
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)
Ejemplo n.º 9
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))
Ejemplo n.º 10
0
 def test_selectionsort(self):
     source = randnum.rand(50)
     target = copy.deepcopy(source)
     selection.sort(source)
     self.assertEqual(source, sorted(target))
Ejemplo n.º 11
0
        end = timeit.default_timer()
        summary.write(str((end-start)*1000) + "\n")
        total += (end-start)*1000
    summary.write("Avg: " + str(total/jobsLimit) + "\n\n")



# SelectionSort
summary.write("[SelectionSort]\n")
for variable in N:
    summary.write("> " + str(variable) + " variable: \n")
    total = 0
    for job in range(jobsLimit):
        data = randnum.rand(variable)
        start = timeit.default_timer()
        selection.sort(data)
        end = timeit.default_timer()
        summary.write(str((end-start)*1000) + "\n")
        total += (end-start)*1000
    summary.write("Avg: " + str(total/jobsLimit) + "\n\n")

# InsertSort
summary.write("[InsertSort]\n")
for variable in N:
    summary.write("> " + str(variable) + " variable: \n")
    total = 0
    for job in range(jobsLimit):
        data = randnum.rand(variable)
        start = timeit.default_timer()
        insert.sort(data)
        end = timeit.default_timer()