Professional Documents
Culture Documents
Unit-2.searching - Sorting Introduction
Unit-2.searching - Sorting Introduction
SORTING TECHNIQUES
UNIT-2
Searching Algorithms are designed to check for an element or retrieve an element from any
data structure where it is used. Based on the type of operations these algorithms are generally
classified into two categories:
Sequential Search: The Sequential Search is the basic and simple Searching Algorithm. Sequential
Search starts at the beginning of the list or array. It traversed the list or array sequentially and checks
for every element of the list or array. The Linear Search is an example of the Sequential Search.
A Linear Search checks one by one each element of the array, without jumping to any item. It searches
the element in the array until a match is found. If the match is found then it returns the index of the
item otherwise it returns the -1. The worst-case complexity of the Linear Search Algorithm is O(N),
where N is the total number of elements in the list. How Linear Search works:
Let’s understand with an example of how linear search works. Suppose, in this example, the task is to search an
element x in the array. For searching the given element, start from the leftmost element of the array and one by
one compare x with each element of the array. If the x matches with the an element it returns the index otherwise
it returns the -1. Below is the image to illustrate the same:
5.1 Searching
Searching is an operation or a technique that helps finds the place of a given element or value in the list. Any search is
said to be successful or unsuccessful depending upon whether the element that is being searched is found or not. Some
of the standard searching technique that is being followed in the data structure is listed below:
1. Linear Search or Sequential Search
2. Binary Search
5.1.1 Linear Search
Linear search is a very basic and simple search algorithm. In Linear search, we search an element or value in a given
array by traversing the array from the starting, till the desired element or value is found. Linear Search is applied on
unsorted or unordered lists, when there are fewer elements in a list. It compares the element to be searched with all the
elements present in the array and when the element is matched successfully, it returns the index of the element in the
array, else it returns -1.
Example: Consider the following list of elements and the element to be searched.
Index 0 1 2 3 4 5 6
List 12 23 56 34 11 76 63
Search element = 11
Step 1: Search element (11) is compared with first element (12)
Index 0 1 2 3 4 5 6
List 12 23 56 34 11 76 63
11
Both are not matching. So move to next element.
Step 2: Search element (11) is compared with next element (23)
Index 0 1 2 3 4 5 6
List 12 23 56 34 11 76 63
11
Both are not matching. So move to next element.
Step 3: Search element (11) is compared with next element (56)
Index 0 1 2 3 4 5 6
List 12 23 56 34 11 76 63
11
Both are not matching. So move to next element.
Step 4: Search element (11) is compared with next element (34)
Index 0 1 2 3 4 5 6
List 12 23 56 34 11 76 63
11
The arrangement of data in a preferred order is called sorting in the data structure. By sorting data, it is easier
to search through it quickly and easily.
The simplest example of sorting is a dictionary. Before the era of the Internet, when you wanted to look up a
word in a dictionary, you would do so in alphabetical order. This made it easy.
sorting greatly improves the efficiency of searching. If we were to open a phone book, and find that the names
were not presented in any logical order, it would take an incredibly long time to look up someone’s phone
number the same panic an engineer will go through if their data is not sorted and structured.
Searching Algorithms are designed to check for an element or retrieve an element from any data
structure where it is stored.
GENERAL SORT CONCEPTS SORT ORDER
Sorting refers to arranging data in a particular format. Sorting algorithm specifies the way to arrange data in a
particular order.
Increasing Order
A sequence of values is said to be in increasing order, if the successive element is greater than the previous one.
For example, 1, 3, 4, 6, 8, 9
Decreasing Order
A sequence of values is said to be in decreasing order, if the successive element is less than the current one.
For example, 9, 8, 6, 4, 3, 1
INTERNAL & EXTERNAL SORTING
External sorting: If the input data is such that it cannot be adjusted in the memory entirely at once, it needs to
be stored in a hard disk, floppy disk, or any other storage device. This is called external sorting.
merge sort.
STABLE & NON STABLE SORTING
The stability of a sorting algorithm is concerned with how the algorithm treats equal (or repeated) elements.
Stable sorting algorithms preserve the relative order of equal elements, while unstable sorting algorithms don’t.
In other words, stable sorting maintains the position of two equals elements relative to one another.
Stable sorting maintains the order of the two equal balls numbered 20, whereas unstable sorting may invert the
relative order of the two 8s.
Stable sorting
So if we sort students
dataset of Student If we sort this data If on the other hand we
section wise too. But in
Names and their according to name only, used a stable sorting
doing so, if the sorting
respective class sections. then it is unsorted by algorithm, the result
algorithm is not stable, we
sections. would be-
may get
When equal elements are indistinguishable, such as with integers, or more generally, any data where the entire
element is the key, stability is not an issue.
Stability is also not an issue if all keys are different.
Algorithms are stable by nature: Bubble Sort, Insertion Sort, Merge Sort, Count Sort
Algorithms unstable by nature: Quick Sort, Heap Sort, and Selection Sort
A simple steps is to do linear search, i.e
1. Start from the leftmost element of a[] and one by one compare
m with each element of a[]
2. If m matches with an element, set flag=1 and return.
3. If m doesn’t match with any of elements, set flag=0.
LINEAR SEARCH
FUNCTION CODE FOR LINEAR SEARCH
THANK YOU