Sorting in data structures is a fundamental operation that is crucial for optimizing the efficiency of data retrieval and manipulation. By ordering data elements according to a defined sequence (numerical, lexicographical, etc.), sorting makes it possible to search for elements more quickly than would be possible in an unsorted structure, especially with algorithms like binary search that rely on a sorted array to operate effectively.
In addition, sorting is essential for tasks that require an ordered dataset, such as finding median values, generating frequency counts, or performing range queries. It also lays the groundwork for more complex operations, such as merging datasets, which requires sorted data to be carried out efficiently.
Queues
a. Concept and Definition
b. Queue as an ADT
c. Implementation of Insert and Delete operation of:
• Linear Queue
• Circular Queue
For More:
https://github.com/ashim888/dataStructureAndAlgorithm
http://www.ashimlamichhane.com.np/
An ordered collection of items from which items may be deleted from one end called the front and into which items may be inserted from other end called rear is known as Queue.
It is a linear data structure.
It is called the First In First Out (FIFO) list. Since in queue, the first element will be the first element out.
Queues
a. Concept and Definition
b. Queue as an ADT
c. Implementation of Insert and Delete operation of:
• Linear Queue
• Circular Queue
For More:
https://github.com/ashim888/dataStructureAndAlgorithm
http://www.ashimlamichhane.com.np/
An ordered collection of items from which items may be deleted from one end called the front and into which items may be inserted from other end called rear is known as Queue.
It is a linear data structure.
It is called the First In First Out (FIFO) list. Since in queue, the first element will be the first element out.
Queue is an abstract data structure, somewhat similar to Stacks. Unlike stacks, a queue is open at both its ends. One end is always used to insert data (enqueue) and the other is used to remove data (dequeue). Queue follows First-In-First-Out methodology, i.e., the data item stored first will be accessed first.
Describes basic understanding of priority queues, their applications, methods, implementation with sorted/unsorted list, sorting applications with insertion sort and selection sort with their running times.
Queue is an abstract data structure, somewhat similar to Stacks. Unlike stacks, a queue is open at both its ends. One end is always used to insert data (enqueue) and the other is used to remove data (dequeue). Queue follows First-In-First-Out methodology, i.e., the data item stored first will be accessed first.
Searching and sorting
Types of Searching
1. Linear Searching
2. Binary Searching
Types of Sorting
1.Selection Sort
2. Insertion Sort
3.Bubble Sort
And the examples of Linear searching, Binary Searching
And also the examples of Selection sort, Insertion sort and Bubble sort and describing them in detail in this ppt
a. Concept and Definition✓
b. Inserting and Deleting nodes ✓
c. Linked implementation of a stack (PUSH/POP) ✓
d. Linked implementation of a queue (Insert/Remove) ✓
e. Circular List
• Stack as a circular list (PUSH/POP) ✓
• Queue as a circular list (Insert/Remove) ✓
f. Doubly Linked List (Insert/Remove) ✓
For more course related material:
https://github.com/ashim888/dataStructureAndAlgorithm/
Personal blog
www.ashimlamichhane.com.np
Queue is an abstract data structure, somewhat similar to Stacks. Unlike stacks, a queue is open at both its ends. One end is always used to insert data (enqueue) and the other is used to remove data (dequeue). Queue follows First-In-First-Out methodology, i.e., the data item stored first will be accessed first.
Describes basic understanding of priority queues, their applications, methods, implementation with sorted/unsorted list, sorting applications with insertion sort and selection sort with their running times.
Queue is an abstract data structure, somewhat similar to Stacks. Unlike stacks, a queue is open at both its ends. One end is always used to insert data (enqueue) and the other is used to remove data (dequeue). Queue follows First-In-First-Out methodology, i.e., the data item stored first will be accessed first.
Searching and sorting
Types of Searching
1. Linear Searching
2. Binary Searching
Types of Sorting
1.Selection Sort
2. Insertion Sort
3.Bubble Sort
And the examples of Linear searching, Binary Searching
And also the examples of Selection sort, Insertion sort and Bubble sort and describing them in detail in this ppt
a. Concept and Definition✓
b. Inserting and Deleting nodes ✓
c. Linked implementation of a stack (PUSH/POP) ✓
d. Linked implementation of a queue (Insert/Remove) ✓
e. Circular List
• Stack as a circular list (PUSH/POP) ✓
• Queue as a circular list (Insert/Remove) ✓
f. Doubly Linked List (Insert/Remove) ✓
For more course related material:
https://github.com/ashim888/dataStructureAndAlgorithm/
Personal blog
www.ashimlamichhane.com.np
Students will be able to learn the various kinds of searching, sorting and hashing techniques to handle the data structures efficiently. This PPT contains the following topics: linear search, binary search, insertion sort, selection sort, bubble sort, shell sort, quick sort merge sort, bucket sort, m-way merge sort, polyphase merge sort, hashing techniques like separate chaining, closed chaining or open addressing, linear probing, quadratic probing, double hashing, rehashing and extendible hashing.
research ethics , plagiarism checking and removal.pptxDr.Shweta
Research ethics, along with plagiarism checking and removal, are integral components of ensuring the integrity and credibility of academic and scientific work. By adhering to ethical guidelines, researchers demonstrate their commitment to honesty, transparency, and the responsible conduct of research, ultimately contributing to the advancement of knowledge and the betterment of society.
Software design is a critical phase in the development of any software application, playing a pivotal role in its success and long-term sustainability.
Search algorithms are fundamental to artificial intelligence (AI) because they play a crucial role in solving complex problems, making decisions, and finding optimal solutions in various AI applications.
Informed search algorithms are commonly used in various AI applications, including pathfinding, puzzle solving, robotics, and game playing. They are particularly effective when the search space is large and the goal state is not immediately visible. By intelligently guiding the search based on heuristic estimates, informed search algorithms can significantly reduce the search effort and find solutions more efficiently than uninformed search algorithms like depth-first search or breadth-first search.
A Constraint Satisfaction Problem (CSP) is a formalism used in computer science and artificial intelligence to represent and solve a wide range of decision and optimization problems. CSPs are characterized by a set of variables, domains for each variable, and a set of constraints that define allowable combinations of variable assignments. The goal in CSPs is to find assignments to the variables that satisfy all constraints.
Publishing a paper is a vital step in the academic and scientific journey, playing a pivotal role in advancing knowledge and establishing one's professional reputation. The process of learning how to publish a paper is crucial because it not only disseminates research findings to a wider audience but also ensures the work undergoes rigorous scrutiny through peer review. Through publication, researchers contribute to the collective understanding of their field, fostering a collaborative and dynamic academic environment. Understanding the nuances of manuscript preparation, journal selection, and submission protocols is essential for navigating the competitive world of academic publishing. Successful publication not only validates the credibility of the research but also opens avenues for career progression, securing research funding, and influencing the direction of scientific discourse. Learning how to publish equips researchers with the skills to communicate effectively, share their discoveries, and actively contribute to the growth and evolution of their respective fields.
A recommendation system, often referred to as a recommender system or recommendation engine, is a type of machine learning application that provides personalized suggestions or recommendations to users. These systems are widely used in various domains to help users discover products, services, or content that are likely to be of interest to them. There are several approaches to building recommendation systems in machine learning:
semi supervised Learning and Reinforcement learning (1).pptxDr.Shweta
Semi-Supervised Learning and Reinforcement Learning are two distinct paradigms within the field of machine learning, each with its own principles and applications. Let's briefly explore each of them:
Statistical theory is a branch of mathematics and statistics that provides the foundation for understanding and working with data, making inferences, and drawing conclusions from observed phenomena. It encompasses a wide range of concepts, principles, and techniques for analyzing and interpreting data in a systematic and rigorous manner. Statistical theory is fundamental to various fields, including science, social science, economics, engineering, and more.
Unsupervised learning is a machine learning paradigm where the algorithm is trained on a dataset containing input data without explicit target values or labels. The primary goal of unsupervised learning is to discover patterns, structures, or relationships within the data without guidance from predefined categories or outcomes. It is a valuable approach for tasks where you want the algorithm to explore the inherent structure and characteristics of the data on its own.
Supervised learning is a fundamental concept in machine learning, where a computer algorithm learns from labeled data to make predictions or decisions. It is a type of machine learning paradigm that involves training a model on a dataset where both the input data and the corresponding desired output (or target) are provided. The goal of supervised learning is to learn a mapping or relationship between inputs and outputs so that the model can make accurate predictions on new, unseen data.v
Machine learning is a subfield of artificial intelligence (AI) that focuses on the development of algorithms and statistical models that enable computer systems to learn and make predictions or decisions without being explicitly programmed. In essence, machine learning allows computers to automatically discover patterns, associations, and insights within data and use that knowledge to improve their performance on a task.
Searching is a fundamental operation in data structures and algorithms, and it involves locating a specific item within a collection of data. Various searching techniques exist, and the choice of which one to use depends on factors like the data structure, the nature of the data, and the efficiency requirements.
A linked list is a fundamental data structure in computer science and is used to organize a collection of elements, such as data, in a linear, non-contiguous manner. Unlike arrays, where elements are stored in contiguous memory locations, linked lists consist of nodes, and each node contains both data and a reference (or link) to the next node in the sequence. Linked lists provide dynamic memory allocation, which allows them to easily grow or shrink as needed.
Data science is an interdisciplinary field that uses algorithms, procedures, and processes to examine large amounts of data in order to uncover hidden patterns, generate insights, and direct decision making.
This is a presentation by Dada Robert in a Your Skill Boost masterclass organised by the Excellence Foundation for South Sudan (EFSS) on Saturday, the 25th and Sunday, the 26th of May 2024.
He discussed the concept of quality improvement, emphasizing its applicability to various aspects of life, including personal, project, and program improvements. He defined quality as doing the right thing at the right time in the right way to achieve the best possible results and discussed the concept of the "gap" between what we know and what we do, and how this gap represents the areas we need to improve. He explained the scientific approach to quality improvement, which involves systematic performance analysis, testing and learning, and implementing change ideas. He also highlighted the importance of client focus and a team approach to quality improvement.
How to Make a Field invisible in Odoo 17Celine George
It is possible to hide or invisible some fields in odoo. Commonly using “invisible” attribute in the field definition to invisible the fields. This slide will show how to make a field invisible in odoo 17.
The Art Pastor's Guide to Sabbath | Steve ThomasonSteve Thomason
What is the purpose of the Sabbath Law in the Torah. It is interesting to compare how the context of the law shifts from Exodus to Deuteronomy. Who gets to rest, and why?
Read| The latest issue of The Challenger is here! We are thrilled to announce that our school paper has qualified for the NATIONAL SCHOOLS PRESS CONFERENCE (NSPC) 2024. Thank you for your unwavering support and trust. Dive into the stories that made us stand out!
We all have good and bad thoughts from time to time and situation to situation. We are bombarded daily with spiraling thoughts(both negative and positive) creating all-consuming feel , making us difficult to manage with associated suffering. Good thoughts are like our Mob Signal (Positive thought) amidst noise(negative thought) in the atmosphere. Negative thoughts like noise outweigh positive thoughts. These thoughts often create unwanted confusion, trouble, stress and frustration in our mind as well as chaos in our physical world. Negative thoughts are also known as “distorted thinking”.
The Roman Empire A Historical Colossus.pdfkaushalkr1407
The Roman Empire, a vast and enduring power, stands as one of history's most remarkable civilizations, leaving an indelible imprint on the world. It emerged from the Roman Republic, transitioning into an imperial powerhouse under the leadership of Augustus Caesar in 27 BCE. This transformation marked the beginning of an era defined by unprecedented territorial expansion, architectural marvels, and profound cultural influence.
The empire's roots lie in the city of Rome, founded, according to legend, by Romulus in 753 BCE. Over centuries, Rome evolved from a small settlement to a formidable republic, characterized by a complex political system with elected officials and checks on power. However, internal strife, class conflicts, and military ambitions paved the way for the end of the Republic. Julius Caesar’s dictatorship and subsequent assassination in 44 BCE created a power vacuum, leading to a civil war. Octavian, later Augustus, emerged victorious, heralding the Roman Empire’s birth.
Under Augustus, the empire experienced the Pax Romana, a 200-year period of relative peace and stability. Augustus reformed the military, established efficient administrative systems, and initiated grand construction projects. The empire's borders expanded, encompassing territories from Britain to Egypt and from Spain to the Euphrates. Roman legions, renowned for their discipline and engineering prowess, secured and maintained these vast territories, building roads, fortifications, and cities that facilitated control and integration.
The Roman Empire’s society was hierarchical, with a rigid class system. At the top were the patricians, wealthy elites who held significant political power. Below them were the plebeians, free citizens with limited political influence, and the vast numbers of slaves who formed the backbone of the economy. The family unit was central, governed by the paterfamilias, the male head who held absolute authority.
Culturally, the Romans were eclectic, absorbing and adapting elements from the civilizations they encountered, particularly the Greeks. Roman art, literature, and philosophy reflected this synthesis, creating a rich cultural tapestry. Latin, the Roman language, became the lingua franca of the Western world, influencing numerous modern languages.
Roman architecture and engineering achievements were monumental. They perfected the arch, vault, and dome, constructing enduring structures like the Colosseum, Pantheon, and aqueducts. These engineering marvels not only showcased Roman ingenuity but also served practical purposes, from public entertainment to water supply.
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.
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.
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
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
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.
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