5 Minutes Or 5 Hours?
Total Page:16
File Type:pdf, Size:1020Kb
Thomas E. Doyle 5222 Big Bow Road Madison Wl 53711 5 Minutes or 5 Hours? sorting techniques compared n an attempt to help justify from this electronic marvel. Ithe purchase of a floppy-disk Nothing happened. Time Required to Sort N Items (seconds) system, I decided to put the Taking advantage of the N Ripple Modified Bubble S-M computer to some practical pause and the presence of a use. It seems that not everyone captive audience, I discussed 50 61 43 33 9 considers piloting the Enter- the advantages of adding a 100 245 173 130 21 prise and destroying Klingons disk to the wonderful com- 150 552 390 290 36 as a useful function worthy of puter. At the end of my rather 300 - 1224 85 another kilobuck investment. lengthy discussion there was Number of Swaps of Entries Using the system to keep track still nothing on the printer. As of household expenses seemed time wore on, I began to con- N Ripple Modified Bubble S-M to be a good place to start. The sider the possibilities: hard- 50 1225 1225 1225 105 Do-AII program by Randy Miller ware problems, software prob- 100 4950 4950 4950 260 {Kilobaud, August 1977) provid- lems or simply another exam- 150 11175 11175 11175 425 ed an ideal program. ple of Murphy's Law. I felt there 300 44850 1000 After the program was load- must be something wrong. Number of Entry Comparisons ed, a list of about a hundred After all, the Enterprise could items was entered for my move across the entire galaxy N Ripple Modified Bubble S-M demonstration of the practical in only seconds, so alphabetiz- 50 2450 1225 1225 263 advantages of a home com- ing this list could not take that 100 9900 4950 4950 668 puter. Everyone gathered for long. Trying to remain cool, I 150 22350 11175 11175 1187 the show, and the program was suggested that we leave the 300 44850 2812 - run. A command was given to computer and come back when sort the list of data alphabeti- it was done. Table 1. cally. Everyone stared at the Much to my suprise, thirty printer waiting for the output minutes later the sorting was 5 REM --- RIPPLE SORT --- 5 REM --- MODIFIED RIPPLE SORT 6 REM --- SET UP ARRAY --- 6 REM SET UP ARRAY —- 10 Nil 50 10 Nil 50 20 DIM D(N) 20 DIM D(N) 50 30 JiN A0 FOR 1:1 TO N 40 FOR 1:1 TO N 50 D(I)ij 50 D(I) 60 J:J-1 60 J=J-1 70 NEXT 70 NEXT 80 PRINT "*" 80 PRINT "*" 90 REM --- START OF SORT --- 90 REM START OF SORT 100 M:N 100 M:N 105 Cr0 110 C:0 110 FOR Irl TO M-1 112 M:M-1 120 CMrCM+1 11 5 IF M:0 THEN 300 130 IF D(I)<rD(I+l) THEN 160 120 FOR 1:1 TO M 135 SW=SW+1 125 CM:CMf 1 1 40 T:D(I): D(I) iD(I + n : D( I+l ) :T 130 IF DCn <:D(I + 1 ) THEN 160 1 50 cn 135 SW:SW+1 1 60 NEXT I 1 40 T:D(I): D(I) :D( I-i-l ): D( I+l) :T 1 70 IF C:1 THEN 105 1 50 C:1 300 REM --- PRINT RESULTS --- 1 60 NEXT I 31 0 PRI NT "SWI TCHES ;SW 170 IF C:1 THEN 1 I 0 320 PRI NT " COMPARISONS i" ;C M 300 REM --- PRINT RESULTS --- 330 PRI NT "SIZE ; N 310 PRI NT "SWITCHES :" ;SW OK 320 PRINT "COMPARISONS :" ;C M 330 PRINT "SIZE -" :N OK Program A. Program B. complete. The printout re- are shown in Table 1. The vealed that the list had been results of this test revealed two 5 REM BIBBLE SORT --- sorted exactly as requested. things: The bubble sort was a 6 REM SET UP ARRAY --- What could have caused the little faster than the others, and 10 N:150 delay? Perhaps my 8080 v\/as sorting takes a lot of time. Sort- 20 DIM DCN) slow. The benchmark programs ing a simple table of 100 30 J:N 40 FOR 1:1 TO N in the basic timing com- numbers took almost three 50 D( I) :J parisons article {Kilobaud, minutes. No wonder the Do-AII 60 J:J-1 June 1977) were run and re- program took so long. 70 NEXT vealed that my computer ran a None of the common sorting 80 PRINT "*" little faster than the one used methods described in Mr. 90 REM --- START OF SORT --- for the article. Rerko's article would speed up 130 M:N Since the program ran prop- a sorting program significantly. 110 FOR 1:1 TO M-1 erly and the computer was up The solution to the problem, if 120 FOR J:I + 1 TO M to speed, the solution to the any, would lie in an uncommon 125 CM:CM+1 130 problem must be in the sorting sorting routine. An article by IF D(I)<:D(J) THEN 170 135 SW:SW+1 technique used in the program. John P. Grillo (Creative Com- 1 40 T:D(I):D(I):D(J): D(J):T An article on sorting routines puting, November 1976) dis- 170 NEXT J by Andrew J. Rerko (Kilobaud, cusses a technique called the 180 NEXT I April 1977) was consulted and Shell-Metzner Sort. This 300 REM --- PRI NT RESULTS --- some test programs (Programs method offered significant 310 PRI NT "SWI TCHES :" ;SW A, B and C) were run using the speed advantages when sort- 320 PRI NT " COMPARISONS :" ;C M Ripple, Modified Ripple and ing large amounts of data. A 330 PRI NT "SIZE -" ; N Bubble routines described in flowchart of the Shell-Metzner OK the article. Sort is shown in Fig. I.The arti- The test programs consisted cle stated that a projected sort Program C. of setting up an array of N of 10,000,000 items would take numbers in reverse order and 93 years using a bubble sort. using each of the sorting Using the S-M technique, sort- routines to sort them. The pro- ing the same data would re- quire only 2.5 days. But would it gram execution times as well gram was run using the S-M eight times faster than the bub- help when sorting small as number of comparisons and method and is shown in Pro- ble sort and over 15 times faster amounts of data? the number of element switch- gram D. When sorting 150 than a ripple sort. The bubble es were recorded. The results The benchmark sorting pro- items, the S-M sort was over sort required over 20 minutes to sort 300 items. The S-M method only one or two as other sorting 8080 system with a 2 MHz clock required only 85 seconds to methods. Following the exam- and zero wait states. Mits 8K sort the same list. The speed ple benchmark program, it BASIC (Version 3.2) was used. advantage of the S-M sort in- should be possible to use the Variable CM was used to total creases dramatically with the S-M technique in other sorting the number of comparisons be- size of the list, but it seemed to programs. tween table entries. The vari- speed sorts of even small lists. able SW was used to total the The next step was to incor- Notes on Programs number of switches between porate the S-M sort technique All programs were run on an table entries.! into the Do-AII program and try it out. A random list of 100 en- tries was prepared and sorted by the standard program. Al- most 45 minutes were required LIST 4050 to sort this list. The Do-AII pro- gram was then modified to use 4050 MrP the S-M sort. Sorting the same 4055 M:INT(M/2) list of 100 entries now required 4060 IF M:0 THEN 1 1 40 less than nine minutes. To 4065 J:| t K:(P-1 ) -M modify the Do-AII program, 4070 l3j remove lines 4050-4115, 4075 L=I+M 4150-4280, 9220-9340 and 4080 IF N( T,I) <:N( T,L) THEN 4105 replace with the new lines 408 5 GOSUB 9210 4090 I:I-M shown in Progam E. 409 5 IF I<1 THEN 4105 The only disadvantage I have 4100 GOTO 407 5 found with the S-M technique 4105 JrJ-H so far is that it does require 41 10 IF J>K THEN 4055 slightly more code, and it uses 41 15 GOTO 4070 Fig. 1. Shell-Metzner Sort. five index variables rather than BR EAK OK LIST 41 50 41 50 MrP 5 REM --- SHELL METZNER SORT 4160 M:INT(n/2) 6 REM --- SET UP ARRAY —- 4170 IF Mr0 THEN 1 1 40 10 N:300 4180 J:1 : K:(P-I ) -M 20 DIM D(N) 4190 I:J 30 J:N 4200 L:I+M 40 FOR 1:1 TO N 4210 IF A$( T,I) <:A$( T, L) THEN 42 60 50 D(I)=J 4220 GOSUB 9210 60 J:J-l 4230 I:I-M 70 NEXT 42 40 IF I<1 THEN 42 60 S0 PRINT 42 50 GOTO 42 00 90 REW START OF SORT 42 60 J:J+1 120 M:N 4270 IF J>K THEN 41 60 • 110 MrINT(M/2) 42 8 0 GOTO 4190 120 IF M:0 THEN 300 BR EAK 130 J:1 : K:N-M OK »1 40 I r J LIST 922 0 • 150 L:I + M 1 55 CMrCM+1 9220 XI :N( 1 ,L) 160 IF D(I)<D(L) THEN 210 92 3 0 X2=N(2,L) 170 TrDdl: D(n :D(L): D(L) :T 92 40 81$:A$(I ,L) 175 SW:SW-H 92 50 B2$rA$(2 ,L) 1«0 I=I-M 92 60 FOR Z:1 TO 2 1'90 IF I<1 THEN 210 92 7 0 N(Z,L) :N(Z,I) 200 GOTO 1 50 9280 A$(Z,L) :A$(Z,I) • 210 J = J+1 92 9 0 NEXT 220 IF J>K THEfJ 112 9300 N(1,1):X1 230 GOTO 1 40 9310 N(2,1):X2 •300 REM PRINT RESULTS --- 9320 A$(l ,n :B1 $ 310 PRINT "SWITCHF^ r" :5W 9330 A$(2,I)3B2$ 320 PRINT "COMPARISONS r" ;C M 9340 RETURN 330 PRINT "SIZE ;N BR EAK OK OK Program D.