Simple sorting algorithms such as insertion sort algorithms are much less efficient on large data sets because it sorts the array one entry at a time in a linear manner (Tang 2012). Even though insertion sorting algorithms are overall better than selection sort or bubble sort as mentioned previously, it still results in a logarithmic performance of just 0(n2) as revealed through the works of Mishra & Garg (2008). Therefore, its performance is lower than that of divide and conquer algorithms which boasts a performance of 0(1) and proves that divide and conquer algorithms are much more efficient than simple sorting algorithms, even as protruded by the vast calculations of the Raford University (2016).
3.3
Explain common string operations such as concatenation, substring, trim etc., also state their practical applications. (LO 3.1)
3.3.1 Sub-Strings
A sub-string is a sequence of characters that contains in a string value. Substrings helps in separating a character or a set of characters apart from an existing string value thereby enabling to make a modified copy of the original text. The methods such as IndexOf, IndexOfAny, LastIndexOf, and LastIndexOfAny relies on substring techniques (Deitel & Deitel 2005). The following points shows how sub-strings could be achieved using the Substring method and its practical applications.
Nilshan Devinda REG: 7919 Data Structures & Algorithms Unit 34 43
Substring Method
The Substring method takes 2 arguments in integer format to identify where it should start the separation and where it should end. The Substring method does not accept the string itself as an argument, therefore the string object is required to perform the operation rather than the string text. The first 3 lines of code in figure 27 demonstrates the use of the Substring method in C# language. One practical application of this method is that certain phrases from a user’s input could be extracted to form a more relative output based on the input.
Replace Method
The Replace method also accepts 2 arguments as parameters, it is required that the first argument to be the substring which should be replaced and the second argument to be the substring to be substituted with the first argument. Lines 4 to 6 in figure 27 illustrates the Replace method in use. This method is extremely useful where certain characters are required to be replaced such as in the case of making web URL’s more readable by replacing each forward slash with commas.
IndexOf Method
The IndexOf method accepts just one argument where a character is passed in return for its index or position in the string, considering the string as an array of characters where white spaces are ignored. The last 3 lines of code in figure below shows the use of IndexOf method to return its index value. The substring method mentioned above requires the starting and ending index to be passed as arguments into the method. IndexOf method would therefore help in finding such indexes. For example, the figure below shows the method used to find the index of ‘C’ from the string s3.
Figure 31: Substring and related methods Source: Microsoft (2016).
Nilshan Devinda REG: 7919 Data Structures & Algorithms Unit 34 44
3.3.2 Concatenation
Concatenation is the process of merging two data literals into a single literal. Concatenation is not limited to string literals and could be used in any other data type such as binary, integer, floating point characters and Boolean. However, concatenation find its syntax to be different across languages (Techopedia 2016). The application of concatenation for string literals in the Java programming language is discussed below.
Even in the Java programming language itself, there are various approaches for concatenation of string values. Each approach has their effects on performance and efficiency of the code. The easiest way to achieve concatenation of strings in java is to simply merge 2 strings using the operator ‘+’ as shown in the image below.
Figure 32: Concatenation using the '+' operator Source: Author’s work.
Even though the above approach for concatenation of strings is almost pleasant and readable, the practice of using ‘+’ operator for concatenation is highly discouraged by Nakov (2013), as the action would result in various performance issues due to reduced efficiency in the process involved while the operator is being used. As a possible solution to this problem, Kulandai (2015) suggests the use of buffered strings as it would significantly reduce the time taken to complete the concatenation process. In this approach, the append method is being used as seen through the image below.
Figure 33: Concatenation of strings by append method Source: Author’s work.
Nilshan Devinda REG: 7919 Data Structures & Algorithms Unit 34 45
The above code sample shows string concatenation but with a string buffer in use where the actual result needs to be converted to a string again. However, there are alternative solutions that performs true concatenation yet without the need of string buffers. The concat method is such an approach where the string to be merged is passed as an argument into the method as shown through the image below.
Figure 34: Concatenation of strings through the concat method Source: Tutorials Point (2016).
The practical applications of concatenation include merging certain substrings which might have been extracted from the user into new string literals or in making new content from existing data such as the use of location names from maps to create readable addresses.
3.3.3 Trim Method
The string trim method in java language is used to eliminate leading and trailing white spaces from a string. It uses the Unicode value of space which is ‘\u0020’ to check whether a given string contains such a Unicode at the beginning or the end, so this method cannot omit middle spaces in the string (Java T Point 2014). A possible use of this method includes validating user inputs where the user might have accidentally typed in white spaces at the beginning or the end of the input. In such a case, the absence of omission of middle spaces in the trim method comes as an advantage, because the input from the user is not altered while validating. The image below shows how this method is being used in the java language.
Figure 35:String Trim method in java Source: Author’s work.
Nilshan Devinda REG: 7919 Data Structures & Algorithms Unit 34 46
Appendices
Appendix A: Conclusion
In the beginning of this document at Task 1, a program for figuring out whether certain strings are palindromes is developed. It uses many techniques and methods for string processing to process the input strings to validate them, for instance removal of white spaces and punctuation marks from the input string is practiced. This program uses an array based stack to reverse the string to check whether the reversed output matches the processed input. This method could be improved in the future where the array based stack could be replaced with a linked list. However, a program that uses a linked list is developed for the task 2 based scenario where the program enables processing of students in a waiting list in the form of a queue using queue operations such as enque and deque. As this is already a better implementation, this program only requires a better design such as a Graphical User Interface which would be more visually appealing than the current version which aides an Output Console from the IDE.
It is clear from the many types of algorithms presented and explained through Task 3, that every algorithm is unique and is more efficient than others on certain tasks, scenarios or conditions. For example, even though insertion sorting algorithms are less efficient than merge sorting algorithms, for data sets below 10 – 12 elements in size, insertion sorting outperforms most of other types of algorithms available including merge sort. However, it is worth noting that on average, for most cases, the Divide and Conquer algorithms are more efficient and faster than Simple Sorting algorithms as they prove a more logarithmic performance. Therefore, the use of the correct type of data structure as well as the algorithm for the specific problem is always essential for implementing faster and efficient programs.
Nilshan Devinda REG: 7919 Data Structures & Algorithms Unit 34 47
Appendix B: Self-Reflection
Though it took so long to catch up, Data Structures & Algorithms became an interesting area which helps in understanding how certain software work and behave. The different types of data structures, algorithms and multiple approaches for sorting and searching data found on this module reveals that the area studied is just the surface, and promotes the learning of much more sophisticated methods in the future. As a novice, it is always hard to understand how exactly modern software are supported using the techniques introduced in this module, however while completing this assignment, it became evident that there are vast amounts of research and experimental work dedicated for this area alone in which cases, the most efficient approaches, algorithms and methods are found and invented, specially based on their performance. However, it is a reason to worry, that such great research could not be referenced in this document.
It is a pleasure to learn about strings and how they could be processed using different methods in a programming language. It is also a pleasure learning about algorithms and where exactly specific algorithms should be used. However, it was unclear and hard to understand about certain data structures such as Graphs and Trees, and how the algorithms are used to sort data in such forms. While it was confusing how certain coding practices and techniques differ the performance of algorithms, such as in the case of recursive and non- recursive binary searching algorithms where the recursive always outperforms the iterative one, they were always promising a whole new perspective of observing the problem from which knowledge about how they actually work could be increased. These skills whether great or small would be beneficent in the future whether it would be for sorting data or for solving other problems as well. Therefore, this document would serve as a problem solver in the future as it could be used as a revision tool when the above mentioned weaknesses arrive.
Nilshan Devinda REG: 7919 Data Structures & Algorithms Unit 34 48
Appendix C: The Gantt Chart
Phases
Time Duration: 21 days [ 3 Weeks]
Week 1 Week 2 Week 3
Date 31/0 8 /1 6 01 /0 9 /1 6 02 /0 9 /1 6 03 /0 9 /1 6 04 /0 9 /1 6 05/09 /1 6 06/09 /1 6 07/09 /1 6 08/09 /1 6 09 /09 /1 6 10/09 /1 6 11 /09 /1 6 12 /09 /1 6 13 /0 9 /1 6 14 /09 /1 6 15 /0 9 /1 6 16 /09/ 1 6 17 /09 /1 6 18 /0 9 /1 6 19 /0 9 /1 6 20 /0 9 /1 6 Chapter 1 Chapter 2 Chapter 3 Finalization
Nilshan Devinda REG: 7919 Data Structures & Algorithms Unit 34 49
References
Abualrob, M. (2010) Iterative Binary search. Weblog. [Online] Available from: http://www.8bitavenue.com/2010/09/iterative-binary-search/ [Accessed 31st August 2016.
GMT 11:25:27].
Beniwal, M., Singh, A., Diwan, A., Kaushik, J., Kumar, K.N., Singh, V., Gautam, N. & Joshi, V.P. (2016) Insertion sorting. [Online] Available from: http://www.studytonight.com/data-structures/insertion-sorting [Accessed 31st August 2016].
C Programming. (2011) Linear Search, Binary Search and other searching Techniques.
[Online] Available from:
http://www.cprogramming.com/discussionarticles/sorting_and_searching.html [Accessed 01st September 2016].
Cormen, T.H., Leiserson, C.E., Rivest, R.L. & Stein, C. (2009) Introduction to Algorithms,
Third Edition. 3rd edition. [e-book] Massachusetts, MIT Press. Available from: https://mitpress.mit.edu/books/introduction-algorithms [Accessed 02nd September 2016]. Damier, I. (2015) Iterative Binary Search. [Online] Available from: http://www.geekviewpoint.com/java/search/binary_search_iterative [Accessed 03rd September 2016].
Deitel, H.M. & Deitel, P. (2005) Strings, Characters and Regular Expressions. In: C# for
Programmers. USA, Prentice Hall Professional, p. 616.
Forrest, P. (2016) What is non-recursive binary search Algorithm? Weblog. [Online]
Available from:
http://www.answers.com/Q/What_is_non_recursive_binary_search_algorithm [Accessed 04th September 2016. GMT 10:23:26].
Geeks for Geeks. (2016) Merge Sort. [Online] Available from: http://quiz.geeksforgeeks.org/merge-sort/ [Accessed 05th September 2016].
Java T Point. (2014) Java String trim. [Online] Available from: http://www.javatpoint.com/java-string-trim [Accessed 6th September 2016].
Nilshan Devinda REG: 7919 Data Structures & Algorithms Unit 34 50
Knight, V. (2013) Comparing Recursive and Iterative Algorithms: Binary Search and
Factorial. Weblog. [Online] Available from:
http://drvinceknight.blogspot.com/2013/06/im-in-middle-of-putting-together-new.html [Accessed 07th September 2016. GMT 09:15:37].
Kulandai, J. (2015) Java String Concatenation. Weblog. [Online] Available from: http://javapapers.com/core-java/java-string-concatenation/ [Accessed 08th September 2016. GMT 09:22:26].
Microsoft. (2016) Strings (C# Programming Guide). [Online] Available from: https://msdn.microsoft.com/en-us/library/ms228362(v=vs.100).aspx [Accessed 09th September 2016].
Mishra, A.D. & Garg, D. (2008) Selection of best Sorting Algorithm. International Journal
of Intelligent Information Processing, 2 (2), 363 – 368.
Nakov, S. (2013) Strings and Text processing In: Fundamentals of Computer programming
with C#: The Bulgarian C# Book. Bulgaria, Svetlin Nakov, p. 484.
Oracle Corporation (1997) PL/SQL User’s Guide and Reference Release 8.0. [Online]
Available from:
https://www.bnl.gov/phobos/Detectors/Computing/Orant/doc/database.804/a58236/06_err s.htm [Accessed 10th September 2016].
Raford University. (2016) Divide and Conquer Algorithms. [Online] Available from: http://www.radford.edu/~nokie/classes/360/divcon.html [Accessed 11th September 2016]. Rouse, M. (2007) Error Handling. Weblog [Online] Available from: http://searchsoftwarequality.techtarget.com/definition/error-handling [Accessed12th September 2016. GMT 10:14:17].
Ruehr, F. (1998) Data Structures and Algorithms: Sorting Algorithms. Presented at
Willamette University. Oregon. [Online] Available from:
http://www.willamette.edu/~fruehr/dsa/lectures/sorting1.html [Accessed 13th September
Nilshan Devinda REG: 7919 Data Structures & Algorithms Unit 34 51
Ryerson University. (2004) Sorting Algorithms. [Online] Available from: http://www.ee.ryerson.ca/~courses/coe428/sorting/insertionsort.html [Accessed 14th September 2016].
Stoimen. (2011) Computer Algorithms: Binary Search. Weblog. [Online] Available from: http://www.stoimen.com/blog/2011/12/26/computer-algorithms-binary-search/ [Accessed 15th September 2016. GMT 11:38:26].
Swartz, F. (2003) C++ Notes: Algorithms: Recursive Binary Search. [Online] Available from: http://www.fredosaurus.com/notes-cpp/algorithms/searching/rbinarysearch.html [Accessed 16th September 2016].
Tang, D. (2012) Introduction to Sorting Algorithms. [Online] Available from: https://www.cpp.edu/~ftang/courses/CS241/notes/sorting.htm [Accessed 17th September 2016].
Techopedia. (2016) Concatenation. [Online] Available from: https://www.techopedia.com/definition/3470/concatenation-programming [Accessed 18th September 2016].
Toptal. (2016) Merge Sort. [Online] Available from:
https://www.toptal.com/developers/sorting-algorithms/merge-sort [Accessed 19th September 2016].
Tripathi, P. (2014) Binary Search Implementation using C#. Weblog. [Online] Available from: http://www.c-sharpcorner.com/blogs/binary-search-implementation-using-c-sharp1 [Accessed 20th September 2016. GMT 08:22:23].
Tutorials Point. (2016) Java – String concat() Method. [Online] Available from: http://www.tutorialspoint.com/java/java_string_concat.htm [Accessed 21st September
Nilshan Devinda REG: 7919 Data Structures & Algorithms Unit 34 52
Bibliography
Bender, M.A., Colton, M.F. & Mosteiro, M.A. (2006) Insertion Sort is 0(n log n). Theory of
Computing Systems, 39 (3), 391 – 397.
Castro, V.E. & Wood, D. (1992) A Survey of Adaptive Sorting Algorithms. ACM
Computing Surveys, 24 (4), 441 – 476.
Cook, C.R. & Kim, D.J. (1980) Best sorting algorithm for nearly sorted Lists.
Communications of the ACM, 23 (11), 620 – 624.
Darlington, J. (1978) A Synthesis of several sorting Algorithms. Acta Informatica, 11 (1), 1 – 30.
Department of Computer Science. (2015) Introduction to Data Structures. CS 46B. San Jose, San Jose State University.
Franciszek, G. & Dominik, S. (2006) Dynamic Behavior of simple Insertion sort Algorithm.
Fundamenta Informaticae, 72 (1), 155 – 165.
Karunanithi, A.K. (2014) A Survey, Discussion and Comparison of Sorting algorithms. Master’s thesis. Umea University.
Mcllroy, M.D. & Bentley, J.L. (1993) Engineering a sort function. Journal of Software:
Practice and Experience, 23 (11), 1249 – 1265.
Rajasekaran, S. (2001) A Framework for Simple Sorting Algorithms on Parallel Disk Systems. Theory of Computing Systems, 34 (2), 101 – 114.
Samet, H. (1990) The Design and Analysis of Spatial Data Structures. USA, Addison- Wesley.
Sedgewick, R. (1978) Implementing Quicksort programs. Communications of the ACM. 21 (10), 847–857.
Vazirani, U., Dasgupta, S. & Papadimitriou, C. (2006) Algorithms. 1st edition. New York, Mcgraw Hill Education.