Data Structures: Searching and Sorting
In the computer system, the large amount of data is stored. The data contain records, and it has to be retrieved as per the searching criterion. If we need effective and efficient storage of the data, we need facilitation to do the fast searching. It has a lot of significance in the system. There are two search algorithms which are used for searching the individual and the required records.
Under this search, the various elements of the given array are compared for searching an item. This is done in a one to one manner. Take a graph in which there are n elements, and they can either be sorted or unsorted. In that graph, we have to find the required element say ‘I’ from the array. The first and the common technique which can be used here is the comparison of the element I with every other element of the array in the one to one format. This is called the sequential search or the linear search. Here, the array is traversed to search the item in a sequence.
This is the algorithm which works on the sorted array only. It is called the binary search. For example, we have an array which has been sorted in the ascending order. For the location of the element in the array, in the binary search, the task begins with the comparison of the element which is needed to be located is done in the middle term. We return immediately when we find the match with the needed element.
In the ascending order of the array, when we get the match before the middle term, then the element has to be less than the middle key. If the value of the required element is higher than the middle key, then it would lie in the upper half of the array. Similarly, we would repeat the process every time we need to search for an element in the array.
The arrangement of the information, as well as the data in the computer system and that too on the basis of the important function, is a process, and the logical form is called as sorting.
The nature of the logic in the sorting can either be ascending or descending in the numerical case and the alphanumeric values, it can be sorted in the form of a dictionary. When we have a list of elements which are to be sorted in the ascending order, then it is a term which refers to the task of the rearrangement of the list’s content in order to get the list in the increasing order.
There are various algorithms to sort the list of the data
When we have a list of the numbers to be sorted in the ascending order, then the operation begins with the comparison of the first and the second item in the list. After this, they have to be arranged in the form that the second element is greater than the first. Similarly, do the operation with the second and the third item of the list so that the third is greater than the second. Do it until you reach the end of the list. When we are done with this, the last element of the list would have the largest element. This comes as the first pass.
In the second pass, until the right position has the largest element, we need to repeat the process for the second-last position. It will end up when the second largest element of the array would be at the second-last position.
Repeat the process until you get the sorted list. The number of passes to be done here are one less than the number of elements in the list.
To sort the elements with the help of the selection method, you need to find the minimum value of the element in the array. Then you need to exchange the value of the first position to the element. In addition to this, you need to find the element with the smallest value from the leftover elements of the array. After this, you need to interchange both the values.
There is a division of the list in two further sub-lists. The first is the sorted list, and the other is unsorted. In the first list of elements, we have the first element of the list, and the unsorted list contains the rest of the elements. Till the list has no elements in it, there is an insertion of one of the elements from the unsorted list to the sorted one. It has to be done in a proper position.
Under this, the original file is broken down as its elements are distributed into various and isolated subfiles. After the distribution, the subfiles are tending to be sorted prior to the elements in the original file.
MERGING OF THE TWO ARRAYS WHICH ARE SORTED
It is the process of the combination of the array elements into the third array is called merging of the two sorted arrays.