Chapter 2 The Last Simple Sorting & Searching Algorithm New
Chapter 2 The Last Simple Sorting & Searching Algorithm New
3
Cont…
● During the sorting process the data traversed many times.
● Each traversal of the data is referend as a Sort Pass.
4
Cont…
Importance of Sorting in Data Structure
● When you are performing sorting on elements, many
complications such as min/max, kth, smallest/largest get
automatically simplified.
● Sorting provides you with many algorithmic solutions, which
might include divide and conquer, iterative, and recursive
based.
● Last but not least, perhaps one of its biggest benefit is time
complexity. It not only saves up your very precious time but
also provides you with right solution.
5
Cont…
Types of sorting
Insertion Sort
Selection Sort
Bubble Sort etc…
6
Cont…
I. Insertion Sort
● It is a simple sorting algorithm that places an unsorted element
at its suitable place in each iteration.
● The array is virtually split into a sorted and unsorted part.
Values from the unsorted part are picked and placed at the
correct position in the sorted part.
● It inserts each item into its proper place in the final list.
● The simplest implementation requires two list structures, the
source list and the list into which sorted items are inserted.
7
Cont…
● To save memory, most implementations use an in-place sort
that works by moving the current item past the already sorted
items and repeatedly swapping it with the preceding item
until it is in place.
Basic Idea:
● Find the location for an element and move all others up, and
insert the element.
The process involved in insertion sort is as follows:
● By default, the left most value can be said to be sorted
relative to itself. Thus, we don’t need to do anything.
8
Cont…
● Check to see if the second value is smaller than the first one.
If it is, swap these two values. The first two values are now
relatively sorted.
● Next, we need to insert the third value in to the relatively
sorted portion so that after insertion, the portion will still be
relatively sorted.
● Remove the third value first. Slide the second value to make
room for insertion. Insert the value in the appropriate
position. Now the first three are relatively sorted.
● Do the same for the remaining items in the list.
9
Cont…
It is a comparison-based sorting algorithm in which the
sorted array is built by inserting one element at its correct
position at a time.
Examples lets consider an unsorted array [23, 1, 10, 5, 2] and
discuss each steps . In every pass, one element is taken from
the unsorted array and inserted it at the correct position in the
sorted array.
First Pass: The whole array is unsorted array and (23) is taken to be
inserted into the sorted array. As (23) is the first element of sorted
array and has no elements to be compared with, it remains at its
position.
10
Cont…
● Second Pass: (1) is the taken from unsorted array to insert into sorted array. It
is compared with all elements of sorted array and found that (23) is the only
number which is greater than (1). (1) is inserted into the sorted array and
position of (23) is shifted by one place in the array. This resulted into
increasing the length of sorted array by one and decreasing the length of
unsorted array by one.
● Third Pass: (10) is taken from the unsorted array and compared with all
elements of sorted array. (23) is the only number in the sorted array which is
greater than (10). (10) is inserted into the sorted array and position of (23) is
shifted by one place in the array.
● Fourth Pass: (5) is compared with all elements of sorted array and it is found
that (10) and (23) are the numbers which need to be shifted by one place to
insert (5) into the sorted array.
● Fifth Pass: To insert (2) into the sorted array, (5), (10) and (23) are shifted by
one place.
11
Cont…
12
Cont…
Pseudocode for the insertion sort algorithm is given below:
InsertionSort(data):
for each element i in data, starting from the leftmost position:
shift all previous elements that are greater than data[i]
right one position
place data[i] in its correct position
13
Cont…
Implementation
void insertion_sort(int list[]){
int temp;
for(int i=1 ; i<n ; i++){
temp=list[i];
for(int j=i; j>0 && temp<list[j-1];j--){
list[j]=list[j-1];
list[j-1]=temp;
}
}
}
14
Cont…
C++ Program to Implement Insertion Sort
#include <iostream>
using namespace std;
int main() {
int a[5]={10, 5, 91, 11, 8};
int i, j, k, temp;
for (i = 1; i < 5; i++)
for (j = i; j >= 1; j--){
if (a[j] < a[j-1]){
temp = a[j]; a[j] = a[j-1]; a[j-1] = temp;
}
}
}
15
Cont…
Time Complexity for Insertion sort
Worst Case Complexity: O(n2)
● It occurs, when array is given in ascending order, and you
want to sort it in descending order or vice versa.
Best Case Complexity: O(n)
● It occurs, when the array is already sorted.
Average Case Complexity: O(n2)
● It occurs when the elements of an array are in neither
ascending nor descending.
16
Cont…
● The array has small number of elements.
● There are only a few elements left to be sorted.
17
Cont…
II. Selection Sort
● It is a sorting algorithm that selects the smallest element from
unsorted list in each iteration and places that element at the
beginning of the unsorted list.
● First, the array is scanned to find the smallest element.
● This is swapped with the element in the first position.
● Next, the remainder of the array is scanned to find the next
smallest element.
● This is swapped with the element in the second position.
18
Cont…
● This process continues until all elements have been placed in
their correct position.
Pseudocode for the Selection sort algorithm is given below:
SelectionSort(data):
for each element i in data apart from the last
Select the smallest element from data[i]…data[n-1]
Swap it with data[i]
19
Cont…
Examples To understand the selection sort, lets consider an
unsorted array [1, 10, 23, -2] and discuss each step taken to
sort the array in ascending order. In every pass, smallest
element is found in the unsorted array and swapped with first
element of unsorted array.
First Pass: The whole array is unsorted array and (-2) is the
smallest number in the array. After finding (-2) as smallest
number, it is swapped with first element of the array.
20
Cont…
● Second Pass: In this example, the left hand side array is sorted array
and length of this array increase by one after each iteration. After first
pass length of the sorted array is one. Rest of the array is unsorted
array. (1) is the smallest number in the unsorted array which is
swapped with first element of the unsorted array. After this swap,
length of the sorted array and unsorted array will be two.
● Third Pass: (10) is the smallest number in the unsorted array and
swapped with the first element of the unsorted array.
● Fourth Pass: (23) is the only number in the unsorted array and found
to be at the correct position.
21
Cont…
22
Cont…
Implementation
void selection_sort(int list[]){
for(int i=0; i<n-1; i++)
int min=i;
for(int j=i+1; j<n; j++){
if(list[j]<list[min])
min=j;
}
temp=list[min];
list[min]=list[i];
list[i]=temp;
}
23
Cont…
C++ Program to Implement Selection Sort
#include <iostream>
using namespace std;
int main() {
int min, a[6]={10, 5, 91, 11, 8, 4};
for (int i = 0; i < 5; i++)
min = i;
for (int j = i + 1; j < 6; j++){
if (a[j] < a[min])
min = j;
}
}
24
Cont…
int temp;
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
}
25
Cont…
26
Cont…
Cycle Number of Comparison
1st (n-1)
2nd (n-2)
3rd (n-3)
...... ……..
last 1
Number of compns: (n - 1) + (n - 2) + (n - 3)+ ...+ 1 = n(n - 1)/ 2 nearly equals
to n2.
The time complexity of the selection sort is the same in all cases. At every
step, you have to find the minimum element and put it in the right place. The
minimum element is not known until the end of the array is not reached.
27
Cont…
Time Complexity for Selection sort
Worst Case Complexity: O(n2)
● It occurs, when array is given in ascending order, and you
want to sort it in descending order or vice versa.
Best Case Complexity: O(n2)
● It occurs, when the array is already sorted.
Average Case Complexity: O(n2)
● It occurs when the elements of an array are in neither
ascending nor descending.
28
Cont…
III. Bubble Sort
● Bubble sort is the simplest algorithm to implement and the
slowest algorithm on very large inputs.
● It works by repeatedly swapping the adjacent elements if they
are in wrong order.
● It starts from the first element. If the elements are in correct
order, there is no swapping condition. So, shift to the next
index, else the elements put in not correct order swap them.
29
Examples for Bubble sort
30
Cont…
Basic Idea:
● Loop through array from i=0 to n and swap adjacent elements
if they are out of order.
Pseudocode and a C++ implementation for bubble sort are
given below:
BubbleSort(data):
for (i=0; i<n-1; i++)
for (j=0; j<n-i-1; j++)
Swap elements in positions i and j if out of order
31
Cont…
Implementation
void bubble_sort(list[]) {
for(int i=0;i<n-1; i++){
for(int j=0;j<n-i-1; j++){
if(list[j]>list[j+1){
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
}
32
Cont…
C++ Program to Implement Bubble Sort
#include <iostream>
int main() {
int temp, a[6]={10, 5, 91, 11, 8, 4};
for(int i = 0; i < 5; i++)
for(int j=0; j<6-i-1; j++){
if (a[j] < a[j+1]){
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
33
Cont…
std::cout<<"The bubble sorted array :"<<endl;
for(int i=0; i<6; i++) {
cout<<a[i]<<“ ”;
}
}
OUTPUT
The bubble sorted array :
4 5 8 10 11 91
34
Cont…
Time Complexity for Bubble sort
Worst Case Complexity: O(n2)
● It occurs, when array is given in ascending order, and you
want to sort it in descending order or vice versa.
Best Case Complexity: O(n)
● It occurs, when the array is already sorted.
Average Case Complexity: O(n2)
● It occurs when the elements of an array are in neither
ascending nor descending.
35
2.2 – Simple Searching Algorithms
● In computer science, searching refers to the process of finding
a particular data item (such as a value or a record) in a data
structure such as an array, a linked list or a tree.
● Searching is the process of finding some particular element in
the list.
● If the element is present in the list, then the process is called
successful and the process returns the location of that element,
otherwise the search is called unsuccessful.
36
Cont…
● Searching algorithms are used to perform this task
efficiently, minimizing the number of comparison or
operations required to find the desired items.
There are two simple searching algorithms:
Linear Search (Sequential Search), and
Binary Search
37
Cont…
I. Linear Search (Sequential Search)
● The linear search is the easiest searching algorithm that starts
at one end, and goes through each element of a list until the
desired element is found, otherwise the search continues till
the end of the data set.
Pseudocode
● Loop through the array starting at the first element until the
value of target matches found.
● If a match is not found, return –1 or NULL.
38
Examples for Linear Search
39
Cont…
Examples 2 N=8
15 5 20 35 2 42 67 17
0 1 2 3 4 5 6 7
Have two conditions;
1. if elements are present in the array, print location (at
what index, position)
2. if elements are not present in the array, (print element not
found) access each (one by one) from the first (start)
element to end.
40
Cont…
Element 42 has to be searched in it using linear Search
Algorithm.
Find key=42
● Present (found the element, then stop when searching
algorithm is present in the array.
● Key 42 (found index 5 (a[5]), position 6))
Find key= 41
● Not Present (searching until end of array and then stop
searching. And print key is not present in the array)
41
Cont…
Implementation
int Linear_Search(int list[], int key){
for(int i=0; i<n; i++){
if(list[i]==key){
return i;
}
return -1;
}
}
42
Time Complexity for Linear Search
Worst Case Complexity: O(n)
The element being searched may be at the last position in
the array or not at all.
Best Case Complexity: O(1)
● The element being searched could be found in the first
position.
Average Case Complexity: O(n)/ O(n+1/2)
● When the element to be searched is in the middle of the array,
the average case of the Linear Search Algorithm is O(n).
43
Cont…
II. Binary Search
It is an efficient searching method , commonly known as a
half-interval search or a logarithmic search.
An element which is to be searched from the list in an array
A[0,….,n-1] is called Key element.
Fetches data from a sorted list of items(the items/ array
should be sorted).
The algorithm works only on an ordered (sorted) list.
This algorithm locates specific items by comparing the
middlemost items in the data collection.
44
The basic idea is:
1. Locate midpoint of array to search
2. Determine if target is in lower half or upper half of an array.
If in lower half, make this half the array to search
If in upper half, make this half the array to search
3. The element does not match, in which case return –1.
Let A[m] be the mid element of the array A.
Then there are three conditions:
I. If key=A[m] then desired element is present in middle of list.
II. Otherwise if key <A[m] then search left sub list
III. Otherwise if key >A[m] then search right sub list.
45
Cont…
This can be represented as:
46
Cont…
Explanation - The following three cases are possible-
Case-01
● If the element being searched is found to be the middle most
element, its index is returned.
Case-02
● If the element being searched is found to be greater than the
middle most element, then its search is further continued in the
right sub array of the middle most element.
47
Cont…
Case-03
● If the element being searched is found to be smaller than the
middle most element, then its search is further continued in the
left sub array of the middle most element.
● This iteration keeps on repeating on the sub arrays until the
desired element is found or size of the sub array reduces to
zero.
48
Cont…
Example – From sorted array below, search the location of value
31 using binary search.
Solution –
● First, Determine half of the array using the formula −
mid = (low + high) / 2
i.e, (0 + 9) / 2 = 4 (integer/ floor value of 4.5). So, 4 is the mid of
the array.
49
Cont…
● Compare the searched value, i.e 31 with the value stored at
location 4. we know that the target value lie at the right
subarray, because 31 is greater than 27.
● We change our low to mid + 1 and find the new mid value
again. low = mid + 1
mid = (low + high) / 2
● Here, our new mid value is 7. We compare the value stored at
location 7 with our target value 31.
50
Cont…
51
Cont…
● We compare the value stored at location 5 with our target
value. We find that it is a match.
52
Cont…
Implementation
int Binary_Search(int list[],int k) {
int left=0, right=n-1, found=0;
do{
mid=(left+right)/2;
if(key==list[mid])
if(found==0)
found=1;
index=-1;
else if(key<list[mid])
else
right=mid-1;
index=mid;
else
return index;
left=mid+1;
}
}while(found==0&&left<=right);
53
Cont…
Time Complexity for Binary Search
● Best case complexity: O(1) ,if searching key element belongs
to middle of the list.
● Average case complexity: O(log n)
● Worst case complexity: O(log n)
54
END
Thank You!
56