简体   繁体   English

shell 排序比较的计数

[英]Count of shell sort comparisons

I have the next problem.我有下一个问题。 There is an implementation of shell sort algorithm below.下面有一个shell排序算法的实现。 How can I count number of comparisons?如何计算比较次数? Where should I increase number of comparisons?我应该在哪里增加比较次数? Because rn I am not sure whether I count in a right way因为我不确定我是否以正确的方式计算

def shell_sort(arr):
    mid = len(arr) // 2
    number_of_comparings = 0
    while mid > 0:
        i = 0
        j = mid
        while j < len(arr):
            if arr[i] >arr[j]:
                arr[i],arr[j] = arr[j],arr[i]
            i += 1
            j += 1
            k = i
            while k - mid > -1:
                if arr[k - mid] > arr[k]:
                    arr[k-mid],arr[k] = arr[k],arr[k-mid]
                k -= 1
                number_of_comparings+=1
        mid //= 2
    print(number_of_comparings)
    return arr
def shell_sort(arr):
    mid = len(arr) // 2
    number_of_comparings = 0
    while mid > 0:
        i = 0
        j = mid
        while j < len(arr):
            if arr[i] >arr[j]:
                arr[i],arr[j] = arr[j],arr[i]
                number_of_comparings += 1
                print(str(number_of_comparings) + "- "+str(arr))
            i += 1
            j += 1
            k = i
            while k - mid > -1:
                if arr[k - mid] > arr[k]:
                    arr[k-mid],arr[k] = arr[k],arr[k-mid]
                    number_of_comparings += 1
                    print(str(number_of_comparings) + "- " + str(arr))
                k -= 1
        mid //= 2
    print("Number of Comparings is: " + str(number_of_comparings))
    return arr

example if arr = [1,1,1,0,4,5,3,2,3,100,3,4] Number of Comparings Will be 7例如,如果arr = [1,1,1,0,4,5,3,2,3,100,3,4]比较次数将为 7

Output:

1- [1, 1, 1, 0, 3, 5, 3, 2, 3, 100, 4, 4]
2- [1, 1, 1, 0, 3, 4, 3, 2, 3, 100, 4, 5]
3- [0, 1, 1, 1, 3, 4, 3, 2, 3, 100, 4, 5]
4- [0, 1, 1, 1, 2, 4, 3, 3, 3, 100, 4, 5]
5- [0, 1, 1, 1, 2, 3, 3, 3, 4, 100, 4, 5]
6- [0, 1, 1, 1, 2, 3, 3, 3, 4, 4, 100, 5]
7- [0, 1, 1, 1, 2, 3, 3, 3, 4, 4, 5, 100]
Number of Comparings is: 7
[0, 1, 1, 1, 2, 3, 3, 3, 4, 4, 5, 100]

but if u want to see all Comparings like if-else , loop(while,for,...)... your code will be:但是,如果您想查看所有比较,例如if-else , loop(while,for,...)...您的代码将是:

def shell_sort(arr):
    mid = len(arr) // 2
    number_of_comparings = 0
    while mid > 0:
        # --------------------------------------------------------------------------------
        number_of_comparings += 1
        print(str(number_of_comparings) + "- while (mid=" + str(mid) +") > 0")
        # --------------------------------------------------------------------------------
        i = 0
        j = mid

        while j < len(arr):
            if arr[i] >arr[j]:
                arr[i],arr[j] = arr[j],arr[i]
            #--------------------------------------------------------------------------------
            number_of_comparings += 1
            print(str(number_of_comparings) + "- if " + "arr[" +str(i)+
                  "] > arr[" + str(j) + "] : " + str(arr[i]) + " > " + str(arr[j]))
            number_of_comparings += 1
            print(str(number_of_comparings) + "- while "+ str(j) +
                  " < Length of arr (" + str(len(arr)) + ")")
            # --------------------------------------------------------------------------------
            i += 1
            j += 1
            k = i
            while k - mid > -1:
                if arr[k - mid] > arr[k]:
                    arr[k-mid],arr[k] = arr[k],arr[k-mid]
                # --------------------------------------------------------------------------------
                number_of_comparings += 1
                print(str(number_of_comparings) + "- if " + "arr[" + str(k)+ " - " + str(mid) +
                "] > arr["+str(k)+"] : "+str(arr[k - mid]) + " > " + str(arr[k]))
                number_of_comparings += 1
                print(str(number_of_comparings) + "- while " +str(k)+" - "+ str(mid)+" > -1")
                # --------------------------------------------------------------------------------
                k -= 1
            # --------------------------------------------------------------------------------
            number_of_comparings += 1
            print(str(number_of_comparings) + "- while " + str(k) + " - " + str(mid) + " > -1")
            # --------------------------------------------------------------------------------
        # --------------------------------------------------------------------------------
        number_of_comparings += 1
        print(str(number_of_comparings) + "- while " + str(j) +
              " < Length of arr (" + str(len(arr)) + ")")
        # --------------------------------------------------------------------------------
        mid //= 2
    # --------------------------------------------------------------------------------
    number_of_comparings += 1
    print(str(number_of_comparings) + "- while (mid=" + str(mid) +") > 0")
    print("Number of Comparings is: " + str(number_of_comparings))
    # --------------------------------------------------------------------------------
    return arr

example if arr = [1,1,1,0,4,5,3,2,3,100,3,4] Number of Comparings Will be 275例如,如果arr = [1,1,1,0,4,5,3,2,3,100,3,4]比较次数将为 275

Output:
1- while (mid=6) > 0
2- if arr[0] > arr[6] : 1 > 3
3- while 6 < Length of arr (12)
4- while 1 - 6 > -1
5- if arr[1] > arr[7] : 1 > 2
6- while 7 < Length of arr (12)
7- while 2 - 6 > -1
8- if arr[2] > arr[8] : 1 > 3
9- while 8 < Length of arr (12)
10- while 3 - 6 > -1
11- if arr[3] > arr[9] : 0 > 100
12- while 9 < Length of arr (12)
13- while 4 - 6 > -1
14- if arr[4] > arr[10] : 3 > 4
15- while 10 < Length of arr (12)
16- while 5 - 6 > -1
17- if arr[5] > arr[11] : 4 > 5
18- while 11 < Length of arr (12)
19- if arr[6 - 6] > arr[6] : 1 > 3
20- while 6 - 6 > -1
21- while 5 - 6 > -1
22- while 12 < Length of arr (12)
23- while (mid=3) > 0
24- if arr[0] > arr[3] : 0 > 1
25- while 3 < Length of arr (12)
26- while 1 - 3 > -1
27- if arr[1] > arr[4] : 1 > 3
28- while 4 < Length of arr (12)
29- while 2 - 3 > -1
30- if arr[2] > arr[5] : 1 > 4
31- while 5 < Length of arr (12)
32- if arr[3 - 3] > arr[3] : 0 > 1
33- while 3 - 3 > -1
34- while 2 - 3 > -1
35- if arr[3] > arr[6] : 1 > 3
36- while 6 < Length of arr (12)
37- if arr[4 - 3] > arr[4] : 1 > 3
38- while 4 - 3 > -1
39- if arr[3 - 3] > arr[3] : 0 > 1
40- while 3 - 3 > -1
41- while 2 - 3 > -1
42- if arr[4] > arr[7] : 2 > 3
43- while 7 < Length of arr (12)
44- if arr[5 - 3] > arr[5] : 1 > 4
45- while 5 - 3 > -1
46- if arr[4 - 3] > arr[4] : 1 > 2
47- while 4 - 3 > -1
48- if arr[3 - 3] > arr[3] : 0 > 1
49- while 3 - 3 > -1
50- while 2 - 3 > -1
51- if arr[5] > arr[8] : 3 > 4
52- while 8 < Length of arr (12)
53- if arr[6 - 3] > arr[6] : 1 > 3
54- while 6 - 3 > -1
55- if arr[5 - 3] > arr[5] : 1 > 3
56- while 5 - 3 > -1
57- if arr[4 - 3] > arr[4] : 1 > 2
58- while 4 - 3 > -1
59- if arr[3 - 3] > arr[3] : 0 > 1
60- while 3 - 3 > -1
61- while 2 - 3 > -1
62- if arr[6] > arr[9] : 3 > 100
63- while 9 < Length of arr (12)
64- if arr[7 - 3] > arr[7] : 2 > 3
65- while 7 - 3 > -1
66- if arr[6 - 3] > arr[6] : 1 > 3
67- while 6 - 3 > -1
68- if arr[5 - 3] > arr[5] : 1 > 3
69- while 5 - 3 > -1
70- if arr[4 - 3] > arr[4] : 1 > 2
71- while 4 - 3 > -1
72- if arr[3 - 3] > arr[3] : 0 > 1
73- while 3 - 3 > -1
74- while 2 - 3 > -1
75- if arr[7] > arr[10] : 3 > 4
76- while 10 < Length of arr (12)
77- if arr[8 - 3] > arr[8] : 3 > 4
78- while 8 - 3 > -1
79- if arr[7 - 3] > arr[7] : 2 > 3
80- while 7 - 3 > -1
81- if arr[6 - 3] > arr[6] : 1 > 3
82- while 6 - 3 > -1
83- if arr[5 - 3] > arr[5] : 1 > 3
84- while 5 - 3 > -1
85- if arr[4 - 3] > arr[4] : 1 > 2
86- while 4 - 3 > -1
87- if arr[3 - 3] > arr[3] : 0 > 1
88- while 3 - 3 > -1
89- while 2 - 3 > -1
90- if arr[8] > arr[11] : 4 > 5
91- while 11 < Length of arr (12)
92- if arr[9 - 3] > arr[9] : 3 > 100
93- while 9 - 3 > -1
94- if arr[8 - 3] > arr[8] : 3 > 4
95- while 8 - 3 > -1
96- if arr[7 - 3] > arr[7] : 2 > 3
97- while 7 - 3 > -1
98- if arr[6 - 3] > arr[6] : 1 > 3
99- while 6 - 3 > -1
100- if arr[5 - 3] > arr[5] : 1 > 3
101- while 5 - 3 > -1
102- if arr[4 - 3] > arr[4] : 1 > 2
103- while 4 - 3 > -1
104- if arr[3 - 3] > arr[3] : 0 > 1
105- while 3 - 3 > -1
106- while 2 - 3 > -1
107- while 12 < Length of arr (12)
108- while (mid=1) > 0
109- if arr[0] > arr[1] : 0 > 1
110- while 1 < Length of arr (12)
111- if arr[1 - 1] > arr[1] : 0 > 1
112- while 1 - 1 > -1
113- while 0 - 1 > -1
114- if arr[1] > arr[2] : 1 > 1
115- while 2 < Length of arr (12)
116- if arr[2 - 1] > arr[2] : 1 > 1
117- while 2 - 1 > -1
118- if arr[1 - 1] > arr[1] : 0 > 1
119- while 1 - 1 > -1
120- while 0 - 1 > -1
121- if arr[2] > arr[3] : 1 > 1
122- while 3 < Length of arr (12)
123- if arr[3 - 1] > arr[3] : 1 > 1
124- while 3 - 1 > -1
125- if arr[2 - 1] > arr[2] : 1 > 1
126- while 2 - 1 > -1
127- if arr[1 - 1] > arr[1] : 0 > 1
128- while 1 - 1 > -1
129- while 0 - 1 > -1
130- if arr[3] > arr[4] : 1 > 2
131- while 4 < Length of arr (12)
132- if arr[4 - 1] > arr[4] : 1 > 2
133- while 4 - 1 > -1
134- if arr[3 - 1] > arr[3] : 1 > 1
135- while 3 - 1 > -1
136- if arr[2 - 1] > arr[2] : 1 > 1
137- while 2 - 1 > -1
138- if arr[1 - 1] > arr[1] : 0 > 1
139- while 1 - 1 > -1
140- while 0 - 1 > -1
141- if arr[4] > arr[5] : 2 > 3
142- while 5 < Length of arr (12)
143- if arr[5 - 1] > arr[5] : 2 > 3
144- while 5 - 1 > -1
145- if arr[4 - 1] > arr[4] : 1 > 2
146- while 4 - 1 > -1
147- if arr[3 - 1] > arr[3] : 1 > 1
148- while 3 - 1 > -1
149- if arr[2 - 1] > arr[2] : 1 > 1
150- while 2 - 1 > -1
151- if arr[1 - 1] > arr[1] : 0 > 1
152- while 1 - 1 > -1
153- while 0 - 1 > -1
154- if arr[5] > arr[6] : 3 > 3
155- while 6 < Length of arr (12)
156- if arr[6 - 1] > arr[6] : 3 > 3
157- while 6 - 1 > -1
158- if arr[5 - 1] > arr[5] : 2 > 3
159- while 5 - 1 > -1
160- if arr[4 - 1] > arr[4] : 1 > 2
161- while 4 - 1 > -1
162- if arr[3 - 1] > arr[3] : 1 > 1
163- while 3 - 1 > -1
164- if arr[2 - 1] > arr[2] : 1 > 1
165- while 2 - 1 > -1
166- if arr[1 - 1] > arr[1] : 0 > 1
167- while 1 - 1 > -1
168- while 0 - 1 > -1
169- if arr[6] > arr[7] : 3 > 3
170- while 7 < Length of arr (12)
171- if arr[7 - 1] > arr[7] : 3 > 3
172- while 7 - 1 > -1
173- if arr[6 - 1] > arr[6] : 3 > 3
174- while 6 - 1 > -1
175- if arr[5 - 1] > arr[5] : 2 > 3
176- while 5 - 1 > -1
177- if arr[4 - 1] > arr[4] : 1 > 2
178- while 4 - 1 > -1
179- if arr[3 - 1] > arr[3] : 1 > 1
180- while 3 - 1 > -1
181- if arr[2 - 1] > arr[2] : 1 > 1
182- while 2 - 1 > -1
183- if arr[1 - 1] > arr[1] : 0 > 1
184- while 1 - 1 > -1
185- while 0 - 1 > -1
186- if arr[7] > arr[8] : 3 > 4
187- while 8 < Length of arr (12)
188- if arr[8 - 1] > arr[8] : 3 > 4
189- while 8 - 1 > -1
190- if arr[7 - 1] > arr[7] : 3 > 3
191- while 7 - 1 > -1
192- if arr[6 - 1] > arr[6] : 3 > 3
193- while 6 - 1 > -1
194- if arr[5 - 1] > arr[5] : 2 > 3
195- while 5 - 1 > -1
196- if arr[4 - 1] > arr[4] : 1 > 2
197- while 4 - 1 > -1
198- if arr[3 - 1] > arr[3] : 1 > 1
199- while 3 - 1 > -1
200- if arr[2 - 1] > arr[2] : 1 > 1
201- while 2 - 1 > -1
202- if arr[1 - 1] > arr[1] : 0 > 1
203- while 1 - 1 > -1
204- while 0 - 1 > -1
205- if arr[8] > arr[9] : 4 > 100
206- while 9 < Length of arr (12)
207- if arr[9 - 1] > arr[9] : 4 > 100
208- while 9 - 1 > -1
209- if arr[8 - 1] > arr[8] : 3 > 4
210- while 8 - 1 > -1
211- if arr[7 - 1] > arr[7] : 3 > 3
212- while 7 - 1 > -1
213- if arr[6 - 1] > arr[6] : 3 > 3
214- while 6 - 1 > -1
215- if arr[5 - 1] > arr[5] : 2 > 3
216- while 5 - 1 > -1
217- if arr[4 - 1] > arr[4] : 1 > 2
218- while 4 - 1 > -1
219- if arr[3 - 1] > arr[3] : 1 > 1
220- while 3 - 1 > -1
221- if arr[2 - 1] > arr[2] : 1 > 1
222- while 2 - 1 > -1
223- if arr[1 - 1] > arr[1] : 0 > 1
224- while 1 - 1 > -1
225- while 0 - 1 > -1
226- if arr[9] > arr[10] : 4 > 100
227- while 10 < Length of arr (12)
228- if arr[10 - 1] > arr[10] : 4 > 100
229- while 10 - 1 > -1
230- if arr[9 - 1] > arr[9] : 4 > 4
231- while 9 - 1 > -1
232- if arr[8 - 1] > arr[8] : 3 > 4
233- while 8 - 1 > -1
234- if arr[7 - 1] > arr[7] : 3 > 3
235- while 7 - 1 > -1
236- if arr[6 - 1] > arr[6] : 3 > 3
237- while 6 - 1 > -1
238- if arr[5 - 1] > arr[5] : 2 > 3
239- while 5 - 1 > -1
240- if arr[4 - 1] > arr[4] : 1 > 2
241- while 4 - 1 > -1
242- if arr[3 - 1] > arr[3] : 1 > 1
243- while 3 - 1 > -1
244- if arr[2 - 1] > arr[2] : 1 > 1
245- while 2 - 1 > -1
246- if arr[1 - 1] > arr[1] : 0 > 1
247- while 1 - 1 > -1
248- while 0 - 1 > -1
249- if arr[10] > arr[11] : 5 > 100
250- while 11 < Length of arr (12)
251- if arr[11 - 1] > arr[11] : 5 > 100
252- while 11 - 1 > -1
253- if arr[10 - 1] > arr[10] : 4 > 5
254- while 10 - 1 > -1
255- if arr[9 - 1] > arr[9] : 4 > 4
256- while 9 - 1 > -1
257- if arr[8 - 1] > arr[8] : 3 > 4
258- while 8 - 1 > -1
259- if arr[7 - 1] > arr[7] : 3 > 3
260- while 7 - 1 > -1
261- if arr[6 - 1] > arr[6] : 3 > 3
262- while 6 - 1 > -1
263- if arr[5 - 1] > arr[5] : 2 > 3
264- while 5 - 1 > -1
265- if arr[4 - 1] > arr[4] : 1 > 2
266- while 4 - 1 > -1
267- if arr[3 - 1] > arr[3] : 1 > 1
268- while 3 - 1 > -1
269- if arr[2 - 1] > arr[2] : 1 > 1
270- while 2 - 1 > -1
271- if arr[1 - 1] > arr[1] : 0 > 1
272- while 1 - 1 > -1
273- while 0 - 1 > -1
274- while 12 < Length of arr (12)
275- while (mid=0) > 0
Number of Comparings is: 275
[0, 1, 1, 1, 2, 3, 3, 3, 4, 4, 5, 100]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM