Data Structure and Algorithm Chapter 2
Data Structure and Algorithm Chapter 2
Sorting Algorithms:
Sorting refers to arranging data in a particular format.
Sorting algorithm specifies the way to arrange data in a particular
order.
Most common orders are in numerical order.
The importance of sorting lies in the fact that data searching can be
optimized to a very high level, if data is stored in a sorted manner.
Sorting is also used to represent data in more readable formats.
Example Sorting Algorithms:-
Bubble Sort
Insertion Sort
Selection Sort
Bubble Sort Algorithm
Bubble Sort is a simple algorithm which is used to sort a given set of
n elements provided in a form of an array with n number of elements.
Bubble Sort compares all the element one by one and sort them based
on their values.
If the given array has to be sorted in ascending order, then bubble sort
will start by comparing the first element of the array with the second
element, if the first element is greater than the second element, it will
swap both the elements, and then move on to compare the second and
the third element, and so on.
If we have total n elements, then we need to repeat this process for n-1
times.
Bubble Sort Algorithm
Pseudo code:
1) Starting with the first element(index = 0), compare the current
element with the next element of the array.
2) If the current element is greater than the next element of the array,
swap them.
3) If the current element is less than the next element, move to the next
element. Repeat Step 1.
Example:- Let's consider an array with values {5, 1, 6, 2, 4, 3}
• Ex:
• After the first iteration, 6 is placed at the last index, which is the
correct position for it, Similarly after the second iteration, 5 will be at
the second last index, and so on.
Selection Sort Algorithm
Selection sort is conceptually the most simplest sorting algorithm.
This algorithm will first find the smallest element in the array and
swap it with the element in the first position, then it will find the
second smallest element and swap it with the element in the second
position, and it will keep on doing this until the entire array is
sorted.
It is called selection sort because it repeatedly selects the next-
smallest element and swaps it into the right place.
How Selection Sort Works?
Following are the steps involved in selection sort(for sorting a given
array in ascending order)-Pseudo Code:
1.Starting from the first element, we search the smallest element in the
array, and replace it with the element in the first position.
2.We then move on to the second position, and look for smallest element
present in the subarray, starting from index 1, till the last index.
3.We replace the element at the second position in the original array, or
we can say at the first position in the subarray, with the second smallest
element.
4.This is repeated, until the array is completely sorted.
• Ex: Below, we have a pictorial representation of how selection sort
will sort the given array.
Insertion Sort Algorithm
Insertion sort is a simple sorting algorithm that builds the final sorted array
(or sorted list) one item at a time.
Consider you have 10 cards out of a deck of cards in your hand. And they are
sorted, or arranged in the ascending order of their numbers.
If I give you another card, and ask you to insert the card in just the right
position, so that the cards in your hand are still sorted. What will you do?
Well, you will have to go through each card from the starting or the back and
find the right position for the new card, comparing it's value with each card.
Once you find the right position, you will insert the card there.
Similarly, if more new cards are provided to you, you can easily repeat the same
process and insert the new cards and keep the cards sorted too.
This is exactly how insertion sort works. It starts from the index 1(not 0), and
each index starting from index 1 is like a new card, that you have to place at the
right position in the sorted subarray on the left.
How Insertion Sort Works?
Following are the steps involved in insertion sort:
1. We start by making the second element of the given array, i.e. element at
index 1, the key. The key element here is the new card that we need to add
to our existing sorted set of cards(remember the example with cards
above).
2. We compare the key element with the element(s) before it, in this case,
element at index 0:
3. If the key element is less than the first element, we insert the key element
before the first element.
4. If the key element is greater than the first element, then we insert it after
the first element.
5. Then, we make the third element of the array as key and will compare it
with elements to it's left and insert it at the right position.
6. And we go on repeating this, until the array is sorted.
Insertion Sort Algorithm
• Example:
• Ex:
Summery-How Insertion Sort Works
As you can see in the diagram above, after picking a key, we start
iterating over the elements to the left of the key.
We continue to move towards left if the elements are greater than the
key element and stop when we find the element which is less than the
key element.
And, insert the key element after the element which is less than the
key element.
Searching Algorithms
Searching is a process of finding a value in a list of value’s. In other
words, searching is the process of locating given value position in a
list of values.
Searching Algorithm: is an algorithm that that programmers develop
to locate the position of a value among a list of items.
Searching algorithm can be:
Linear Search Algorithm (Sequential Search Algorithm)
Binary Search Algorithm
Linear Search Algorithm (Sequential Search Algorithm)
•
•
Binary Search
The binary search algorithm can be used with only a sorted list of elements.
That means the binary search is used only with a list of elements that are already
arranged in an order.
The binary search can not be used for a list of elements arranged in random order.
This search process starts comparing the search element with the middle element in
the list.
If both are matched, then the result is "element found". Otherwise, we check whether
the search element is smaller or larger than the middle element in the list. If the search
element is smaller, then we repeat the same process for the left sublist of the middle
element.
If the search element is larger, then we repeat the same process for the right sublist of
the middle element.
We repeat this process until we find the search element in the list or until we left with
a sublist of only one element. And if that element also doesn't match with the search
element, then the result is "Element not found in the list".
Binary search is implemented using following steps-Pseudo code
Step 1 - Read the search element from the user.
Step 2 - Find the middle element in the sorted list.
Step 3 - Compare the search element with the middle element in the sorted list.
Step 4 - If both are matched, then display "Given element is found!!!" and terminate the
function.
Step 5 - If both are not matched, then check whether the search element is smaller or
larger than the middle element.
Step 6 - If the search element is smaller than middle element, repeat steps 2, 3, 4 and 5 for
the left sublist of the middle element.
Step 7 - If the search element is larger than middle element, repeat steps 2, 3, 4 and 5 for
the right sublist of the middle element.
Step 8 - Repeat the same process until we find the search element in the list or until sublist
contains only one element.
Step 9 - If that element also doesn't match with the search element, then display "Element
is not found in the list!!!" and terminate the function.
• Example: Consider the following list of elements and the element to
be searched...
•A
Assignment 1:
1) List and explain applications of Data Structure and Algorithm.
2) Assume you have different machines, then how you can calculate
space and time complexity of an algorithm on each machines?
3) What is the advantage of calculating time and space complexity of
an algorithm?
4) Explain advantages of searching and sorting algorithms in detail?
See you next