4
4
5
5
def mergeSort (array ):
6
6
if len (array ) > 1 :
7
-
8
- r = len (array )// 2
7
+ r = len (array ) // 2
9
8
L = array [:r ]
10
9
M = array [r :]
11
10
12
-
13
11
mergeSort (L )
14
12
mergeSort (M )
15
13
@@ -24,112 +22,100 @@ def mergeSort(array):
24
22
j += 1
25
23
k += 1
26
24
27
-
28
25
while i < len (L ):
29
26
array [k ] = L [i ]
30
27
i += 1
31
28
k += 1
32
29
33
30
while j < len (M ):
34
31
array [k ] = M [j ]
35
- j += 1
32
+ j += 1
36
33
k += 1
37
34
38
-
39
35
def partition (array , low , high ):
40
-
41
36
pivot = array [high ]
42
37
i = low - 1
43
38
44
39
for j in range (low , high ):
45
40
if array [j ] <= pivot :
41
+ i = i + 1
42
+ array [i ], array [j ] = array [j ], array [i ]
46
43
47
- i = i + 1
48
-
49
- (array [i ], array [j ]) = (array [j ], array [i ])
50
-
51
- (array [i + 1 ], array [high ]) = (array [high ], array [i + 1 ])
52
- return i + 1
53
-
44
+ array [i + 1 ], array [high ] = array [high ], array [i + 1 ]
45
+ return i + 1
54
46
55
47
def quickSort (array , low , high ):
56
48
if low < high :
57
49
pi = partition (array , low , high )
58
50
quickSort (array , low , pi - 1 )
59
51
quickSort (array , pi + 1 , high )
60
52
61
-
62
53
def selectionSort (array , size ):
63
54
for step in range (size ):
64
55
min_idx = step
65
-
66
56
for i in range (step + 1 , size ):
67
-
68
57
if array [i ] < array [min_idx ]:
69
58
min_idx = i
70
- (array [step ], array [min_idx ]) = (array [min_idx ], array [step ])
71
-
59
+ array [step ], array [min_idx ] = array [min_idx ], array [step ]
72
60
73
61
def read_Input ():
74
- a = []
75
- n = int (input ("Enter the number of TV Channels:" ))
76
- print ("Enter the no. of viewers" )
77
- for i in range (0 ,n ):
78
- l = int (input ())
62
+ a = []
63
+ n = int (input ("Enter the number of TV Channels: " ))
64
+ print ("Enter the number of viewers" )
65
+ for i in range (0 , n ):
66
+ l = int (input ())
79
67
a .append (l )
80
- return a ;
68
+ return a
81
69
82
70
elements = list ()
83
71
times = list ()
84
- global labeldata
72
+
85
73
print ("1. Merge Sort 2. Quick Sort 3. Selection Sort" )
86
- ch = int (input ("Enter the Choice :" ))
87
- if (ch == 1 ):
88
- array = read_Input ()
89
- mergeSort (array )
90
- labeldata = "MergeSort"
91
- print ("Sorted Array is:" )
92
- print (array )
93
- elif (ch == 2 ):
94
- array = read_Input ()
95
- size = len (array )
96
- labeldata = "QuickSort"
97
- quickSort (array , 0 , size - 1 )
98
- print ('Sorted Array is:' )
99
- print (array )
100
- if (ch == 3 ):
101
- array = read_Input ()
102
- size = len (array )
103
- labeldata = "SelectionSort"
104
- selectionSort (array , size )
105
- print ('Sorted Array is:' )
106
- print (array )
107
- print ("***************************Running Time Analysis************************" )
108
- for i in range (1 ,11 ):
74
+ ch = int (input ("Enter the Choice: " ))
109
75
110
- array = randint ( 0 , 1000 * i , 1000 * i )
111
- start = time . time ()
76
+ if ch in [ 1 , 2 , 3 ]:
77
+ array = read_Input ()
112
78
113
- if ch == 1 :
79
+ if ch == 1 :
80
+ labeldata = "MergeSort"
114
81
mergeSort (array )
115
- elif ch == 2 :
116
- size = len (array )
117
- quickSort (array , 0 , size - 1 )
118
- else :
119
- size = len (array )
82
+ elif ch == 2 :
83
+ labeldata = "QuickSort"
84
+ size = len (array )
85
+ quickSort (array , 0 , size - 1 )
86
+ elif ch == 3 :
87
+ labeldata = "SelectionSort"
88
+ size = len (array )
120
89
selectionSort (array , size )
121
90
122
- end = time .time ()
123
-
124
- print ("Sorted List is:" )
125
- print (len (array ), "Elements Sorted by" , labeldata ,end - start )
126
- elements .append (len (array ))
127
- times .append (end - start )
91
+ print ("Sorted Array is:" )
92
+ print (array )
128
93
129
- plt .xlabel ('List Length' )
130
- plt .ylabel ('Time Complexity' )
131
- labeldata = "sort"
132
- plt .plot (elements , times , label = labeldata )
133
- plt .grid ()
134
- plt .legend ()
135
- plt .show (
94
+ print ("***************************Running Time Analysis************************" )
95
+ for i in range (1 , 11 ):
96
+ array = randint (0 , 1000 * i , 1000 * i )
97
+ start = time .time ()
98
+
99
+ if ch == 1 :
100
+ mergeSort (array )
101
+ elif ch == 2 :
102
+ size = len (array )
103
+ quickSort (array , 0 , size - 1 )
104
+ elif ch == 3 :
105
+ size = len (array )
106
+ selectionSort (array , size )
107
+
108
+ end = time .time ()
109
+
110
+ print (len (array ), "Elements Sorted by" , labeldata , "in" , end - start , "seconds" )
111
+ elements .append (len (array ))
112
+ times .append (end - start )
113
+
114
+ plt .xlabel ('List Length' )
115
+ plt .ylabel ('Time Complexity' )
116
+ plt .plot (elements , times , label = labeldata )
117
+ plt .grid ()
118
+ plt .legend ()
119
+ plt .show ()
120
+ else :
121
+ print ("Invalid choice. Please select 1, 2, or 3 for sorting algorithms." )
0 commit comments