Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

Chapter 10 Analysis of Sorting Algorithms

The document provides an overview of various sorting algorithms, including Bubble Sort, Insertion Sort, Selection Sort, Merge Sort, Shell Sort, Heap Sort, Bucket Sort, Counting Sort, Radix Sort, and Quicksort. Each algorithm is described with its methodology, steps, and suitability for different data sizes. The document emphasizes that many of these algorithms are not ideal for large datasets due to their time complexities.

Uploaded by

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

Chapter 10 Analysis of Sorting Algorithms

The document provides an overview of various sorting algorithms, including Bubble Sort, Insertion Sort, Selection Sort, Merge Sort, Shell Sort, Heap Sort, Bucket Sort, Counting Sort, Radix Sort, and Quicksort. Each algorithm is described with its methodology, steps, and suitability for different data sizes. The document emphasizes that many of these algorithms are not ideal for large datasets due to their time complexities.

Uploaded by

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

Sorting Algorithms

• Sorting refers to arranging data in a particular format.


Sorting refers to arranging data in a particular format.
Most common orders are in numerical or lexicographical
order.
Bubble Sort Algorithm
Bubble sort is a simple sorting algorithm
This sorting algorithm is comparison-based algorithm in
which each pair of adjacent elements is compared and
the elements are swapped if they are not in order.
This algorithm is not suitable for large data sets as its
average and worst case complexity are of O(n2)
where n is the number of items.
Bubble Sort Algorithm
Bubble Sort works by repeatedly exchanging adjacent elements, if
necessary. When no exchanges are required, the file is sorted.

Steps
Check if the first element in the input array is greater than the
next element in the array.
If it is greater, swap the two elements; otherwise move the
pointer forward in the array.
Repeat Step 2 until we reach the end of the array.
Check if the elements are sorted; if not, repeat the same
process (Step 1 to Step 3) from the last element of the array to
the first.
The final output achieved is the sorted array.
Bubble Sort Algorithm
Pseudocode Memory Requirement
From the algorithm stated
above, it is clear that
bubble sort does not
require extra memory.
Bubble Sort Algorithm
Example

1 4 7
End result after first iteration

2 5

3 6
SAMPLE CODE INCLUDED
Insertion Sort Algorithm
Insertion sort is a very simple method to sort numbers in
an ascending or descending order.
This method follows the incremental method.
This is an in-place comparison-based sorting algorithm.
Here, a sub-list is maintained which is always sorted.
For example, the lower part of an array is maintained to
be sorted. An element which is to be 'inserted' in this
sorted sub-list, has to find its appropriate place and then
it has to be inserted there.
This algorithm is not suitable for large data sets
Insertion Sort Algorithm
Steps
If it is the first element, it is already sorted. return 1;
Pick next element
Compare with all elements in the sorted sub-list
Shift all the elements in the sorted sub-list that is
greater than the value to be sorted
Insert the value
Repeat until list is sorted
Insertion Sort Algorithm
Pseudocode
Insertion Sort Algorithm
Example
an unsorted array

Insertion sort compares the first two elements.

It finds that both 14 and 33 are already in ascending order.


For now, 14 is in sorted sub-list.

Insertion sort moves ahead and compares 33 with 27.


Insertion Sort Algorithm
Finds that 33 is not in the correct position. It swaps 33 with
27. It also checks with all the elements of sorted sub-list.

By now we have 14 and 27 in the sorted sub-list. Next, it


compares 33 with 10. These values are not in a sorted
order.

So they are swapped.

However, swapping makes 27 and 10 unsorted.

Hence, we swap them too.


Selection Sort Algorithm
an in-place comparison-based algorithm in which the list
is divided into two parts, the sorted part at the left end
and the unsorted part at the right end.
The smallest element is selected from the unsorted
array and swapped with the leftmost element, and that
element becomes a part of the sorted array.
This process continues moving unsorted array
boundaries by one element to the right.
This algorithm is not suitable for large data sets as its
average and worst case complexities are of O(n2),
where n is the number of items.
Selection Sort Algorithm
We first find the smallest value in the array and
exchange it with the element in the first position, then
find the second smallest element and exchange it with
the element in the second position, and we continue the
process in this way until the entire array is sorted.
Selection Sort Algorithm
Example

For the first position in the sorted list, the whole list
is scanned sequentially. we search the whole list
and find that 10 is the lowest value.

So we replace 14 with 10.

For the second position, where 33 is residing, we


start scanning the rest of the list in a linear manner.
Merge Sort Algorithm
based on divide and conquer technique.
it is one of the most used and approached algorithms.
Merge sort first divides the array into equal halves and
then combines them in a sorted manner.
How Merge Sort Works?

divides the whole array iteratively into equal halves

Now we divide these two arrays into halves.

We further divide these arrays and we achieve


atomic value which can no more be divided.
Now, we combine them in exactly the same
manner as they were broken down. We first
compare the element for each list and then
combine them into another list in a sorted
manner. 14 and 33 are in sorted positions. 27
and 10 are swapped
How Merge Sort Works?
In the next iteration of the combining phase, we compare lists
of two data values, and merge them into a list of found data
values placing all in a sorted order.

After the final merging, the list becomes sorted and is


considered the final solution.
Shell Sort Algorithm
Shell sort is a highly efficient sorting algorithm and is based
on insertion sort algorithm.
This algorithm avoids large shifts as in case of insertion
sort, if the smaller value is to the far right and has to be
moved to the far left.
This algorithm uses insertion sort on a widely spread
elements, first to sort them and then sorts the less widely
spaced elements. This spacing is termed as interval.
This algorithm is quite efficient for medium-sized data sets
Shell Sort Algorithm
Algorithm for shell sort.
Shell Sort Algorithm
We will use the original sequence of shell
sort, i.e., N/2, N/4,....,1 as the intervals.

In the first loop, n is equal to 8


(size of the array), so the
elements are lying at the interval
of 4 (n/2 = 4). Elements will be
compared and swapped if they
are not in order.

After comparing and swapping, the


updated array will look as follows -
Shell Sort Algorithm

In the second loop, elements are lying at


the interval of 2 (n/4 = 2), where n = 8.

In the third loop, elements are lying at


the interval of 1 (n/8 = 1). In this step,
After comparing and swapping, the shell sort uses insertion sort to sort the
updated array will look as follows - array elements.
Heap Sort Algorithm
Heap Sort is an efficient sorting technique based on the
heap data structure.
The heap is a nearly-complete binary tree where the
parent node could either be minimum or maximum.
The heap with minimum root node is called min-
heap and the root node with maximum root node is
called max-heap.
How the Heap Sort Algorithm
Works
1. TRANSFORM THE ARRAY INTO A BINARY TREE
To transform the array into a binary tree, we can start by inserting the first element of the
array as a node into the tree and continuing this process in a breadth-first manner until all
elements of the array have been added. This will result in a binary tree with all of the
elements of the array, as shown below:

[12, 11, 31, 3, 5, 7, 9]


2. CONVERT THE BINARY TREE INTO A MAX HEAP
In a max heap, all parent nodes must have values that are greater than or
equal to the values of their children.
This will ensure that the highest value is always at the root of the tree.

max heap = [31, 11, 12, 3, 5, 7, 9]


3. SWAP THE ROOT NODE WITH THE LAST
ELEMENT IN THE HEAP
Now, we will transform the array into a tree,
then the tree into a max heap. This step is
depicted in the image below:

• [9, 11, 12, 3, 5, 7]

• Notice that the last element in the array,


holding the value 31, is highlighted in
red. This is because, in the next step,
and in future iterations, we will be
omitting the last value because it’s in a
sorted position.
Bucket Sort Algorithm
Bucket Sort is a sorting algorithm that divides the
unsorted array elements into several groups called
buckets.
Each bucket is then sorted by using any of the
suitable sorting algorithms or recursively applying the
same bucket algorithm.
The process of bucket sort can be understood as
a scatter-gather approach. Here, elements are first
scattered into buckets then the elements in each bucket
are sorted. Finally, the elements are gathered in order.
Bucket Sort Algorithm
Working of Bucket Sort
1. Suppose, the input array is:

Create an array of size 10. Each slot of this array is used


as a bucket for storing elements.
2. Insert elements into the buckets from the array. The elements
are inserted according to the range of the bucket.

3. The elements of each bucket are sorted using any of the stable
sorting algorithms.
4. The elements from each bucket are gathered.
Counting Sort Algorithm
Counting sort is a sorting algorithm that sorts the
elements of an array by counting the number of
occurrences of each unique element in the array.
The count is stored in an auxiliary (separate) array and
the sorting is done by mapping the count as an index of
the auxiliary array.
How Counting Sort Works
Find out the maximum element (let it
be max) from the given array.

Initialize an array of length max+1


with all elements 0. This array is
used for storing the count of the
elements in the array.
How Counting Sort Works
3. Store the count of each element at
their respective index in count array

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. This is done by adding the
previous element to the next one. Add
index 1 value which is 1 to index 2 value
which is 2. 1+2=3. Thus, index 2 now
has a value of 3.
How Counting Sort Works
5. Find the index of each
element of the original
array in the count
array. Subtract 1 to get
the correct index number.
Then add the remaining
values of the same
number by reducing 1 to
index number.
Radix Sort Algorithm
Radix sort is a sorting algorithm
that sorts the elements by first
grouping the individual digits of
the same place value. Then, sort
the elements according to their
increasing/decreasing order.
Let the initial array be [121, 432,
564, 23, 1, 45, 788]. It is sorted
according to radix sort as shown
in the figure below.
How Radix Sort Works
Find the largest element in the array, i.e. max. Let X
be the number of digits in max. X is calculated
because we have to go through all the significant
places of all elements.
In this array [121, 432, 564, 23, 1, 45, 788], we have
the largest number 788. It has 3 digits. Therefore, the
loop should go up to hundreds place (3 times).
How Radix Sort Works
2. Now, go through each
significant place one by
one. Use any stable sorting
technique to sort the digits
at each significant place.
We have used counting sort
for this. Sort the elements
based on the unit place
digits (X=0).
How Radix Sort Works
3. Now, sort the elements based on digits at tens place

4. Finally, sort the elements based on the digits at hundreds


place
Quicksort Algorithm
Quicksort is a sorting algorithm based on the divide and conquer
approach where
An array is divided into subarrays by selecting a pivot
element (element selected from the array). While dividing the
array, the pivot element should be positioned in such a way
that elements less than pivot are kept on the left side and
elements greater than pivot are on the right side of the pivot.
The left and right subarrays are also divided using the same
approach. This process continues until each subarray
contains a single element.
At this point, elements are already sorted. Finally, elements
are combined to form a sorted array.
How Quicksort works
1. Select a pivot. Since, pivot is at left,
so algorithm starts from right and move
towards left.

2. Now, a[pivot] < a[right], so algorithm


moves forward one position towards left,
i.e.
How Quicksort works
3. Because, a[pivot] > a[right],
so, algorithm will swap a[pivot]
with a[right], and pivot moves
to right

4. Since,pivot is at right, so
algorithm starts from left and
moves to right.
How Quicksort works
5. Since a[pivot] > a[left], so algorithm moves
one position to right as –

6. Now, a[left] = 29, a[right] = 24, and a[pivot]


= 24. As a[pivot] < a[left], so, swap a[pivot]
and a[left], now pivot is at left, i.e. -
How Quicksort works
7. Since, pivot is at left, so algorithm starts
from right, and move to left. Now, a[left] = 24,
a[right] = 29, and a[pivot] = 24. As a[pivot] <
a[right], so algorithm moves one position to
left, as –

8. Now, a[pivot] = 24, a[left] = 24, and a[right]


= 14. As a[pivot] > a[right], so, swap a[pivot]
and a[right], now pivot is at right, i.e. -
How Quicksort works
9. Now, a[pivot] = 24, a[left] = 14, and a[right] = 24. Pivot is at
right, so the algorithm starts from left and move to right.
Now, a[pivot] = 24, a[left] = 24, and a[right] = 24. So, pivot, left
and right are pointing the same element. It represents the
termination of procedure. Element 24, which is the pivot
element is placed at its exact position.

10. Elements that are right side of element 24 are greater than
it, and the elements that are left side of element 24 are smaller
than it.

11. Now, in a similar manner, quick sort algorithm is separately


applied to the left and right sub-arrays. After sorting gets done,
the array will be -

You might also like