0% found this document useful (0 votes)
3 views

L3 Searching & Sorting algorithms

The document covers fundamental algorithms in searching and sorting, including linear and binary searches, as well as various sorting methods like selection, insertion, merge, and quick sort. It explains the principles behind each algorithm, their efficiency, and performance characteristics. The course is instructed by MD Faizul Abedin Shibli at Feni University, focusing on the organization and manipulation of data structures.

Uploaded by

apollo3122003
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

L3 Searching & Sorting algorithms

The document covers fundamental algorithms in searching and sorting, including linear and binary searches, as well as various sorting methods like selection, insertion, merge, and quick sort. It explains the principles behind each algorithm, their efficiency, and performance characteristics. The course is instructed by MD Faizul Abedin Shibli at Feni University, focusing on the organization and manipulation of data structures.

Uploaded by

apollo3122003
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 54

CSE-223:-Algorithms

Searching & Sorting


Algorithms
Course Instructor

MD Faizul Abedin Shibli


Lecturer
Dept. of CSE
Feni University
Searching and Sorting
Today's
topics:
Searching
Linear
Search
Binary
Search
Sorting
Selection
Sort 2

Insertion
Searching
Searching is when we find something in a data structure. We
frequently search for strings in things like web pages, PDFs,
documents, etc., but we can also search through other data
structures, like lists, dictionaries, etc.
Depending on how our data is organized, we can search
in different ways. For unorganized data, we usually have
to do a linear search, which is the first type of search we
will discuss.
If our data is organized in some way, we can do more
efficient searches. If our data is in a strict order, we can
perform a binary search, which is the second type
3
of search
we will look at.
Linear Searching
The most straightforward type of search is the linear
search.
We traverse the data structure (e.g., a string's characters,
or a list) until we find the result. How would we do a linear
search on a list, like this? Let's say we are searching for 15.
lst = [12, 4, 9, 18, 53, 82, 15, 99, 98, 14, 11]

8
Linear Searching
The most straightforward type of search is the linear
search.
We traverse the data structure (e.g., a string's characters,
or a list) until we find the result. How would we do a linear
search on a list, like this? Let's say we are searching for 15.
lst = [12, 4, 9, 18, 53, 82, 15, 99, 98, 14, 11]

9
Binary Search C
code
Binary Search Animation
Binary Searching
Can we calculate the maximum number of guesses a binary
search will take? Sure!
Each guess splits the amount of the list we have to search
by a factor of 2. Let's see what this means for 16 elements,
if we guess incorrectly until we reach one element. The
number of elements left will drop by half each time:
16 -> 8 -> 4 ->2 ->1
This was four total divisions (16/2 = 8, 8/2=4, 4/2=2, 2/2
= 1), so we have: 4
1
16 × ( ) = 1
2
In general, this
becomes:
1 where n is the number of elements, and
n× ( ) k
2 = 1 knumber
is the
of times we have to 15

divide.
Binary Searching
We can
simplify:
k
1 1
n× ( ) = 1 →n× k = 1→n=
2 2
2k
We can take the log2 of both sides to get:

log 2 (n) = log2(2k )

And we get (after swapping sides):

k = log2 (n)
Therefore, the maximum number of steps, k, for a
binary search is log2(n).
16

For n =100, log2(100) = 6.64, or a maximum of 7 steps.


Sorting
In general, sorting consists of
putting elements into a
particular order, most often the
order is numerical or
lexicographical (i.e.,
alphabetic).
In order for a list to be sorted,
it
must:
be in nondecreasing
order (each element must
be no smaller than the
previous element)
be a permutation of the
input
17
Types of Sorting

18
Bubble sort C
code
Bubble sort C
code
Selection Sort

Selection Sort is another in­place sort that has a simple algorithm:


Find the smallest item in the list, and exchange it with the left­most
unsorted element.
Repeat the process from the first unsorted element.
See animation at:
http://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html

24
Selection Sort

Algorithm

 Find the smallest item in the list, and exchange it


with the left­most unsorted element.
 Repeat the process from the first unsorted element.

Selection sort is particularly slow, because it


needs to go through the entire list each time to
find the smallest item.

25
Selection Sort

Algorithm

 Find the smallest item in the list, and exchange it


with the left­most unsorted element.
 Repeat the process from the first unsorted element.

Selection sort is particularly slow, because it


needs to go through the entire list each time to
find the smallest item.

26
Selection Sort

Algorithm

 Find the smallest item in the list, and exchange it


with the left­most unsorted element.
 Repeat the process from the first unsorted element.

Selection sort is particularly slow, because it


needs to go through the entire list each time to
find the smallest item.

27
Selection Sort

2
Selection sort is always an n algorithm, because you don't get the benefit
if
the list is already sorted ­you always have to find the next smallest
element.
What might selection sort be good for?
If you want to find the "top X" elements, you can do so relatively
fast (at least as fast as any other algorithm, anyway)

28
Selection Sort C
Code

29
Selection sort is also easy to write, but unless you want to find the top X,
it is worth it to simply write insertion sort, instead.
Selection Sort Animation

30
Selection sort is also easy to write, but unless you want to find the top X,
it is worth it to simply write insertion sort, instead.
Insertion Sort C
code

34
Insertion Sort C
code

35
Insertion Sort
Animation
Merge Sort
So far, we have seen sorts that have bad worst­case performance (and in the
case of selection sort, always have bad performance).
Now, we are going to look at a sort called merge sort that has good
performance, always.
Merge sort uses the same basic idea as binary search: split the problem
into halves as much as you can, leading to logarithmic behavior. Merge
sort is called a divide­and­conquer algorithm because of this splitting.
Merge sort can be coded recursively, so we will look at that.
The basic idea of merge sort is to sort already sorted lists. For
example: lst1 = [3, 5, 11]
lst2 = [1, 8, 10]
merge(lst1, lst2) produces [1, 3, 5, 8, 10, 11]

37
Merge Sort Animation
Merge Sort C
Code
Merge Sort C
Code
Merge Sort C
Code
Merge Sort Animation
Quick Sort
Quicksort is a sorting algorithm that is often faster than most other types of sorts,
including merge sort
However, though it is often fast, it is not always fast and can degrade significantly
given the wrong conditions
Quicksort is another divide-and-conquer algorithm
The basic idea:
Divide a list into two smaller sublists: the low elements and the high elements.
Then, recursively sort the sub­lists

44
Quick Sort
The quicksort algorithm:
Pick an element, called a pivot, from the list
Reorder the list so that all elements with values less than the pivot
come before the pivot, while all elements with values greater than
the pivot come after it. After this partitioning, the pivot is in its final
position. This is called the partition operation.
Recursively apply the above steps to the sub-list of
elements with smaller values and separately to the sub­list of elements
with greater values.
The base case of the recursion is for lists of 0 or 1 elements, which
do not need to be sorted.

45
Quick Sort

46
Quick Sort

47
Quick Sort

48
Quick Sort C
Code
Quick Sort C
Code
Quick Sort C
Code
Quick Sort
Animation
Time and Space Complexity of Sorting
Algorithm

You might also like