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

7 Different Sorting Algorithms

The document provides an overview of various sorting algorithms, including Selection Sort, Bubble Sort, Insertion Sort, Merge Sort, Counting Sort, Radix Sort, and Bucket Sort. Each algorithm is explained with its steps, advantages, and disadvantages, highlighting their time complexities and suitability for different data sets. The document serves as an educational resource for understanding basic sorting concepts and methods.

Uploaded by

walangkayofr
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

7 Different Sorting Algorithms

The document provides an overview of various sorting algorithms, including Selection Sort, Bubble Sort, Insertion Sort, Merge Sort, Counting Sort, Radix Sort, and Bucket Sort. Each algorithm is explained with its steps, advantages, and disadvantages, highlighting their time complexities and suitability for different data sets. The document serves as an educational resource for understanding basic sorting concepts and methods.

Uploaded by

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

• USED TO

REARRANGE
A GIVEN
SORTING ARRAY OR
ALGORITHM LIST OF
ELEMENTS IN
AN ORDER
7
SORTING
ALGORITHMS
A comparison-based
sorting algorithm
It sorts an array by
1. repeatedly selecting the
SELECTION smallest (or largest)
SORT element from the
unsorted portion and
swapping it with the first
unsorted element. This
process continues until
the entire array is sorted.
Start from the first element at
index 0, find the smallest
element in the rest of the
1. array which is unsorted, and
SELECTION swap (11) with current
element(64).
SORT

STEP 01
Move to the next element at
index 1 (25). Find the smallest
1. in unsorted subarray, and
swap (12) with current
SELECTION
element (25).
SORT

STEP 02
Move to element at index 2
(25). Find the minimum
1. element from unsorted
SELECTION subarray. Swap (22) with
SORT current element (25).

STEP 03
Move to element at index 3
(25). Find the minimum from
1. unsorted subarray and swap
SELECTION (25) with current element
SORT (25).

STEP 04
Move to element at index 4
(64), find the minimum from
1. unsorted subarray and swap
SELECTION (64) with current element
SORT (64).

STEP 05
1. We get the sorted array at the
SELECTION end.
SORT

STEP 06
1. SELECTION
SORT

IMPLEMENTATI
ON
OUTPUT:
ORIGINAL VECTOR: 64 25 12
22 11
SORTED VECTOR: 11 12 22
25 64
 Easy to understand and
implement, making it
1. ideal for teaching basic
SELECTION sorting concepts.
SORT  Requires only a constant
0(1) extra memory
ADVANTAGE space.
 It requires less number
S
of swaps (or memory
writes) compared to
many other standard
 It has a time complexity
1. SELECTION of 0(n^2) makes it
SORT slower compared to
algorithms like Quick sort
DISADVANTAG or Merge Sort.
 Does not maintain the
ES
relative order of equal
element which means it
is not stable.
 It is the simplest sorting
algorithm that works by
repeatedly swapping the
2. BUBBLE
adjacent element if they
SORT are in the wrong order.
 This is not suitable for
large data sets as its
average and worst-case
time complexity.
 Placing the 1st largest
element at its correct
2. BUBBLE position
SORT

STEP 01
 Placing 2nd largest
element at its correct
2. BUBBLE position
SORT

STEP 02
 Placing 3rd largest
element at its correct
2. BUBBLE position
SORT

STEP 03
 It is easy to understand
and implement.
2. BUBBLE  It does not require any
SORT additional memory
space.
 It is stable sorting
ADVANTAGES
algorithm, meaning that
element with the same
key value maintain their
relative order in the
 It has a time complexity
of 0(n^2) which makes it
2. BUBBLE very slow for large data
SORT sets.
 It has almost no or
DISADVANTAG limited real world
ES applications. It is mostly
used in academics to
teach different ways of
sorting.
 It is as simple sorting
algorithm that works by
iteratively inserting each
element of an unsorted list
into its correct position in a
3. INSERTION sorted portion of the list.
SORT  It is like sorting playing cards
in your hands, you split the
cards into two groups: the
sorted cards and the
unsorted. Then, you pick a
card from the unsorted group
and put it in the right place in
3. INSERTION
SORT

STEPS
Initial:
 Current element is 23.
 The first element in the array
is assumed to be sorted.
 The sorted part until 0th index
3. INSERTION is: [23]
SORT
First Pass:
 Compare 1 with 23 (current
element with the sorted part).
 Since 1 is smaller, insert 1
before 23.
 The sorted part until 1st index
Second Pass:
 Compare 10 with 1 and 23
(current element with the
sorted part).
 Since 10 is greater than 1 and
3. INSERTION smaller than 23, insert 10
SORT between 1 and 23.

Third Pass:
 Compare 5 with 1, 10, and 23
(current element with the
sorted part).
 Since 5 is greater than 1 and
Fourth Pass:
 Compare 2 with 1, 5, 10, and
23 (current element with the
sorted part)
 Since 2 is greater than 1 and
3. INSERTION smaller than 5 insert 2
SORT between 1 and 5.
 The sorted part until 4th index
is: 1, 2, 5, 10, 23

Final Array:
 The sorted array is: 1, 2, 5, 10,
23
 Simple and easy to implement.
 Stable sorting algorithm.
3. INSERTION  Efficient for small lists and
nearly sorted lists.
SORT  Space-efficient as it is and in-
pace algorithm.
ADVANTAGES  Adoptive, the number of
inversions is directly
proportional to number of
swaps.
3. INSERTION
SORT  Inefficient for large lists.
 Not as efficient as other
DISADVANTAG sorting algorithm, (e.g.,
merge sort, quick sort) for
ES
most cases.
It follows the divide-and-
conquer approach
It works by recursively
4. MERGE dividing the input array
SORT into smaller subarrays and
sorting those subarrays
then merging them back
together to obtain the
sorted array.
1. Divide: Divide the list or
array recursively into two
halves until it can no more
4. MERGE be divided.
SORT 2. Conquer: Each subarray is
sorted individually using
STEPS the merge sort algorithm.
3. Merge: the sorted
subarrays are merged back
together in sorted order.
The process continues until
all elements from both
Split the Array into two equal
halves

4. MERGE
SORT

STEP 01
Split the Array into two halves

4. MERGE
SORT

STEP 02
Merging unit length cells into
sorted subarrays

4. MERGE
SORT

STEP 03
Merging sorted subarrays into
the sorted array

4. MERGE
SORT

STEP 04
 It sorts elements of an
array by counting the
number of occurrences of
5. COUNTING each unique element in
SORT the array. The count is
stored in an auxiliary
array and the sorting is
done by mapping the
count as an index of the
auxiliary array.
5. COUNTING SORT

Steps
1. Find out the maximum element (let
it be max) from the given array.
8 4 2 2 8 3 3 1

Given
array
5. COUNTING SORT

Steps
2. Initialize an array of length max+1
with all elements 0. This array is used
for sorting the count of the elements in
the array.0 0 0 0 0 0 0 0 0
0 1 2 3 4 5 6 7 8

Count array
5. COUNTING SORT

Steps
3. Store the count of each element at
their respective index in count array.

0 1 2 2 1 0 0 0 1
0 1 2 3 4 5 6 7 8

Count of each element


stored
5. COUNTING SORT

4. Store cumulative sum of the


elements of the count array. It helps in
placing the elements into the correct
index of the sorted array.
0 1 3 5 6 6 6 6 7
0 1 2 3 4 5 6 7 8

Cumulative
5. COUNTING SORT

5. 4 2 2 8 3 3 1

0 1 3 5 6 6 6 6 7

0 1 3 5 6 6 6 6 7
It sorts elements by
processing them digit by
digit.
It is an efficient sorting
algorithm for integers or
6. RADIX SORT strings with fixed-size keys.
It distributes the elements
into buckets based on each
digit’s value. By repeatedly
sorting the elements by
their significant digits, from
6. RADIX SORT
Find the largest element in
the array, which is 802. it
STEP 01
has three digits, so we will
iterate three times, once
for each significant place.
Sort the elements based on the
unit place digits (x=0). We use a
stable sorting technique, such as
counting sort, to sort the digits
at each significant place. It’s
6. RADIX SORT important to understand that the
default implementation of
STEP 02 counting sort is unstable i.e.
same keys can be in different
order than the input array. To
solve this problem, we can
iterate the input array in reverse
order to build the output array.
This strategy helps us to keep as
Sorting based on the unit place:
• Perform counting sort on the
array based on the unit place
digits.
• The sorted array based on the
6. RADIX SORT unit place is 170, 90, 802, 2, 24,
45, 75, 66
STEP 02
Sort the elements based on the
tens place digits.
Sorting based on the tens place:
• Perform counting sort on the array
based on the tens place digits.
6. RADIX SORT • The sorted array based on the
tens place is 802, 2, 24, 45, 66,
170, 75, 90
STEP 03
Sort the elements based on the
hundreds place digits.
Sorting based on the hundreds
place:
• Perform counting sort on the
6. RADIX SORT array based on the hundreds
place digits.
• The sorted array based on the
STEP 04 hundreds place is 2, 24, 45, 66,
75, 90, 170, 802
The array is now sorted in
ascending order.
The final sorted array using radix
sort is 2, 24, 45, 66, 75, 90, 170,
802
6. RADIX SORT

STEP 05
OUTPUT

2 24 45 66 75 90 170 802
it has a linear time
complexity, which makes it
faster than comparison-
based sorting algorithms
6. RADIX SORT It is a stable sorting
algorithm, meaning that
ADVANTAGES elements with the same key
value maintain their relative
order in the sorted output
It is efficient for sorting
large numbers of integers
or strings
It is not efficient for sorting
floating-point numbers or other
types of data that cannot be easily
mapped to a small number of
6. RADIX SORT digits.
It requires a significant amount of
memory to hold the count of the
DISADVANTAG number of times each digit value
ES appears
It is not efficient for small data sets
or data sets with a small number of
unique keys
It requires that the data being
sorted can be represented in a
fixed number of digits, which may
Create an array of size 10,
where each slop represents
a bucket
7. BUCKET
SORT

STEP 01
Insert elements into the
buckets from the input
array based on their range.
7. BUCKET
SORT

STEP 02
Sort the elements within
each bucket. In this
example, we use quicksort
7. BUCKET ( or any stable sorting
SORT algorithm) to sort the
elements within each
STEP 03 bucket.
Gather the elements from
each bucket and put them
back into the original array.
7. BUCKET
SORT

STEP 04
The original array now contains the
sorted elements.

The final sorted array using bucket


7. BUCKET sort for the given input is
SORT 0.12, 0.17, 0.21, 0.23, 0.26, 0.39, 0.68, 0.72, 0.78,
0.94

STEP 05

You might also like