Please see the below graphical illustration!. The array is sorted, and the algorithm can terminate. The algorithm needs one whole pass without any swap to know it is sorted. Now, the array is already sorted, but our algorithm does not know if it is completed. ( 1 4 2 5 8 ) > ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not swap them. ( 5 1 4 2 8 ) > ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps them. The algorithm goes for n (length of array) n iterations across the array, and take one element in each phase The algorithm take elements from left most node to right most node and compare two adjacent elements and replace their positions if the right most element is greater than left most element of the pair. I thought I can implement a simple “bubble sort” to sort the table. The reason why I said the story is, before started with writing the code I searched a couple of times in internet to see some sample codes but internet disappointed me I couldn’t very much useful codes over there. Few days back one of my friend contacted me to get the code for sorting a table in Cobol, his requirement was very rare/unique so I had to write custom sort for his requirement, since he needs it in CICS-COBOL environment we could not use SORT verb. But trust me there can be situations in CICS-COBOL program. I can understand what you are thinking while reading the heading “Sorting table! its not required because we have sort utilities to do this”. If you are going to do a sort in code, the easiest sort procedure to implement is the Bubble Sort.Hi folks, I could see the folds on you forehead. You will also need to define a Temporary Student 05 Temp-Student Pic x(27). This both an answer and a guide to writing Cobol 01 CLASSROOM-TABLE.Īnd the procedure becomes MOVE "MICHAELA" TO STUDENT-Name (1). I don't think I'm iterating through the table correctly, but all other ways I've tried gives me errors. But when it prints the table again, everything is the same. I know that the code is getting to the sort, since it prints both inside sort and sort is done. PERFORM VARYING I FROM 1 BY 1 UNTIL I > 4 I don't think I am even implementing this table correctly, so any help on how to improve this code would be great. I am learning COBOL, and I am having difficulty trying to figure out how to sort this table.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
March 2023
Categories |