For really large sorts it might make sense to marshal parameters and data and go off to an external sort product. Perfectly doable in REXX.
Cheers, Martin Martin Packer, zChampion, Principal Systems Investigator, Worldwide Cloud & Systems Performance, IBM +44-7802-245-584 email: [email protected] Twitter / Facebook IDs: MartinPacker Blog: https://www.ibm.com/developerworks/mydeveloperworks/blogs/MartinPacker Podcast Series (With Marna Walle): https://developer.ibm.com/tv/mpt/ or https://itunes.apple.com/gb/podcast/mainframe-performance-topics/id1127943573?mt=2 From: "Joel C. Ewing" <[email protected]> To: [email protected] Date: 23/09/2017 23:29 Subject: Re: simple rexx sort Sent by: IBM Mainframe Discussion List <[email protected]> On 09/19/2017 07:54 AM, Dyck, Lionel B. (TRA) wrote: > I needed a simple sort for the words in a string and came up with this - hope it helps others. There are probably better ways to do this so feel free to share: > > /* ------------------ rexx ------------------------ * > | Simple bubble sort for words in a string (array) | > * ------------------------------------------------ */ > string = '10 0 1 9 6 2 4 3' > do imx = 1 to words(string)-1 > do im = 1 to words(string) > w1 = word(string,im) > w2 = word(string,im+1) > if w1 > w2 then do > if im > 1 > then lm = subword(string,1,im-1) > else lm = '' > rm = subword(string,im+2) > string = lm w2 w1 rm > end > end > end > say string > > > -------------------------------------------------------------------------- > Lionel B. Dyck > Mainframe Systems Programmer - TRA > > For sorting 8 values it's not going to make any difference what sort method is used, but I wish people would just stop teaching Bubble Sort to new programmers as a sorting algorithm and teach an Insertion Sort instead. An Insertion Sort is even easier to explain than a Bubble Sort, not any more difficult to program, and will always out perform a Bubble Sort, with the difference becoming more significant for larger N. Of course for really large N, you should always look at snazzier sorting techniques, but they are significantly more complex and probably not something one should attempt to write from memory. Here is an example of an Insertion Sort for similar data, implemented as a REXX Procedure on a Linux platform using oorexx. The 10K repetition loop and use of 50 rather than 8 values was just to get a measurable execution time comparison on my Linux platform. For 10K sorts of 50 values Bubble Sort took 33 seconds vs only 4 seconds for same data using the Insertion Sort. #!/usr/bin/rexx /* ------------------ rexx ------------------------ * | Simple insertion sort for words in a string (array) | * ------------------------------------------------ */ Do times = 1 To 10000 string = "10 0 200 50 11 32 54 23 13 17 35 14 77 52 92 600 21 45 501 399 27 15 22 425 301" , "7 45 65 201 41 37 39 26 19 99 87 73 61 59 95 306 29 48 555 333 22 74 85 444 222" Call Insert_Sort End Say string Exit Insert_Sort: Procedure Expose string n = Words(string) If n< 2 Then Return sorted = Word(string,1) Do i = 2 To n next = Word(string,i) Do j = 1 To i-1 /* find where to insert next value among already sorted values */ If next < Word(sorted,j) Then Do /* must insert before jth value */ If j > 1 Then lm = Subword(sorted,1,j-1) Else lm = '' sorted = lm next Subword(sorted,j) Leave j End If j = i-1 Then sorted = sorted next End End string = sorted Return -- Joel C. Ewing, Bentonville, AR [email protected] ---------------------------------------------------------------------- For IBM-MAIN subscribe / signoff / archive access instructions, send email to [email protected] with the message: INFO IBM-MAIN Unless stated otherwise above: IBM United Kingdom Limited - Registered in England and Wales with number 741598. Registered office: PO Box 41, North Harbour, Portsmouth, Hampshire PO6 3AU ---------------------------------------------------------------------- For IBM-MAIN subscribe / signoff / archive access instructions, send email to [email protected] with the message: INFO IBM-MAIN
