SlideShare a Scribd company logo
Data Structure & Algorithm
Unit-III Sorting
by:
Dr. Shweta Saraswat
10/8/2023
Contents
Sorting Techniques:
Sorting
Introduction
4
• Sorting techniques are fundamental algorithms in
computer science and data structures.
• They are used to arrange data in a specific order,
typically in ascending or descending order,
making it easier to search for and retrieve
information efficiently.
• There are various sorting techniques, each with its
own advantages and disadvantages.
What is Sorting?
• Sorting is the process of arranging a collection of
data elements in a specific order, typically in
ascending or descending order.
10/8/2023
6
Sorting Techniques
Here are some of the most commonly used sorting
techniques:
1. Bubble Sort: Bubble sort repeatedly steps
through the list, compares adjacent elements, and
swaps them if they are in the wrong order. The
pass through the list is repeated until the list is
sorted.
2. Selection Sort: Selection sort divides the input list
into two parts: a sorted sublist and an unsorted
sublist. It repeatedly selects the minimum element
from the unsorted sublist and moves it to the
sorted sublist.
7
Sorting Techniques
Here are some of the most commonly used sorting
techniques:
3. Insertion Sort: Insertion sort builds the final sorted
array one item at a time. It takes one element from the
input data and inserts it into its correct position within
the sorted array.
4. Quick Sort: Quick sort is a divide-and-conquer sorting
algorithm that works by selecting a "pivot" element
from the array and partitioning the other elements into
two sub-arrays, according to whether they are less than
or greater than the pivot. The sub-arrays are then
sorted recursively.
8
Sorting Techniques
Here are some of the most commonly used sorting
techniques:
5. Merge Sort: Merge sort is another divide-and-conquer
algorithm. It divides the unsorted list into n sub-lists,
each containing one element, and then repeatedly
merges sub-lists to produce new sorted sub-lists until
there is only one sub-list remaining.
6. Heap Sort: Heap sort is based on the data structure
called a binary heap. It involves building a binary heap
from the input data and repeatedly extracting the
maximum element from the heap until the heap is
empty.
9
Sorting Techniques
Here are some of the most commonly used sorting
techniques:
7. Counting Sort: Counting sort is a non-comparison-
based sorting algorithm. It works well for integers or
objects with a small, finite range of values. It counts the
number of occurrences of each element and uses this
information to place elements in their correct positions.
8. Radix Sort: Radix sort is another non-comparison-based
sorting algorithm that works well for integers. It sorts
data by processing individual digits, starting from the
least significant digit to the most significant digit.
1
0
Sorting Techniques
Here are some of the most commonly used sorting
techniques:
9. Bucket Sort: Bucket sort divides the input data into a
fixed number of buckets, and each bucket is sorted
individually. After sorting, the buckets are concatenated
to obtain the final sorted list.
10. Tim Sort: Tim sort is a hybrid sorting algorithm that
combines elements of merge sort and insertion sort. It
is designed for sorting real-world data and takes
advantage of partially ordered data.
Basic Concepts of Sorting
Basic Concepts of sorting
Here are some basic concepts and terms related to
sorting algorithms:
1. Key: The key is the value or attribute used to
compare and order the data elements. In a list
of numbers, the key is the number itself.
2. Comparison: Most sorting algorithms
determine the order of elements by comparing
their keys. Elements are swapped or rearranged
based on the results of these comparisons.
10/8/2023
Basic Concepts of sorting
3. Stable vs. Unstable Sort: A sorting algorithm is
stable if it maintains the relative order of equal
elements. In other words, if two elements have the
same key, the one that appeared first in the original
list will still appear first after sorting.
4. Sorting: An in-place sorting algorithm rearranges
the elements within the input data structure without
requiring additional memory for a new data
structure. In-place sorting algorithms are memory-
efficient but may have a higher time complexity.
10/8/2023
Basic Concepts of sorting
5. Out-of-Place Sorting: An out-of-place sorting
algorithm creates a new data structure to store the
sorted elements. While it preserves the original data,
it requires extra memory.
6. Comparison-Based Sorting: Many sorting
algorithms are based on comparing elements to
determine their order. The number of comparisons
made during the sorting process is a key factor in
evaluating the efficiency of these algorithms.
10/8/2023
Basic Concepts of sorting
• 7. Time Complexity: Sorting algorithms are often
analyzed in terms of their time complexity, which
describes how the running time of the algorithm
scales with the size of the input data. Common time
complexities for sorting algorithms include O(n^2),
O(n*log(n)), and O(n).
8. Space Complexity: Space complexity refers to the
amount of additional memory required by the sorting
algorithm to perform its task. It is usually measured in
terms of auxiliary memory space.
10/8/2023
Bubble sort
10/8/2023
Why it is named as bubble?
• It is called bubble sort because the movement of
array elements is just like the movement of air
bubbles in the water. Bubbles in water rise up to
the surface; similarly, the array elements in
bubble sort move to the end in each iteration.
• At each step, if two adjacent elements of a list
are not in order, they will be swapped. Thus,
larger elements will “bubble” to the end, (or
smaller elements will be “bubbled” to the front,
depending on implementation) and hence the
name.
10/8/2023
Bubble sort
• Bubble sort works on the repeatedly swapping of
adjacent elements until they are not in the
intended order.
• Although it is simple to use, it is primarily used
as an educational tool because the performance
of bubble sort is poor in the real world.
• The bubble sort, also known as the ripple sort, is
one of the least efficient sorting algorithms.
10/8/2023
Bubble short is majorly used
where -
• complexity does not matter
• simple and shortcode is preferred
10/8/2023
Working of Bubble sort
• Compare the first two values and swap if
necessary.
• Then compare the next pair of values and swap if
necessary.
• This process is repeated n-1 times, where n is the
number of values being sorted.
10/8/2023
Example
10/8/2023
The example above sorts 4 numbers into ascending numerical order. As you can see, this
requires 3 (n-1) passes to achieve since there are 4 items of data. The bigger numbers
can be seen to bubble (or ripple) to the top.
Algorithm
In the algorithm given below, suppose arr is an array
of n elements. The assumed swap function in the
algorithm will swap the values of given array elements.
begin BubbleSort(arr)
for all array elements
if arr[i] > arr[i+1]
swap(arr[i], arr[i+1])
end if
end for
return arr
end BubbleSort
EFFICIENCY OF THE BUBBLE SORT
• The bubble sort algorithm is a reliable sorting
algorithm. This algorithm has a worst-case time
complexity of O(n2). The bubble sort has a space
complexity of O(1). The number of swaps in bubble
sort equals the number of inversion pairs in the
given array. When the array elements are few and the
array is nearly sorted, bubble sort is effective and
efficient.
10/8/2023
Bubble Sort Exercise
BUBBLE SORT THE CARDS AS FOLLOWS:
• Start from the left
• Compare cards 1 and 2. If card 1 is bigger than card 2,
swap their positions, otherwise leave them alone.
• Compare cards 2 and 3. If card 2 is bigger than card 3,
swap their positions, otherwise leave them alone.
• Compare cards 3 and 4. If card 3 is bigger than card 4,
swap their positions, otherwise leave them alone.
• Repeat this process until you reach the end of the row
(The biggest number should have bubbled up to the
right hand side of the list).
• Now repeat the WHOLE PROCESS again, starting from
the left hand side of the list.
Advantages & disadvantages
Insertion Sort
10/8/2023
What Is Insertion Sort?
• Insertion sort is a simple and intuitive sorting
algorithm in which we build the sorted array
using one element at a time. It is easy to
implement and is quite efficient for small sets of
data, especially for substantially sorted data. It is
flexible — it is useful in scenarios where all the
array elements are not available in the
beginning.
Insertion Sort?
• You may have used this sorting method
unknowingly at some point in your life. Insertion
sort is also known as “card player” sort. If you’ve
ever played a game of cards, chances are you
have used some form of insertion sort to arrange
the cards in your hand in a strategic order.
How Insertion Sort Works
• 1. If it is the first element, it is already sorted.
• 2. Pick the next element.
• 3. Compare with all the elements in sorted sub-list.
• 4. Shift all the elements in sorted sub-list that is greater than the
value to be sorted.
• 5. Insert the value.
• 6. Repeat until list is sorted.
Example
Example
Insertion Sort Algorithm
• Step 1 - If the element is the first element, assume that it is
already sorted. Return 1.
• Step2 - Pick the next element, and store it separately in a key.
• Step3 - Now, compare the key with all elements in the sorted
array.
• Step 4 - If the element in the sorted array is smaller than the
current element, then move to the next element. Else, shift
greater elements in the array towards the right.
Step 5 - Insert the value.
• Step 6 - Repeat until the array is sorted.
Advantages & disadvantages
Selection Sort
10/8/2023
What Is Selection Sort?
• selection sort is an in-place comparison-based
sorting algorithm. This sorting algorithm is
known for its simplicity and memory efficiency it
doesn’t take up any extra space. The selection
sort method repeatedly searches “remaining
items” to find the least one and moves it to its
final location.
10/8/2023
Selection Sort Working
Principle
• This algorithm divides the input array into two
subparts — the sorted part and the unsorted
part. Initially, the sorted part of the array is
empty, and the unsorted part is the input array.
• The algorithm works on the principle of finding
the lowest number from the unsorted part of the
array and then swapping it with the first element
of the unsorted part. This is done over and over
until the entire array becomes sorted (in
ascending order).
10/8/2023
Selection Sort Example
• Input array:
• arr [ ]= 19 10 4 8 3
•
10/8/2023
• First, we search for the lowest number in arr[0-4] to
swap it with the unsorted part’s first element. And so,
we swap 3 with 19, and the array gets modified as
follows:
• arr[ ]= 3 10 4 8 19
• Next, we need to find the lowest number in arr[1-4] to
swap with the unsorted array’s first element. So, we
swap 4 with 10.
• arr[ ]= 3 4 10 8 19
• Now, we must find the lowest number in arr[2-4] and
swap. arr[ ]= 3 4 8 10 19
• Do the same for arr[3-4]
• arr[ ]= 3 4 8 10 19
• And our array is sorted!
Selection Sort Algorithm
• Step 1: Set minIndex to position 0
(minIndex will hold the index of the smallest
number in the unsorted subarray)
• Step 2: Search for the smallest element in the
unsorted subarray and update minIndex
• Step 3: Swap the element at the position minIndex
with the first element of the unsorted subarray.
• Step 4: Again set minIndex to the first position of
the unsorted subarray
• Step 5: Repeat steps 2 to 4 until the array gets
sorted
Selection Sort Complexity
• Time Complexity
• The time complexity of the selection sort algorithm
is O(n^2).
• Let’s look at the total number of comparisons made
to get a better idea:
• Worst-case complexity: O(n^2)
When the input array is sorted in descending
order. In this case, n-1 swaps are performed
• Best-case complexity: O(n^2)
When the array is already sorted (ascending
order) In this case, no swap operation is
performed
• Average-case complexity: O(n^2)
When the array is neither sorted in ascending
nor in descending order
10/8/2023
Space Complexity
• Selection sort’s space complexity is O(1), as it
doesn’t take any extra space.
10/8/2023
Quick Sort
10/8/2023
Definition
• QuickSort is a sorting algorithm based on
the Divide and Conquer algorithm that picks an
element as a pivot and partitions the given array
around the picked pivot by placing the pivot in
its correct position in the sorted array.
Divide and conquer approach
Divide and conquer is a technique of breaking down the
algorithms into subproblems, then solving the
subproblems, and combining the results back together
to solve the original problem.
Divide: In Divide, first pick a pivot element. After that,
partition or rearrange the array into two sub-arrays
such that each element in the left sub-array is less than
or equal to the pivot element and each element in the
right sub-array is larger than the pivot element.
Conquer: Recursively, sort two subarrays with Quicksort.
working
• The key process in quickSort is a partition().
The target of partitions is to place the pivot (any
element can be chosen to be a pivot) at its
correct position in the sorted array and put all
smaller elements to the left of the pivot, and all
greater elements to the right of the pivot.
• Partition is done recursively on each side of the
pivot after the pivot is placed in its correct
position and this finally sorts the array.
10/8/2023
Choice of Pivot:
• There are many different choices for picking
pivots.
• Always pick the first element as a pivot.
• Always pick the last element as a pivot
• Pick a random element as a pivot.
• Pick the middle as the pivot.
example
10/8/2023
Quick sort algorithm
visualization
Quick Sort Algorithm
Step 1: An array is divided into subarrays by selecting
a pivot element (element selected from the array).
Step 2: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.
Step 3:The left and right subarrays are also divided using
the same approach. This process continues until each
subarray contains a single element.
Step 4: At this point, elements are already sorted. Finally,
elements are combined to form a sorted array.
Example
Array elements before quick sort are: [4 6 3 2 1 9 7 ]
• pivot swapped: 9, 7 Updated Array: [4 6 3 2 1 7 9 ]
• pivot swapped: 4, 1 Updated Array: [1 6 3 2 4 7 9 ]
• item swapped: 6, 2
• pivot swapped: 6, 4 Updated Array: [1 2 3 4 6 7 9 ]
• pivot swapped: 3, 3 Updated Array: [1 2 3 4 6 7 9 ]
• Array elements after quick sort are: [1 2 3 4 6 7 9 ]
10/8/2023
Example
Heap Sort
What is a heap?
• A heap is a complete binary tree, and the binary
tree is a tree in which the node can have the
utmost two children. A complete binary tree is a
binary tree in which all the levels except the last
level, i.e., leaf node, should be completely filled,
and all the nodes should be left-justified.
What is heap sort?
• Heapsort is a popular and efficient sorting
algorithm. The concept of heap sort is to
eliminate the elements one by one from the
heap part of the list, and then insert them into
the sorted part of the list.
• Heapsort is the in-place sorting algorithm.
working
• It involves building a max heap for increasing order
sorting which contains largest element of the heap at
the root. For decreasing order sorting min heap is
used which contains smallest element of the heap at
the root. The process step of for increasing order
sorting of heap sort is summarized below:
• Step 1: Build a max heap which contains largest
element of the heap at the root
• Step 2: Swap the last element of heap with root
element and remove the last element from the heap.
With the remaining elements repeat the step 1.
example
• To understand the heap sort, lets consider an
unsorted array [10, 1, 23, 50, 7, -4]
• In the below figure, the heap structure of input
array and max heap is shown. The index number of
the root element of the heap is 0. In max heap, the
largest element of the heap always resides at the
root.
example
example
Advantages and Disadvantages
Merge sort
Definition
Merge Sort is a popular and efficient comparison-
based sorting algorithm. It is a divide-and-
conquer algorithm that divides the input list into
smaller sublists, sorts those sublists, and then
merges the sorted sublists to produce a fully
sorted list. The key idea is to repeatedly divide
the list in half until it is broken down into
smaller, sortable pieces and then combine
(merge) those pieces to produce the final sorted
result.
• Merge sort is similar to the quick sort algorithm
as it uses the divide and conquer approach to
sort the elements. It is one of the most popular
and efficient sorting algorithm. It divides the
given list into two equal halves, calls itself for the
two halves and then merges the two sorted
halves. We have to define the merge() function
to perform the merging.
Algorithm
Merge Sort is a divide-and-conquer algorithm for sorting a list of
elements. Here are the high-level steps for the Merge Sort
algorithm:
• Divide: Divide the unsorted list into two roughly equal halves.
This can be done by finding the middle index of the list.
• Conquer: Recursively sort the two smaller sublists created in
the previous step. This process continues until the sublists are
small enough to be considered sorted by definition (i.e., they
contain only one element).
• Merge: Merge the two sorted sublists to produce a single,
sorted list. This is the most critical step in the Merge Sort
algorithm.
• a. Create two temporary arrays (or lists) to hold the two sublists.
• b. Initialize three index variables: one for each of the two
sublists and one for the main merged list.
example
example
example
Applications of Merge Sort:
• Sorting large datasets: Merge sort is
particularly well-suited for sorting large datasets
due to its guaranteed worst-case time
complexity of O(n log n).
• External sorting: Merge sort is commonly used
in external sorting, where the data to be sorted is
too large to fit into memory.
• Custom sorting: Merge sort can be adapted to
handle different input distributions, such as
partially sorted, nearly sorted, or completely
unsorted data.
Merge sort
Radix Sort
What is radix?
• In radix sorting, the term "radix" refers to the
base of the numeral system being used to
represent the numbers you are sorting. It is
essentially the number of unique digits or
symbols that can be used to represent values in
that numeral system. Commonly used radices
include base-2 (binary), base-10 (decimal), and
base-16 (hexadecimal).
• Example: In decimal radix sorting (base-10),
there are 10 unique digits (0-9) that can be used
to represent numbers. Each digit can be
considered a "radix."
Definition
• Radix sort is a non-comparative integer sorting
algorithm that works by distributing elements
into a set of "buckets" or "bins" based on their
individual digits or radix (e.g., decimal digits in
the case of base-10 numbers). The algorithm
processes the elements from the least significant
digit (LSB) to the most significant digit (MSB) or
vice versa, performing multiple passes until all
digits have been considered.
10/8/2023
Working
• The Radix sort algorithm works by ordering each
digit from least significant to most significant.
• In base 10, radix sort would sort by the digits in
the one's place, then the ten's place, and so on.
• To sort the values in each digit place, Radix sort
employs counting sort as a subroutine.
• This means that for a three-digit number in base
10, counting sort will be used to sort the 1st,
10th, and 100th places, resulting in a completely
sorted list. Here's a rundown of the counting sort
algorithm.
Example
10/8/2023
Example
What Is a Radix Sort Algorithm?
• Radix Sort is a linear sorting algorithm.
• Radix Sort's time complexity of O(nd), where n is the size of
the array and d is the number of digits in the largest number.
• It is not an in-place sorting algorithm because it requires extra
space.
• Radix Sort is a stable sort because it maintains the relative order of
elements with equal values.
• Radix sort algorithm may be slower than other sorting algorithms
such as merge sort and Quicksort if the operations are inefficient.
These operations include sub-inset lists and delete functions, and
the process of isolating the desired digits.
• Because it is based on digits or letters, radix sort is less flexible than
other sorts. If the type of data changes, the Radix sort must be
rewritten.
Application
• Radix sort is a stable sorting algorithm, which
means that it preserves the relative order of
equal elements. It is particularly efficient when
sorting a large number of integers with a limited
range of values. However, it may not be the best
choice for sorting data with variable-length keys
or data that doesn't have a clear radix (e.g.,
strings or floating-point numbers).
Advantages Radix Sort
Algorithm
• Fast when the keys are short, i.e. when the array
element range is small.
• Used in suffix arrays construction algorithms
such as Manber's and the DC3 algorithm.
• Radix Sort is a stable sort because it maintains
the relative order of elements with equal values.
10/8/2023
Disadvantages of Radix Sort
Algorithm
• The Radix Sort algorithm is less flexible than other sorts because it is
based on digits or letters. As a result, for each different type of data,
it must be rewritten.
• Radix sort has a higher constant than other sorting algorithms.
• It takes up more space than Quicksort, which is used for in-place
sorting.
• Radix sort may be slower than other sorting algorithms such as
merge sort and Quicksort if the operations are inefficient. These
operations include sub-inset lists and delete functions, as well as the
process of isolating the desired digits.
• Because it is based on digits or letters, the radix sort is less flexible
than other sorts. If the data type must be rewritten, so must the
Radix sort.
Note
• Radix sort and bucket sort are almost equivalent;
bucket sort goes from MSD to LSD, while radix
sort is capable of both "direction" (LSD or MSD).
Counting Sorting
10/8/2023
Definition
• Counting sort is a sorting technique that is based
on the keys between specific ranges
10/8/2023
Approach
• This sorting technique doesn't perform sorting
by comparing elements. It performs sorting by
counting objects having distinct key values like
hashing. After that, it performs some arithmetic
operations to calculate each object's index
position in the output sequence. Counting sort is
not used as a general-purpose sorting algorithm.
• Counting sort is effective when range is not
greater than number of objects to be sorted. It
can be used to sort the negative input values.
10/8/2023
Working of counting sort
Algorithm
• Now, let's see the working of the counting sort
Algorithm.
• To understand the working of the counting sort
algorithm, let's take an unsorted array. It will be
easier to understand the counting sort via an
example.
• Let the elements of array are –
10/8/2023
example
10/8/2023
example
• Now, we have to store the count of each array
element at their corresponding index in the
count array.
• The count of an element will be stored as -
Suppose array element '4' is appeared two times,
so the count of element 4 is 2. Hence, 2 is stored
at the 4th position of the count array. If any
element is not present in the array, place 0, i.e.
suppose element '3' is not present in the array,
so, 0 will be stored at 3rd position.
10/8/2023
example
10/8/2023
example
example
example
Advantages of Counting Sort:
• It is quite fast
• It is a stable algorithm
• Note: For a sorting algorithm to be stable, the
order of elements with equal keys (values) in the
sorted array should be the same as that of the
input array.
10/8/2023
Disadvantages of Counting
Sort:
• It is not suitable for sorting large data sets
• It is not suitable for sorting string values
Comparison of Time and
space complexity
Types of Sorting
• 1. Comparison based sorting algorithm
• 2.Linear time sorting algorithm
• 3.In-place sorting algorithm
• 4. Stable sorting algorithm
10/8/2023
1. Comparison based sorting
2.Linear time sorting
3. In-place sorting algorithm
4. Stable sorting algorithm
10/8/2023
SORTING techniques.pptx

More Related Content

What's hot

stack presentation
stack presentationstack presentation
queue.pptx
queue.pptxqueue.pptx
queue.pptx
Dr.Shweta
 
Stack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi LecturerStack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi Lecturer
gomathi chlm
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data Structure
Meghaj Mallick
 
Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESStacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURES
Sowmya Jyothi
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
Lovely Professional University
 
sorting and its types
sorting and its typessorting and its types
sorting and its types
SIVASHANKARIRAJAN
 
STACK.pptx
STACK.pptxSTACK.pptx
STACK.pptx
Dr.Shweta
 
Priority queues
Priority queuesPriority queues
Priority queues
Priyanka Rana
 
Ppt on Linked list,stack,queue
Ppt on Linked list,stack,queuePpt on Linked list,stack,queue
Ppt on Linked list,stack,queue
Srajan Shukla
 
Queues
QueuesQueues
Unit 4 queue
Unit   4 queueUnit   4 queue
Unit 4 queue
Dabbal Singh Mahara
 
Searching and sorting
Searching  and sortingSearching  and sorting
Searching and sorting
PoojithaBollikonda
 
Doubly linked list
Doubly linked listDoubly linked list
Doubly linked list
chauhankapil
 
Linked List
Linked ListLinked List
Linked List
Ashim Lamichhane
 
Stack - Data Structure
Stack - Data StructureStack - Data Structure
Stack - Data Structure
Bhavesh Sanghvi
 
How bubble sort works
How bubble sort worksHow bubble sort works
How bubble sort works
Gaurav Kumar
 
My lectures circular queue
My lectures circular queueMy lectures circular queue
My lectures circular queueSenthil Kumar
 
Link_List.pptx
Link_List.pptxLink_List.pptx
Link_List.pptx
sandeep54552
 

What's hot (20)

stack presentation
stack presentationstack presentation
stack presentation
 
queue.pptx
queue.pptxqueue.pptx
queue.pptx
 
Stack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi LecturerStack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi Lecturer
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data Structure
 
Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESStacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURES
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
sorting and its types
sorting and its typessorting and its types
sorting and its types
 
STACK.pptx
STACK.pptxSTACK.pptx
STACK.pptx
 
Priority queues
Priority queuesPriority queues
Priority queues
 
Ppt on Linked list,stack,queue
Ppt on Linked list,stack,queuePpt on Linked list,stack,queue
Ppt on Linked list,stack,queue
 
Sorting
SortingSorting
Sorting
 
Queues
QueuesQueues
Queues
 
Unit 4 queue
Unit   4 queueUnit   4 queue
Unit 4 queue
 
Searching and sorting
Searching  and sortingSearching  and sorting
Searching and sorting
 
Doubly linked list
Doubly linked listDoubly linked list
Doubly linked list
 
Linked List
Linked ListLinked List
Linked List
 
Stack - Data Structure
Stack - Data StructureStack - Data Structure
Stack - Data Structure
 
How bubble sort works
How bubble sort worksHow bubble sort works
How bubble sort works
 
My lectures circular queue
My lectures circular queueMy lectures circular queue
My lectures circular queue
 
Link_List.pptx
Link_List.pptxLink_List.pptx
Link_List.pptx
 

Similar to SORTING techniques.pptx

Data Structures_ Sorting & Searching
Data Structures_ Sorting & SearchingData Structures_ Sorting & Searching
Data Structures_ Sorting & Searching
ThenmozhiK5
 
DS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptx
prakashvs7
 
DS PPT - ( 1 )SORTING lgoritham techniques with bast example
DS PPT - ( 1 )SORTING lgoritham techniques with bast exampleDS PPT - ( 1 )SORTING lgoritham techniques with bast example
DS PPT - ( 1 )SORTING lgoritham techniques with bast example
Vivek487417
 
Data structure and algorithms
Data structure and algorithmsData structure and algorithms
Data structure and algorithms
technologygyan
 
Searching,sorting
Searching,sortingSearching,sorting
Searching,sorting
LavanyaJ28
 
Sorting
SortingSorting
Sorting
FahadSaeed39
 
Selection Sort On C++.ppt.pptx
Selection Sort On C++.ppt.pptxSelection Sort On C++.ppt.pptx
Selection Sort On C++.ppt.pptx
MuneebUrRehman643659
 
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHIBCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
Sowmya Jyothi
 
Data Structure & Algorithms - Operations
Data Structure & Algorithms - OperationsData Structure & Algorithms - Operations
Data Structure & Algorithms - Operations
babuk110
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
haramaya university
 
Ijcse13 05-01-048
Ijcse13 05-01-048Ijcse13 05-01-048
Ijcse13 05-01-048vital vital
 
Ijcse13 05-01-048
Ijcse13 05-01-048Ijcse13 05-01-048
Ijcse13 05-01-048vital vital
 
Searching, Sorting and Hashing Techniques
Searching, Sorting and Hashing TechniquesSearching, Sorting and Hashing Techniques
Searching, Sorting and Hashing Techniques
Selvaraj Seerangan
 
What is a Sorting Algorithm Ishank mini project.pptx
What is a Sorting Algorithm Ishank mini project.pptxWhat is a Sorting Algorithm Ishank mini project.pptx
What is a Sorting Algorithm Ishank mini project.pptx
SneakyGoblin1
 
arrays in c
arrays in carrays in c
arrays in c
vidhi mehta
 
DS Module1 (1).pptx
DS Module1 (1).pptxDS Module1 (1).pptx
DS Module1 (1).pptx
AnuJoseph95
 
4.1 sequentioal search
4.1 sequentioal search4.1 sequentioal search
4.1 sequentioal search
Krish_ver2
 
Data operatons & searching and sorting algorithms
Data operatons & searching and sorting algorithmsData operatons & searching and sorting algorithms
Data operatons & searching and sorting algorithms
Anushdika Jeganathan
 
1.4 Sorting.pptx
1.4 Sorting.pptx1.4 Sorting.pptx
1.4 Sorting.pptx
Sujan527908
 

Similar to SORTING techniques.pptx (20)

Data Structures_ Sorting & Searching
Data Structures_ Sorting & SearchingData Structures_ Sorting & Searching
Data Structures_ Sorting & Searching
 
DS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptx
 
DS PPT - ( 1 )SORTING lgoritham techniques with bast example
DS PPT - ( 1 )SORTING lgoritham techniques with bast exampleDS PPT - ( 1 )SORTING lgoritham techniques with bast example
DS PPT - ( 1 )SORTING lgoritham techniques with bast example
 
Data structure and algorithms
Data structure and algorithmsData structure and algorithms
Data structure and algorithms
 
Sorting.pptx
Sorting.pptxSorting.pptx
Sorting.pptx
 
Searching,sorting
Searching,sortingSearching,sorting
Searching,sorting
 
Sorting
SortingSorting
Sorting
 
Selection Sort On C++.ppt.pptx
Selection Sort On C++.ppt.pptxSelection Sort On C++.ppt.pptx
Selection Sort On C++.ppt.pptx
 
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHIBCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
 
Data Structure & Algorithms - Operations
Data Structure & Algorithms - OperationsData Structure & Algorithms - Operations
Data Structure & Algorithms - Operations
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
 
Ijcse13 05-01-048
Ijcse13 05-01-048Ijcse13 05-01-048
Ijcse13 05-01-048
 
Ijcse13 05-01-048
Ijcse13 05-01-048Ijcse13 05-01-048
Ijcse13 05-01-048
 
Searching, Sorting and Hashing Techniques
Searching, Sorting and Hashing TechniquesSearching, Sorting and Hashing Techniques
Searching, Sorting and Hashing Techniques
 
What is a Sorting Algorithm Ishank mini project.pptx
What is a Sorting Algorithm Ishank mini project.pptxWhat is a Sorting Algorithm Ishank mini project.pptx
What is a Sorting Algorithm Ishank mini project.pptx
 
arrays in c
arrays in carrays in c
arrays in c
 
DS Module1 (1).pptx
DS Module1 (1).pptxDS Module1 (1).pptx
DS Module1 (1).pptx
 
4.1 sequentioal search
4.1 sequentioal search4.1 sequentioal search
4.1 sequentioal search
 
Data operatons & searching and sorting algorithms
Data operatons & searching and sorting algorithmsData operatons & searching and sorting algorithms
Data operatons & searching and sorting algorithms
 
1.4 Sorting.pptx
1.4 Sorting.pptx1.4 Sorting.pptx
1.4 Sorting.pptx
 

More from Dr.Shweta

research ethics , plagiarism checking and removal.pptx
research ethics , plagiarism checking and removal.pptxresearch ethics , plagiarism checking and removal.pptx
research ethics , plagiarism checking and removal.pptx
Dr.Shweta
 
effective modular design.pptx
effective modular design.pptxeffective modular design.pptx
effective modular design.pptx
Dr.Shweta
 
software design: design fundamentals.pptx
software design: design fundamentals.pptxsoftware design: design fundamentals.pptx
software design: design fundamentals.pptx
Dr.Shweta
 
Search Algorithms in AI.pptx
Search Algorithms in AI.pptxSearch Algorithms in AI.pptx
Search Algorithms in AI.pptx
Dr.Shweta
 
Informed search algorithms.pptx
Informed search algorithms.pptxInformed search algorithms.pptx
Informed search algorithms.pptx
Dr.Shweta
 
constraint satisfaction problems.pptx
constraint satisfaction problems.pptxconstraint satisfaction problems.pptx
constraint satisfaction problems.pptx
Dr.Shweta
 
review paper publication.pptx
review paper publication.pptxreview paper publication.pptx
review paper publication.pptx
Dr.Shweta
 
Recommended System.pptx
 Recommended System.pptx Recommended System.pptx
Recommended System.pptx
Dr.Shweta
 
semi supervised Learning and Reinforcement learning (1).pptx
 semi supervised Learning and Reinforcement learning (1).pptx semi supervised Learning and Reinforcement learning (1).pptx
semi supervised Learning and Reinforcement learning (1).pptx
Dr.Shweta
 
introduction to Statistical Theory.pptx
 introduction to Statistical Theory.pptx introduction to Statistical Theory.pptx
introduction to Statistical Theory.pptx
Dr.Shweta
 
Unit 2 unsupervised learning.pptx
Unit 2 unsupervised learning.pptxUnit 2 unsupervised learning.pptx
Unit 2 unsupervised learning.pptx
Dr.Shweta
 
unit 1.2 supervised learning.pptx
unit 1.2 supervised learning.pptxunit 1.2 supervised learning.pptx
unit 1.2 supervised learning.pptx
Dr.Shweta
 
Introduction of machine learning.pptx
Introduction of machine learning.pptxIntroduction of machine learning.pptx
Introduction of machine learning.pptx
Dr.Shweta
 
searching techniques.pptx
searching techniques.pptxsearching techniques.pptx
searching techniques.pptx
Dr.Shweta
 
LINKED LIST.pptx
LINKED LIST.pptxLINKED LIST.pptx
LINKED LIST.pptx
Dr.Shweta
 
complexity.pptx
complexity.pptxcomplexity.pptx
complexity.pptx
Dr.Shweta
 
dsa.pptx
dsa.pptxdsa.pptx
dsa.pptx
Dr.Shweta
 
Introduction to Data Science.pptx
Introduction to Data Science.pptxIntroduction to Data Science.pptx
Introduction to Data Science.pptx
Dr.Shweta
 

More from Dr.Shweta (18)

research ethics , plagiarism checking and removal.pptx
research ethics , plagiarism checking and removal.pptxresearch ethics , plagiarism checking and removal.pptx
research ethics , plagiarism checking and removal.pptx
 
effective modular design.pptx
effective modular design.pptxeffective modular design.pptx
effective modular design.pptx
 
software design: design fundamentals.pptx
software design: design fundamentals.pptxsoftware design: design fundamentals.pptx
software design: design fundamentals.pptx
 
Search Algorithms in AI.pptx
Search Algorithms in AI.pptxSearch Algorithms in AI.pptx
Search Algorithms in AI.pptx
 
Informed search algorithms.pptx
Informed search algorithms.pptxInformed search algorithms.pptx
Informed search algorithms.pptx
 
constraint satisfaction problems.pptx
constraint satisfaction problems.pptxconstraint satisfaction problems.pptx
constraint satisfaction problems.pptx
 
review paper publication.pptx
review paper publication.pptxreview paper publication.pptx
review paper publication.pptx
 
Recommended System.pptx
 Recommended System.pptx Recommended System.pptx
Recommended System.pptx
 
semi supervised Learning and Reinforcement learning (1).pptx
 semi supervised Learning and Reinforcement learning (1).pptx semi supervised Learning and Reinforcement learning (1).pptx
semi supervised Learning and Reinforcement learning (1).pptx
 
introduction to Statistical Theory.pptx
 introduction to Statistical Theory.pptx introduction to Statistical Theory.pptx
introduction to Statistical Theory.pptx
 
Unit 2 unsupervised learning.pptx
Unit 2 unsupervised learning.pptxUnit 2 unsupervised learning.pptx
Unit 2 unsupervised learning.pptx
 
unit 1.2 supervised learning.pptx
unit 1.2 supervised learning.pptxunit 1.2 supervised learning.pptx
unit 1.2 supervised learning.pptx
 
Introduction of machine learning.pptx
Introduction of machine learning.pptxIntroduction of machine learning.pptx
Introduction of machine learning.pptx
 
searching techniques.pptx
searching techniques.pptxsearching techniques.pptx
searching techniques.pptx
 
LINKED LIST.pptx
LINKED LIST.pptxLINKED LIST.pptx
LINKED LIST.pptx
 
complexity.pptx
complexity.pptxcomplexity.pptx
complexity.pptx
 
dsa.pptx
dsa.pptxdsa.pptx
dsa.pptx
 
Introduction to Data Science.pptx
Introduction to Data Science.pptxIntroduction to Data Science.pptx
Introduction to Data Science.pptx
 

Recently uploaded

MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
bennyroshan06
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
Excellence Foundation for South Sudan
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
Steve Thomason
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
rosedainty
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
GeoBlogs
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
PedroFerreira53928
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 

Recently uploaded (20)

MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 

SORTING techniques.pptx

  • 1. Data Structure & Algorithm Unit-III Sorting by: Dr. Shweta Saraswat
  • 4. Introduction 4 • Sorting techniques are fundamental algorithms in computer science and data structures. • They are used to arrange data in a specific order, typically in ascending or descending order, making it easier to search for and retrieve information efficiently. • There are various sorting techniques, each with its own advantages and disadvantages.
  • 5. What is Sorting? • Sorting is the process of arranging a collection of data elements in a specific order, typically in ascending or descending order. 10/8/2023
  • 6. 6 Sorting Techniques Here are some of the most commonly used sorting techniques: 1. Bubble Sort: Bubble sort repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted. 2. Selection Sort: Selection sort divides the input list into two parts: a sorted sublist and an unsorted sublist. It repeatedly selects the minimum element from the unsorted sublist and moves it to the sorted sublist.
  • 7. 7 Sorting Techniques Here are some of the most commonly used sorting techniques: 3. Insertion Sort: Insertion sort builds the final sorted array one item at a time. It takes one element from the input data and inserts it into its correct position within the sorted array. 4. Quick Sort: Quick sort is a divide-and-conquer sorting algorithm that works by selecting a "pivot" element from the array and partitioning the other elements into two sub-arrays, according to whether they are less than or greater than the pivot. The sub-arrays are then sorted recursively.
  • 8. 8 Sorting Techniques Here are some of the most commonly used sorting techniques: 5. Merge Sort: Merge sort is another divide-and-conquer algorithm. It divides the unsorted list into n sub-lists, each containing one element, and then repeatedly merges sub-lists to produce new sorted sub-lists until there is only one sub-list remaining. 6. Heap Sort: Heap sort is based on the data structure called a binary heap. It involves building a binary heap from the input data and repeatedly extracting the maximum element from the heap until the heap is empty.
  • 9. 9 Sorting Techniques Here are some of the most commonly used sorting techniques: 7. Counting Sort: Counting sort is a non-comparison- based sorting algorithm. It works well for integers or objects with a small, finite range of values. It counts the number of occurrences of each element and uses this information to place elements in their correct positions. 8. Radix Sort: Radix sort is another non-comparison-based sorting algorithm that works well for integers. It sorts data by processing individual digits, starting from the least significant digit to the most significant digit.
  • 10. 1 0 Sorting Techniques Here are some of the most commonly used sorting techniques: 9. Bucket Sort: Bucket sort divides the input data into a fixed number of buckets, and each bucket is sorted individually. After sorting, the buckets are concatenated to obtain the final sorted list. 10. Tim Sort: Tim sort is a hybrid sorting algorithm that combines elements of merge sort and insertion sort. It is designed for sorting real-world data and takes advantage of partially ordered data.
  • 11. Basic Concepts of Sorting
  • 12. Basic Concepts of sorting Here are some basic concepts and terms related to sorting algorithms: 1. Key: The key is the value or attribute used to compare and order the data elements. In a list of numbers, the key is the number itself. 2. Comparison: Most sorting algorithms determine the order of elements by comparing their keys. Elements are swapped or rearranged based on the results of these comparisons. 10/8/2023
  • 13. Basic Concepts of sorting 3. Stable vs. Unstable Sort: A sorting algorithm is stable if it maintains the relative order of equal elements. In other words, if two elements have the same key, the one that appeared first in the original list will still appear first after sorting. 4. Sorting: An in-place sorting algorithm rearranges the elements within the input data structure without requiring additional memory for a new data structure. In-place sorting algorithms are memory- efficient but may have a higher time complexity. 10/8/2023
  • 14. Basic Concepts of sorting 5. Out-of-Place Sorting: An out-of-place sorting algorithm creates a new data structure to store the sorted elements. While it preserves the original data, it requires extra memory. 6. Comparison-Based Sorting: Many sorting algorithms are based on comparing elements to determine their order. The number of comparisons made during the sorting process is a key factor in evaluating the efficiency of these algorithms. 10/8/2023
  • 15. Basic Concepts of sorting • 7. Time Complexity: Sorting algorithms are often analyzed in terms of their time complexity, which describes how the running time of the algorithm scales with the size of the input data. Common time complexities for sorting algorithms include O(n^2), O(n*log(n)), and O(n). 8. Space Complexity: Space complexity refers to the amount of additional memory required by the sorting algorithm to perform its task. It is usually measured in terms of auxiliary memory space. 10/8/2023
  • 17. Why it is named as bubble? • It is called bubble sort because the movement of array elements is just like the movement of air bubbles in the water. Bubbles in water rise up to the surface; similarly, the array elements in bubble sort move to the end in each iteration. • At each step, if two adjacent elements of a list are not in order, they will be swapped. Thus, larger elements will “bubble” to the end, (or smaller elements will be “bubbled” to the front, depending on implementation) and hence the name. 10/8/2023
  • 18. Bubble sort • Bubble sort works on the repeatedly swapping of adjacent elements until they are not in the intended order. • Although it is simple to use, it is primarily used as an educational tool because the performance of bubble sort is poor in the real world. • The bubble sort, also known as the ripple sort, is one of the least efficient sorting algorithms. 10/8/2023
  • 19. Bubble short is majorly used where - • complexity does not matter • simple and shortcode is preferred 10/8/2023
  • 20. Working of Bubble sort • Compare the first two values and swap if necessary. • Then compare the next pair of values and swap if necessary. • This process is repeated n-1 times, where n is the number of values being sorted. 10/8/2023
  • 21. Example 10/8/2023 The example above sorts 4 numbers into ascending numerical order. As you can see, this requires 3 (n-1) passes to achieve since there are 4 items of data. The bigger numbers can be seen to bubble (or ripple) to the top.
  • 22. Algorithm In the algorithm given below, suppose arr is an array of n elements. The assumed swap function in the algorithm will swap the values of given array elements. begin BubbleSort(arr) for all array elements if arr[i] > arr[i+1] swap(arr[i], arr[i+1]) end if end for return arr end BubbleSort
  • 23. EFFICIENCY OF THE BUBBLE SORT • The bubble sort algorithm is a reliable sorting algorithm. This algorithm has a worst-case time complexity of O(n2). The bubble sort has a space complexity of O(1). The number of swaps in bubble sort equals the number of inversion pairs in the given array. When the array elements are few and the array is nearly sorted, bubble sort is effective and efficient. 10/8/2023
  • 25. BUBBLE SORT THE CARDS AS FOLLOWS: • Start from the left • Compare cards 1 and 2. If card 1 is bigger than card 2, swap their positions, otherwise leave them alone. • Compare cards 2 and 3. If card 2 is bigger than card 3, swap their positions, otherwise leave them alone. • Compare cards 3 and 4. If card 3 is bigger than card 4, swap their positions, otherwise leave them alone. • Repeat this process until you reach the end of the row (The biggest number should have bubbled up to the right hand side of the list). • Now repeat the WHOLE PROCESS again, starting from the left hand side of the list.
  • 26.
  • 29. What Is Insertion Sort? • Insertion sort is a simple and intuitive sorting algorithm in which we build the sorted array using one element at a time. It is easy to implement and is quite efficient for small sets of data, especially for substantially sorted data. It is flexible — it is useful in scenarios where all the array elements are not available in the beginning.
  • 30. Insertion Sort? • You may have used this sorting method unknowingly at some point in your life. Insertion sort is also known as “card player” sort. If you’ve ever played a game of cards, chances are you have used some form of insertion sort to arrange the cards in your hand in a strategic order.
  • 31. How Insertion Sort Works • 1. If it is the first element, it is already sorted. • 2. Pick the next element. • 3. Compare with all the elements in sorted sub-list. • 4. Shift all the elements in sorted sub-list that is greater than the value to be sorted. • 5. Insert the value. • 6. Repeat until list is sorted.
  • 34. Insertion Sort Algorithm • Step 1 - If the element is the first element, assume that it is already sorted. Return 1. • Step2 - Pick the next element, and store it separately in a key. • Step3 - Now, compare the key with all elements in the sorted array. • Step 4 - If the element in the sorted array is smaller than the current element, then move to the next element. Else, shift greater elements in the array towards the right. Step 5 - Insert the value. • Step 6 - Repeat until the array is sorted.
  • 37. What Is Selection Sort? • selection sort is an in-place comparison-based sorting algorithm. This sorting algorithm is known for its simplicity and memory efficiency it doesn’t take up any extra space. The selection sort method repeatedly searches “remaining items” to find the least one and moves it to its final location. 10/8/2023
  • 38. Selection Sort Working Principle • This algorithm divides the input array into two subparts — the sorted part and the unsorted part. Initially, the sorted part of the array is empty, and the unsorted part is the input array. • The algorithm works on the principle of finding the lowest number from the unsorted part of the array and then swapping it with the first element of the unsorted part. This is done over and over until the entire array becomes sorted (in ascending order). 10/8/2023
  • 39. Selection Sort Example • Input array: • arr [ ]= 19 10 4 8 3 • 10/8/2023
  • 40. • First, we search for the lowest number in arr[0-4] to swap it with the unsorted part’s first element. And so, we swap 3 with 19, and the array gets modified as follows: • arr[ ]= 3 10 4 8 19 • Next, we need to find the lowest number in arr[1-4] to swap with the unsorted array’s first element. So, we swap 4 with 10. • arr[ ]= 3 4 10 8 19 • Now, we must find the lowest number in arr[2-4] and swap. arr[ ]= 3 4 8 10 19 • Do the same for arr[3-4] • arr[ ]= 3 4 8 10 19 • And our array is sorted!
  • 41. Selection Sort Algorithm • Step 1: Set minIndex to position 0 (minIndex will hold the index of the smallest number in the unsorted subarray) • Step 2: Search for the smallest element in the unsorted subarray and update minIndex • Step 3: Swap the element at the position minIndex with the first element of the unsorted subarray. • Step 4: Again set minIndex to the first position of the unsorted subarray • Step 5: Repeat steps 2 to 4 until the array gets sorted
  • 42. Selection Sort Complexity • Time Complexity • The time complexity of the selection sort algorithm is O(n^2). • Let’s look at the total number of comparisons made to get a better idea:
  • 43. • Worst-case complexity: O(n^2) When the input array is sorted in descending order. In this case, n-1 swaps are performed • Best-case complexity: O(n^2) When the array is already sorted (ascending order) In this case, no swap operation is performed • Average-case complexity: O(n^2) When the array is neither sorted in ascending nor in descending order 10/8/2023
  • 44. Space Complexity • Selection sort’s space complexity is O(1), as it doesn’t take any extra space. 10/8/2023
  • 45.
  • 47. Definition • QuickSort is a sorting algorithm based on the Divide and Conquer algorithm that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array.
  • 48. Divide and conquer approach Divide and conquer is a technique of breaking down the algorithms into subproblems, then solving the subproblems, and combining the results back together to solve the original problem. Divide: In Divide, first pick a pivot element. After that, partition or rearrange the array into two sub-arrays such that each element in the left sub-array is less than or equal to the pivot element and each element in the right sub-array is larger than the pivot element. Conquer: Recursively, sort two subarrays with Quicksort.
  • 49. working • The key process in quickSort is a partition(). The target of partitions is to place the pivot (any element can be chosen to be a pivot) at its correct position in the sorted array and put all smaller elements to the left of the pivot, and all greater elements to the right of the pivot. • Partition is done recursively on each side of the pivot after the pivot is placed in its correct position and this finally sorts the array. 10/8/2023
  • 50. Choice of Pivot: • There are many different choices for picking pivots. • Always pick the first element as a pivot. • Always pick the last element as a pivot • Pick a random element as a pivot. • Pick the middle as the pivot.
  • 53. Quick Sort Algorithm Step 1: An array is divided into subarrays by selecting a pivot element (element selected from the array). Step 2: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. Step 3:The left and right subarrays are also divided using the same approach. This process continues until each subarray contains a single element. Step 4: At this point, elements are already sorted. Finally, elements are combined to form a sorted array.
  • 54. Example Array elements before quick sort are: [4 6 3 2 1 9 7 ] • pivot swapped: 9, 7 Updated Array: [4 6 3 2 1 7 9 ] • pivot swapped: 4, 1 Updated Array: [1 6 3 2 4 7 9 ] • item swapped: 6, 2 • pivot swapped: 6, 4 Updated Array: [1 2 3 4 6 7 9 ] • pivot swapped: 3, 3 Updated Array: [1 2 3 4 6 7 9 ] • Array elements after quick sort are: [1 2 3 4 6 7 9 ] 10/8/2023
  • 57. What is a heap? • A heap is a complete binary tree, and the binary tree is a tree in which the node can have the utmost two children. A complete binary tree is a binary tree in which all the levels except the last level, i.e., leaf node, should be completely filled, and all the nodes should be left-justified.
  • 58. What is heap sort? • Heapsort is a popular and efficient sorting algorithm. The concept of heap sort is to eliminate the elements one by one from the heap part of the list, and then insert them into the sorted part of the list. • Heapsort is the in-place sorting algorithm.
  • 59. working • It involves building a max heap for increasing order sorting which contains largest element of the heap at the root. For decreasing order sorting min heap is used which contains smallest element of the heap at the root. The process step of for increasing order sorting of heap sort is summarized below: • Step 1: Build a max heap which contains largest element of the heap at the root • Step 2: Swap the last element of heap with root element and remove the last element from the heap. With the remaining elements repeat the step 1.
  • 60. example • To understand the heap sort, lets consider an unsorted array [10, 1, 23, 50, 7, -4] • In the below figure, the heap structure of input array and max heap is shown. The index number of the root element of the heap is 0. In max heap, the largest element of the heap always resides at the root.
  • 65. Definition Merge Sort is a popular and efficient comparison- based sorting algorithm. It is a divide-and- conquer algorithm that divides the input list into smaller sublists, sorts those sublists, and then merges the sorted sublists to produce a fully sorted list. The key idea is to repeatedly divide the list in half until it is broken down into smaller, sortable pieces and then combine (merge) those pieces to produce the final sorted result.
  • 66. • Merge sort is similar to the quick sort algorithm as it uses the divide and conquer approach to sort the elements. It is one of the most popular and efficient sorting algorithm. It divides the given list into two equal halves, calls itself for the two halves and then merges the two sorted halves. We have to define the merge() function to perform the merging.
  • 67. Algorithm Merge Sort is a divide-and-conquer algorithm for sorting a list of elements. Here are the high-level steps for the Merge Sort algorithm: • Divide: Divide the unsorted list into two roughly equal halves. This can be done by finding the middle index of the list. • Conquer: Recursively sort the two smaller sublists created in the previous step. This process continues until the sublists are small enough to be considered sorted by definition (i.e., they contain only one element). • Merge: Merge the two sorted sublists to produce a single, sorted list. This is the most critical step in the Merge Sort algorithm. • a. Create two temporary arrays (or lists) to hold the two sublists. • b. Initialize three index variables: one for each of the two sublists and one for the main merged list.
  • 71. Applications of Merge Sort: • Sorting large datasets: Merge sort is particularly well-suited for sorting large datasets due to its guaranteed worst-case time complexity of O(n log n). • External sorting: Merge sort is commonly used in external sorting, where the data to be sorted is too large to fit into memory. • Custom sorting: Merge sort can be adapted to handle different input distributions, such as partially sorted, nearly sorted, or completely unsorted data.
  • 74. What is radix? • In radix sorting, the term "radix" refers to the base of the numeral system being used to represent the numbers you are sorting. It is essentially the number of unique digits or symbols that can be used to represent values in that numeral system. Commonly used radices include base-2 (binary), base-10 (decimal), and base-16 (hexadecimal). • Example: In decimal radix sorting (base-10), there are 10 unique digits (0-9) that can be used to represent numbers. Each digit can be considered a "radix."
  • 75. Definition • Radix sort is a non-comparative integer sorting algorithm that works by distributing elements into a set of "buckets" or "bins" based on their individual digits or radix (e.g., decimal digits in the case of base-10 numbers). The algorithm processes the elements from the least significant digit (LSB) to the most significant digit (MSB) or vice versa, performing multiple passes until all digits have been considered. 10/8/2023
  • 76. Working • The Radix sort algorithm works by ordering each digit from least significant to most significant. • In base 10, radix sort would sort by the digits in the one's place, then the ten's place, and so on. • To sort the values in each digit place, Radix sort employs counting sort as a subroutine. • This means that for a three-digit number in base 10, counting sort will be used to sort the 1st, 10th, and 100th places, resulting in a completely sorted list. Here's a rundown of the counting sort algorithm.
  • 79. What Is a Radix Sort Algorithm? • Radix Sort is a linear sorting algorithm. • Radix Sort's time complexity of O(nd), where n is the size of the array and d is the number of digits in the largest number. • It is not an in-place sorting algorithm because it requires extra space. • Radix Sort is a stable sort because it maintains the relative order of elements with equal values. • Radix sort algorithm may be slower than other sorting algorithms such as merge sort and Quicksort if the operations are inefficient. These operations include sub-inset lists and delete functions, and the process of isolating the desired digits. • Because it is based on digits or letters, radix sort is less flexible than other sorts. If the type of data changes, the Radix sort must be rewritten.
  • 80. Application • Radix sort is a stable sorting algorithm, which means that it preserves the relative order of equal elements. It is particularly efficient when sorting a large number of integers with a limited range of values. However, it may not be the best choice for sorting data with variable-length keys or data that doesn't have a clear radix (e.g., strings or floating-point numbers).
  • 81. Advantages Radix Sort Algorithm • Fast when the keys are short, i.e. when the array element range is small. • Used in suffix arrays construction algorithms such as Manber's and the DC3 algorithm. • Radix Sort is a stable sort because it maintains the relative order of elements with equal values. 10/8/2023
  • 82. Disadvantages of Radix Sort Algorithm • The Radix Sort algorithm is less flexible than other sorts because it is based on digits or letters. As a result, for each different type of data, it must be rewritten. • Radix sort has a higher constant than other sorting algorithms. • It takes up more space than Quicksort, which is used for in-place sorting. • Radix sort may be slower than other sorting algorithms such as merge sort and Quicksort if the operations are inefficient. These operations include sub-inset lists and delete functions, as well as the process of isolating the desired digits. • Because it is based on digits or letters, the radix sort is less flexible than other sorts. If the data type must be rewritten, so must the Radix sort.
  • 83. Note • Radix sort and bucket sort are almost equivalent; bucket sort goes from MSD to LSD, while radix sort is capable of both "direction" (LSD or MSD).
  • 85. Definition • Counting sort is a sorting technique that is based on the keys between specific ranges 10/8/2023
  • 86. Approach • This sorting technique doesn't perform sorting by comparing elements. It performs sorting by counting objects having distinct key values like hashing. After that, it performs some arithmetic operations to calculate each object's index position in the output sequence. Counting sort is not used as a general-purpose sorting algorithm. • Counting sort is effective when range is not greater than number of objects to be sorted. It can be used to sort the negative input values. 10/8/2023
  • 87. Working of counting sort Algorithm • Now, let's see the working of the counting sort Algorithm. • To understand the working of the counting sort algorithm, let's take an unsorted array. It will be easier to understand the counting sort via an example. • Let the elements of array are – 10/8/2023
  • 89. example • Now, we have to store the count of each array element at their corresponding index in the count array. • The count of an element will be stored as - Suppose array element '4' is appeared two times, so the count of element 4 is 2. Hence, 2 is stored at the 4th position of the count array. If any element is not present in the array, place 0, i.e. suppose element '3' is not present in the array, so, 0 will be stored at 3rd position. 10/8/2023
  • 94. Advantages of Counting Sort: • It is quite fast • It is a stable algorithm • Note: For a sorting algorithm to be stable, the order of elements with equal keys (values) in the sorted array should be the same as that of the input array. 10/8/2023
  • 95. Disadvantages of Counting Sort: • It is not suitable for sorting large data sets • It is not suitable for sorting string values
  • 96. Comparison of Time and space complexity
  • 97. Types of Sorting • 1. Comparison based sorting algorithm • 2.Linear time sorting algorithm • 3.In-place sorting algorithm • 4. Stable sorting algorithm 10/8/2023
  • 99.
  • 101. 3. In-place sorting algorithm
  • 102. 4. Stable sorting algorithm