Arrays are important data structures in programming, for these allows us to store collection of elements. Arrays can be easily manipulated. It is made easy to convert them into strings and character arrays. One of the uses of this is cryptography, where plaintexts are encrypted and decrypted back.
DataEncryption
Caesar Cipher
The Caesar cipher was used to send secret messages to protect crucial military messages. This is one of the earliest encryption scheme, and manipulation of characters are used to do this. In this simple cipher, each letter in a message is replaced by the letter that is three positions ahead in the alphabet. For example, 'A' becomes 'D', 'B' becomes 'E', and so on. This process continues until 'W', which wraps around to 'Z', and 'X' wraps around to 'A'.
To implement the Caesar cipher, arrays can be used for both encryption and decryption. Uppercase characters, for example, can be used as indices by subtracting the Unicode value of 'A'. This approach simplifies the process of mapping characters to their encrypted or decrypted counterparts. .toCharArray() is used to be able to return an array with the same characters inputed by the user. For encryption, replace each letter 'i' with the letter at position '(i + 3).
Sorting an Array
Arrays play a crucial role in various sorting algorithms. One simple sorting algorithm to understand is the Insertion Sort.
Insertion Sort Algorithm
Insertion Sort works by maintaining a "sorted" portion of the array and repeatedly inserting elements from the unsorted portion into their correct positions within the sorted portion. It is a straightforward algorithm, especially when visualized as if you were sorting a deck of cards.
This depicts the progression of the insertion-sort algorithm applied to an array containing characters. The illustration highlights the sorted portion of the array in white, while the character being currently inserted into the sorted section is shaded. Additionally, the character on the left, stored in the variable, is emphasized. Each row in the figure corresponds to one iteration of the outer loop, and each duplicate array within a row represents an iteration of the inner loop. Comparisons between elements are depicted with connecting arcs, and it is indicated whether each comparison resulted in a movement or not. No swap is being made here. This works if the array is not in order.
Stack
Stack Data Structure
A stack is a fundamental data structure that follows the Last-In-First-Out (LIFO) principle. In a stack, elements are inserted and removed from the top (the most recently inserted element). Stacks are used in various applications, including web browsers' navigation history and text editors' undo functionality.
Implementing a Stack
There are different ways to implement a stack, but two common methods are using an array-based implementation and a linked list-based implementation.
Array-Based Stack
• Stores elements in an array.
• Requires specifying a fixed capacity.
• Efficient for scenarios with a known maximum size.
• Operations like push and pop are typically O(1) in time complexity.
Adding a capacity is crucial. In practice, an application might require significantly less memory than the inputed capacity, resulting in wasted resources. Conversely, some applications might need more space than this predefined capacity allows, triggering an exception in our stack implementation when a client program attempts to add an exceeding object to the stack capacity. Therefore, the array-based stack implementation may not always be the optimal choice.
Linked List-Based Stack
• Uses a singly linked list to store elements.
• Dynamically adjusts to the number of elements.
• Does not have a fixed capacity.
• All operations, including push and pop, are typically O(1) in time complexity.
Used Methods
The common used methods on the Stack Implementation are:
• Push – adds an element to the top of the stack.
• Pop – removes and returns the element at the top of the stack.
• Top - retrieves the element at the top of the stack without removing it.
• Size - determine the number of elements or items currently present in a stack.
• isEmpty - This operation checks if the stack is empty, meaning it contains no elements.
Posted using Honouree