Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

import math 

import random 

import csv 

import timeit as tiempo 

import unittest 

 

class SortingAlgorithms: 

 

def __init__(self): 

self.fileinputpath = None 

self.fileoutputpath = None 

self.number_records = 0 

self.time_consumed = 0 

self.start_time = 0 

self.end_time = 0 

self.values = [] 

self.outvalues = [] 

 

def set_input_data(self, file_path_name): 

self.fileinputpath = file_path_name 

return self.__set_values() 

 

def __set_values(self): 

try: 

self.values = [] 

with open(self.fileinputpath) as f: 

data = [list(map(int, rec)) 

for rec in csv.reader(f, delimiter=',')] 

self.values = data[0] 

self.number_records = len(self.values) 

return True 

except Exception as ex: 

return False 

 

def get_values(self): 

return self.values 

 

def get_values_sorted(self): 

return self.outvalues 

 

def set_output_data(self, file_path_name): 

self.fileoutputpath = file_path_name 

return self.__set_out_values() 

 

def __set_out_values(self): 

try: 

with open(self.fileoutputpath, 'w', newline='') as csvfile: 

writer = csv.writer(csvfile) 

writer.writerow(self.outvalues) 

return True 

except Exception as ex: 

return False; 

 

def __execute_merge_sort(self, a): 

if len(a) > 1: 

middle = len(a)//2 

l = a[:middle] 

r = a[middle:] 

self.__execute_merge_sort(l) 

self.__execute_merge_sort(r) 

x = y = z = 0 

while x < len(l) and y < len(r): 

if l[x] < r[y]: 

a[z] = l[x] 

x += 1 

else: 

a[z] = r[y] 

y += 1 

z += 1 

while x < len(l): 

a[z] = l[x] 

x += 1 

z += 1 

while y < len(r): 

a[z] = r[y] 

y += 1 

z += 1 

 

def execute_merge_sort(self): 

self.start_time = tiempo.default_timer() 

self.outvalues = self.values 

self.__execute_merge_sort(self.outvalues) 

self.end_time = tiempo.default_timer() 

self.time_consumed = format(self.end_time-self.start_time, '.8f') 

 

def __heapify(self, a, n, i): 

largest = i 

l = 2 * i + 1 

r = 2 * i + 2 

if l < n and a[i] < a[l]: 

largest = l 

if r < n and a[largest] < a[r]: 

largest = r 

if largest != i: 

a[i], a[largest] = a[largest], a[i] 

self.__heapify(a, n, largest) 

 

def __execute_heap_sort(self, a): 

n = len(a) 

for i in range(n, -1, -1): 

self.__heapify(a, n, i) 

for i in range(n-1, 0, -1): 

a[i], a[0] = a[0], a[i] 

self.__heapify(a, i, 0) 

 

def execute_heap_sort(self): 

self.start_time = tiempo.default_timer() 

self.outvalues = self.values 

self.__execute_heap_sort(self.outvalues) 

self.end_time = tiempo.default_timer() 

self.time_consumed = format(self.end_time-self.start_time, '.8f') 

 

def __partition(self, a, low, high): 

i = (low-1) 

pivot = a[high] 

for j in range(low, high): 

if a[j] < pivot: 

i = i+1 

a[i], a[j] = a[j], a[i] 

a[i+1], a[high] = a[high], a[i+1] 

return (i+1) 

 

def __execute_quick_sort(self, a, low, high): 

if low < high: 

pi = self.__partition(a, low, high) 

self.__execute_quick_sort(a, low, pi-1) 

self.__execute_quick_sort(a, pi+1, high) 

 

def execute_quick_sort(self): 

self.start_time = tiempo.default_timer() 

self.outvalues = self.values 

self.__execute_quick_sort(self.outvalues, 0, len(self.outvalues)-1) 

self.end_time = tiempo.default_timer() 

self.time_consumed = format(self.end_time-self.start_time, '.8f') 

 

def get_performance_data(self): 

return self.number_records, self.time_consumed, self.start_time, self.end_time 

 

class My_tests(unittest.TestCase): 

 

def test_set_input_data(self): 

sa= SortingAlgorithms(); 

res = sa.set_input_data('fakefile.csv'); 

self.assertFalse(res) 

 

def test_merge_sort(self): 

sa = SortingAlgorithms() 

sa.set_input_data('random_numbers_1.csv') 

sa.execute_merge_sort() 

res = sa.get_values_sorted() 

self.assertTrue(res[0] < res[1] and res[1]<res[2]) 

 

def test_heap_sort(self): 

sa = SortingAlgorithms() 

sa.set_input_data('random_numbers_1.csv') 

sa.execute_heap_sort() 

res = sa.get_values_sorted() 

self.assertTrue(res[0] < res[1] and res[1]<res[2]) 

 

def test_quick_sort(self): 

sa = SortingAlgorithms() 

sa.set_input_data('random_numbers_1.csv') 

sa.execute_quick_sort() 

res = sa.get_values_sorted() 

self.assertTrue(res[0] < res[1] and res[1]<res[2]) 

 

def test_get_performance_data(self): 

sa = SortingAlgorithms() 

sa.set_input_data('random_numbers_5.csv') 

sa.execute_merge_sort() 

a, b, c, d = sa.get_performance_data() 

self.assertTrue(float(a) > 0 and float(b) > 0 and float(c) > 0 and float(d) > 0) 

 

def test_set_output_data(self): 

sa = SortingAlgorithms() 

sa.set_input_data('random_numbers_5.csv') 

sa.execute_merge_sort() 

res= sa.set_output_data('random_numbers_5_test.csv') 

self.assertTrue(res) 

 

if __name__ == '__main__': 

unittest.main()