0% found this document useful (0 votes)
24 views95 pages

Sorting 2nd

The second chapter of sorting

Uploaded by

nizam uddin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views95 pages

Sorting 2nd

The second chapter of sorting

Uploaded by

nizam uddin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 95

SORTING ALGORITHMS

SHELL SORT
MERGE SORT
RADIX SORT
BUCKET SORT
QUICK SORT
HEAP SORT

GDC Thana 3rd Semester Lect Zubair


Khan
Shell Sort
GDC Thana 3rd Semester Lect Zubair
Khan
Shell sort
 Shell sort is a generalized version of the
insertion sort algorithm. It first sorts elements
that are far apart from each other and
successively reduces the interval between the
elements to be sorted. The interval between the
elements is reduced based on the sequence
used.
Shell sort
 Shell sort is mainly a variation of Insertion Sort. In

insertion sort, we move elements only one position ahead.

When an element has to be moved far ahead, many

movements are involved. The idea of Shell Sort is to allow

the exchange of far items. In Shell sort, we make the array

h-sorted for a large value of h. We keep reducing the

value of h until it becomes 1. An array is said to be h-

sorted if all sub lists of every h’th element are sorted.


How Shell Sort Works?

 Let us consider the following example to have an


idea of how shell sort works. {35, 33 , 42, 10 ,
14, 19, 27, 44}
 For our example and ease of understanding, we
take the interval of 4. Make a virtual sub-list of all
values located at the interval of 4 positions
How Shell Sort Works?

 Let us consider the following example to have


an idea of how shell sort works. We take the
same array we have used in our previous
examples. For our example and ease of
understanding, we take the interval of 4. Make
a virtual sub-list of all values located at the
interval of 4 positions
How Shell Sort Works?

 Here these values are

{35, 14}, {33, 19}, {42, 27} and


{10, 44}
How Shell Sort Works?
How Shell Sort Works?
 We compare values in each sub-list and swap
them (if necessary) in the original array. After this
step, the new array should look like this −
How Shell Sort Works?
 Then, we take interval of 2 and this gap
generates two sub-lists - {14, 27, 35, 42}, {19,
10, 33, 44}
How Shell Sort Works?
 We compare and swap the values, if required, in
the original array. After this step, the array should
look like this −
How Shell Sort Works?
 Finally, we sort the rest of the array using interval
of value 1. Shell sort uses insertion sort to sort
the array.
How Shell Sort
Works?
Algorithm:
 Step 1 − Initialize the value of h
 Step 2 − Divide the list into smaller sub-list of
equal interval h
 Step 3 − Sort these sub-lists using insertion
sort
 Step 3 − Repeat until complete list is sorted
Algorithm:
Algorithm:
 N/2, N/4,....,1 as the intervals.
 At the interval of 4, the sublists are
 {33, 12}, {31, 17}, {40, 25}, {8, 42}.
Algorithm:
Algorithm:
Algorithm:
Algorithm:
 In the second loop, elements are lying at
the interval of 2 (n/4 = 2), where n = 8.
 Now, we are taking the interval of 2 to
sort the rest of the array. With an
interval of 2, two sublists will be
generated - {12, 25, 33, 40}, and {17,
8, 31, 42}.
Algorithm:
Algorithm:
Algorithm:

 In the third loop, elements are lying at


the interval of 1 (n/8 = 1), where n = 8.
At last, we use the interval of value 1 to
sort the rest of the array elements. In
this step, shell sort uses insertion sort to
sort the array elements.
Algorithm:
Example
 { 19 , 14 , 15 , 20 , 35 , 39 , 8, 11 }

Merge Sort
GDC Thana 3rd Semester Lect Zubair
Khan
Merge Sort
 The Merge Sort algorithm is a sorting algorithm
that is based on the Divide and
Conquer paradigm. In this algorithm, the array is
initially divided into two equal halves and then
they are combined in a sorted manner.
Divide-and-Conquer
Algorithm
 A divide-and-conquer algorithm recursively
breaks down a problem into two or more
sub-problems of the same or related type,
until these become simple enough to be
solved directly. The solutions to the sub-
problems are then combined to give a solution to
the original problem.
Merge Sort Working
Process:
 Think of it as a recursive algorithm continuously splits the
array in half until it cannot be further divided. This means
that if the array becomes empty or has only one element
left, the dividing will stop, i.e. it is the base case to stop the
recursion. If the array has multiple elements, split the array
into halves and recursively invoke the merge sort on each of
the halves. Finally, when both halves are sorted, the merge
operation is applied. Merge operation is the process of taking
two smaller sorted arrays and combining them to eventually
make a larger one.
Example
 lets consider an array
Merge Sort Working
Process:
 According to the merge sort, first divide the given
array into two equal halves. Merge sort keeps
dividing the list into equal parts until it cannot be
further divided.
 As there are eight elements in the given array, so
it is divided into two arrays of size 4.
Merge Sort Working
Process:
Merge Sort Working
Process:
 Now, again divide these two arrays into
halves. As they are of size 4, so divide
them into new arrays of size 2.
Merge Sort Working
Process:
 Now, again divide these arrays to get the atomic
value that cannot be further divided.
Merge Sort Working
Process:
 Now, combine them in the same manner they
were broken.
 In combining, first compare the element of each
array and then combine them into another array
in sorted order.
 So, first compare 12 and 31, both are in sorted
positions. Then compare 25 and 8, and in the list
of two values, put 8 first followed by 25. Then
compare 32 and 17, sort them and put 17 first
Merge Sort Working
Process:
Merge Sort Working
Process:
 In the next iteration of combining, now
compare the arrays with two data values
and merge them into an array of found
values in sorted order.
Merge Sort Working
Process:
 Now, there is a final merging of the arrays. After
the final merging of above arrays, the array will
look like
Merge Sort Working
Process:
 Now, the array is completely sorted.
Algorithm
 step 1: start
 step 2: declare array and left, right, mid
variable
 step 3: perform merge function.
if left > right
return
mid= (left+right)/2
mergesort(array, left, mid)
mergesort(array, mid+1, right)
merge(array, left, mid, right)
 step 4: Stop
Example
 { 13 , 8 , 5 , 25 , 32 , 37 , 39 , 40}
Radix Algorithm
GDC Thana 3rd Semester Lect Zubair
Khan
Radix Algorithm
 Radix sort is the linear sorting algorithm that is used for integers.

In Radix sort, there is digit by digit sorting is performed that is

started from the least significant digit to the most significant digit.

 The process of radix sort works similar to the sorting of students

names, according to the alphabetical order. In this case, there are

26 radix formed due to the 26 alphabets in English. In the first

pass, the names of students are grouped according to the

ascending order of the first letter of their names. After that, in the

second pass, their names are grouped according to the ascending

order of the second letter of their name. And the process

continues until we find the sorted list.


Working of Radix sort Algorithm

Now, let's see the working of Radix sort Algorithm.

The steps used in the sorting of radix sort are listed as

follows -

 First, we have to find the largest element (suppose max)

from the given array. Suppose 'x' be the number of

digits in max. The 'x' is calculated because we need to

go through the significant places of all elements.

 After that, go through one by one each significant place.

Here, we have to use any stable sorting algorithm to


Working of Radix sort Algorithm

Now let's see the working of radix sort in detail by

using an example. To understand it more clearly,

let's take an unsorted array and try to sort it using

radix sort. It will make the explanation clearer and

easier.
Radix Algorithm

 In the given array, the largest element is 736 that

have 3 digits in it. So, the loop will run up to three times

(i.e., to the hundreds place). That means three passes are

required to sort the array.

 Now, first sort the elements on the basis of unit place digits

(i.e., x = 0). Here, we are using the counting sort algorithm

to sort the elements.


Pass 1:

In the first pass, the list is sorted on the basis of the


digits at 0's place.
After the first pass, the array elements
are -
2nd Pass
 In this pass, the list is sorted on the basis of the
next significant digits (i.e., digits at 10th place).
2nd Pass
 After the second pass, the array elements
are –
3rd Pass
 In this pass, the list is sorted on the basis of the
next significant digits (i.e., digits at 100th place).
3rd Pass
 After the third pass, the array elements
are –
Algorithm
 radixSort(arr)
 max = largest element in the given array
 d = number of digits in the largest element (or, max)
 Now, create d buckets of size 0 - 9
 for i -> 0 to d
 sort the array elements using counting sort (or any stabl
e sort) according to the digits at
 the ith place
Bucket Sort Algorithm
GDC Thana 3rd Semester Lect Zubair
Khan
Bucket Sort Algorithm
 Bucket sort is mainly useful when input is
uniformly distributed over a range. Bucket sort is
a sorting algorithm that separate the elements
into multiple groups said to be buckets. Elements
in bucket sort are first uniformly divided into
groups called buckets, and then they are sorted
by any other sorting algorithm. After that,
elements are gathered in a sorted manner.
Bucket Sort

The basic procedure of performing the bucket sort is


given as follows -
 First, partition the range into a fixed number of
buckets.
 Then, toss every element into its appropriate bucket.
 After that, sort each bucket individually by applying a
sorting algorithm.
 And at last, concatenate all the sorted buckets.
Bucket Sort
Bucket Sort Algorithm

The process of Bucket Sort can be understand as a


scatter-gather approach.
 Dividing the elements into several groups
called buckets.
 Elements inside each bucket are sort by using
suitable sorting or calling bucket sort algorithm
recursively.
 Finally, elements are gathered in order
Scatter-Gather

 The routing message processor Scatter-


Gather sends a request message to
multiple targets concurrently. It
collects the responses from all routes,
and aggregates them into a single
message.
 { 50 , 39 , 2 , 1 , 23 , 14 , 17 , 41 , 8, 6 ,
25 , 33 , 45 , 18 }
Qucik Sort Algorithm
GDC Thana 3rd Semester Lect Zubair
Khan
Quick Sort
 Quick sort is the widely used sorting algorithm. It
is a faster and highly efficient sorting algorithm.
This algorithm follows the divide and conquer
approach. Divide and conquer is a technique of
breaking down the algorithms into sub problems,
then solving the sub problems, and combining
the results back together to solve the original
problem.
Quick Sort
 Quicksort picks an element as pivot, and then it
partitions the given array around the picked pivot
element. In quick sort, a large array is divided
into two arrays in which one holds values that are
smaller than the specified value (Pivot), and
another array holds the values that are greater
than the pivot.
 After that, left and right sub-arrays are also
partitioned using the same approach. It will
Choosing the pivot

Picking a good pivot is necessary for the fast


implementation of quicksort. However, it is typical
to determine a good pivot. Some of the ways of
choosing a pivot are as follows -
 Pivot can be random, i.e. select the random pivot
from the given array.
 Pivot can either be the rightmost element of the
leftmost element of the given array.
 Select median as the pivot element.
Working with Quick Sort

 Let us take an example with the list of


integers
 50 70 60 90 40 80 10 20 30
Working with Quick Sort

 Let us take the first element to be pivot


that is 50.
 Take two pointers i , j . Initially place i at
the pivot i.e. index 0 and j at position n
and keep a number (say infinite) at index
n.
 10 , 30 , 20 , 40 – 50 –
 10 , 20 , 30 , 40 – 50
 lHS
 RHS
 80 , 90 , 60 , 70
 80, 60 , 90 , 70
 60 , 80 , 90 , 70
 60 , 70 , 80 , 90
Working with Quick Sort
 Now , we see that all elements before 50 are
lesser than 50 and all elements after 50 are
greater than 50.
 Now apply this partitioning on both sides of 50
and hence, we will get our sorted array.
Algorithm
 QUICKSORT (array A, start, end)
 {
 if (start < end)
 {
 p = partition(A, start, end)
 QUICKSORT (A, start, p - 1)
 QUICKSORT (A, p + 1, end)
 }
 }
Heap Sort
What is Heap Sort ?
 Heap sort is a comparison-based sorting
technique based on Binary Heap data structure.
It is similar to the selection sort where we first
find the minimum element and place the
minimum element at the beginning. Repeat the
same process for the remaining elements.
Advantages of heap sort:
 Efficiency – The time required to perform Heap sort increases

logarithmically while other algorithms may grow exponentially slower

as the number of items to sort increases. This sorting algorithm is very

efficient.

 Memory Usage – Memory usage is minimal because apart from what

is necessary to hold the initial list of items to be sorted, it needs no

additional memory space to work

 Simplicity – It is simpler to understand than other equally efficient

sorting algorithms because it does not use advanced computer

science concepts such as recursion


What is meant by Heapify?
 Heapify is the process of creating a heap data
structure from a binary tree represented using an
array. It is used to create Min-Heap or Max-heap.
Start from the first index of the non-leaf node
whose index is given by n/2 – 1. Heapify uses
recursion
How does Heapify work?
 Array = {1, 3, 5, 4, 6, 13, 10, 9, 8, 15, 17}
 Corresponding Complete Binary Tree is:

 1
 / \
 3 5
 / \ / \
 4 6 13 10
 /\ /\
 9 8 15 17
How does Heapify work?
 The task to build a Max-Heap from above
array.

 Total Nodes = 11.


 Last Non-leaf node index = (11/2) – 1 = 4.
 Therefore, last non-leaf node = 6.

 To build the heap, heapify only the nodes:


[1, 3, 5, 4, 6] in reverse order.
How does Heapify work?
 Heapify 6: Swap 6 and 17.
 1
/ \
3 5
/ \ / \
4 17 13 10
/\ / \
9 8 15 6
How does Heapify work?
 Heapify 4: Swap 4 and 9.
 1
/ \
3 5
/ \ / \
9 17 13 10
/\ / \
4 8 15 6
How does Heapify work?
 Heapify 5: Swap 13 and 5.
 1
/ \
3 13
/ \ / \
9 17 5 10
/\ / \
4 8 15 6
How does Heapify work?
 Heapify 3: First Swap 3 and 17, again swap 3
and 15.

 1
 / \
 17 13
 / \ / \
 9 15 5 10
 /\ / \
 4 8 3 6
How does Heapify work?
 Heapify 1: First Swap 1 and 17, again swap 1
and 15, finally swap 1 and 6.

 17
 / \
 15 13
 / \ / \
 9 6 5 10
 /\ / \
 4 8 3 1
How does Heapify work?
 Follow the given steps to solve the
problem:

 Build a max heap from the input data.


 At this point, the maximum element is
stored at the root of the heap. Replace it
with the last item of the heap followed
by reducing the size of the heap by 1.
Finally, heapify the root of the tree.
 Repeat step 2 while the size of the heap
is greater than 1.
Working of Heap Sort
 To understand heap sort more clearly, let’s take
an unsorted array and try to sort it using heap
sort.
Consider the array: arr[] = {4, 10, 3, 5, 1}.
 Build Complete Binary Tree: Build a complete
binary tree from the array.
How does Heapify work?
 {4, 10, 3, 5, 1}.

 4
 / \
 10 3
 / \
 5 1
How does Heapify work?
 {4, 10, 3, 5, 1}.

 10
 / \
 4 3
 / \
 5 1
How does Heapify work?
 {4, 10, 3, 5, 1}.

 10
 / \
 5 3
 / \
 4 1
How does Heapify work?
 {4, 10, 3, 5, 1}.

 5
 / \
 1 3
 /
 4
How does Heapify work?
 {4, 10, 3, 5, 1}.

 1
 / \
 4 3
How does Heapify work?
 {4, 10, 3, 5, 1}.

 4
 / \
 1 3
How does Heapify work?
 {4, 10, 3, 5, 1}.

 3
 / \
 1
How does Heapify work?
 {4, 10, 3, 5, 1}.

 3
 / \
 1
How does Heapify work?
 Now when the root is removed once
again it is sorted. and the sorted array
will be like arr[] = {1, 3, 4, 5, 10}.

You might also like