The document discusses various sorting algorithms. It provides an overview of bubble sort, selection sort, and insertion sort. For bubble sort, it explains the process of "bubbling up" the largest element to the end of the array through successive passes. For selection sort, it illustrates the process of finding the largest element and swapping it into the correct position in each pass to sort the array. For insertion sort, it notes that elements are inserted into the sorted portion of the array in the proper position.
Data Structure Introduction
Data Structure Definition
Data Structure Types
Data Structure Characteristics
Need for Data Structure
Stack Definition
Stack Representation
Stack Operations
Stack Algorithm
Program for Stack in C++
Linked List Definition
Linked List Representation
Linked List Operations
Linked List Algorithm
Program for Linked List in C++
Linked List Defination
Linked List Representation
Linked List Operations
Linked List Algorithm
Program for Linked List in C++
It is a presentation on some Searching and Sorting Techniques for Computer Science.
It consists of the following techniques:
Sequential Search
Binary Search
Selection Sort
Bubble Sort
Insertion Sort
Array is a container which can hold a fix number of items and these items should be of the same type. Most of the data structures make use of arrays to implement their algorithms. Following are the important terms to understand the concept of array.
Data Structure Introduction
Data Structure Definition
Data Structure Types
Data Structure Characteristics
Need for Data Structure
Stack Definition
Stack Representation
Stack Operations
Stack Algorithm
Program for Stack in C++
Linked List Definition
Linked List Representation
Linked List Operations
Linked List Algorithm
Program for Linked List in C++
Linked List Defination
Linked List Representation
Linked List Operations
Linked List Algorithm
Program for Linked List in C++
It is a presentation on some Searching and Sorting Techniques for Computer Science.
It consists of the following techniques:
Sequential Search
Binary Search
Selection Sort
Bubble Sort
Insertion Sort
Array is a container which can hold a fix number of items and these items should be of the same type. Most of the data structures make use of arrays to implement their algorithms. Following are the important terms to understand the concept of array.
In this article, different types of sorting algorithms like the bubble sort, selection sort, etc are discussed. The working method, implementation using C language, and time complexity of different algorithms are also discussed.
About
Indigenized remote control interface card suitable for MAFI system CCR equipment. Compatible for IDM8000 CCR. Backplane mounted serial and TCP/Ethernet communication module for CCR remote access. IDM 8000 CCR remote control on serial and TCP protocol.
• Remote control: Parallel or serial interface.
• Compatible with MAFI CCR system.
• Compatible with IDM8000 CCR.
• Compatible with Backplane mount serial communication.
• Compatible with commercial and Defence aviation CCR system.
• Remote control system for accessing CCR and allied system over serial or TCP.
• Indigenized local Support/presence in India.
• Easy in configuration using DIP switches.
Technical Specifications
Indigenized remote control interface card suitable for MAFI system CCR equipment. Compatible for IDM8000 CCR. Backplane mounted serial and TCP/Ethernet communication module for CCR remote access. IDM 8000 CCR remote control on serial and TCP protocol.
Key Features
Indigenized remote control interface card suitable for MAFI system CCR equipment. Compatible for IDM8000 CCR. Backplane mounted serial and TCP/Ethernet communication module for CCR remote access. IDM 8000 CCR remote control on serial and TCP protocol.
• Remote control: Parallel or serial interface
• Compatible with MAFI CCR system
• Copatiable with IDM8000 CCR
• Compatible with Backplane mount serial communication.
• Compatible with commercial and Defence aviation CCR system.
• Remote control system for accessing CCR and allied system over serial or TCP.
• Indigenized local Support/presence in India.
Application
• Remote control: Parallel or serial interface.
• Compatible with MAFI CCR system.
• Compatible with IDM8000 CCR.
• Compatible with Backplane mount serial communication.
• Compatible with commercial and Defence aviation CCR system.
• Remote control system for accessing CCR and allied system over serial or TCP.
• Indigenized local Support/presence in India.
• Easy in configuration using DIP switches.
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...Amil Baba Dawood bangali
Contact with Dawood Bhai Just call on +92322-6382012 and we'll help you. We'll solve all your problems within 12 to 24 hours and with 101% guarantee and with astrology systematic. If you want to take any personal or professional advice then also you can call us on +92322-6382012 , ONLINE LOVE PROBLEM & Other all types of Daily Life Problem's.Then CALL or WHATSAPP us on +92322-6382012 and Get all these problems solutions here by Amil Baba DAWOOD BANGALI
#vashikaranspecialist #astrologer #palmistry #amliyaat #taweez #manpasandshadi #horoscope #spiritual #lovelife #lovespell #marriagespell#aamilbabainpakistan #amilbabainkarachi #powerfullblackmagicspell #kalajadumantarspecialist #realamilbaba #AmilbabainPakistan #astrologerincanada #astrologerindubai #lovespellsmaster #kalajaduspecialist #lovespellsthatwork #aamilbabainlahore#blackmagicformarriage #aamilbaba #kalajadu #kalailam #taweez #wazifaexpert #jadumantar #vashikaranspecialist #astrologer #palmistry #amliyaat #taweez #manpasandshadi #horoscope #spiritual #lovelife #lovespell #marriagespell#aamilbabainpakistan #amilbabainkarachi #powerfullblackmagicspell #kalajadumantarspecialist #realamilbaba #AmilbabainPakistan #astrologerincanada #astrologerindubai #lovespellsmaster #kalajaduspecialist #lovespellsthatwork #aamilbabainlahore #blackmagicforlove #blackmagicformarriage #aamilbaba #kalajadu #kalailam #taweez #wazifaexpert #jadumantar #vashikaranspecialist #astrologer #palmistry #amliyaat #taweez #manpasandshadi #horoscope #spiritual #lovelife #lovespell #marriagespell#aamilbabainpakistan #amilbabainkarachi #powerfullblackmagicspell #kalajadumantarspecialist #realamilbaba #AmilbabainPakistan #astrologerincanada #astrologerindubai #lovespellsmaster #kalajaduspecialist #lovespellsthatwork #aamilbabainlahore #Amilbabainuk #amilbabainspain #amilbabaindubai #Amilbabainnorway #amilbabainkrachi #amilbabainlahore #amilbabaingujranwalan #amilbabainislamabad
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdffxintegritypublishin
Advancements in technology unveil a myriad of electrical and electronic breakthroughs geared towards efficiently harnessing limited resources to meet human energy demands. The optimization of hybrid solar PV panels and pumped hydro energy supply systems plays a pivotal role in utilizing natural resources effectively. This initiative not only benefits humanity but also fosters environmental sustainability. The study investigated the design optimization of these hybrid systems, focusing on understanding solar radiation patterns, identifying geographical influences on solar radiation, formulating a mathematical model for system optimization, and determining the optimal configuration of PV panels and pumped hydro storage. Through a comparative analysis approach and eight weeks of data collection, the study addressed key research questions related to solar radiation patterns and optimal system design. The findings highlighted regions with heightened solar radiation levels, showcasing substantial potential for power generation and emphasizing the system's efficiency. Optimizing system design significantly boosted power generation, promoted renewable energy utilization, and enhanced energy storage capacity. The study underscored the benefits of optimizing hybrid solar PV panels and pumped hydro energy supply systems for sustainable energy usage. Optimizing the design of solar PV panels and pumped hydro energy supply systems as examined across diverse climatic conditions in a developing country, not only enhances power generation but also improves the integration of renewable energy sources and boosts energy storage capacities, particularly beneficial for less economically prosperous regions. Additionally, the study provides valuable insights for advancing energy research in economically viable areas. Recommendations included conducting site-specific assessments, utilizing advanced modeling tools, implementing regular maintenance protocols, and enhancing communication among system components.
Final project report on grocery store management system..pdfKamal Acharya
In today’s fast-changing business environment, it’s extremely important to be able to respond to client needs in the most effective and timely manner. If your customers wish to see your business online and have instant access to your products or services.
Online Grocery Store is an e-commerce website, which retails various grocery products. This project allows viewing various products available enables registered users to purchase desired products instantly using Paytm, UPI payment processor (Instant Pay) and also can place order by using Cash on Delivery (Pay Later) option. This project provides an easy access to Administrators and Managers to view orders placed using Pay Later and Instant Pay options.
In order to develop an e-commerce website, a number of Technologies must be studied and understood. These include multi-tiered architecture, server and client-side scripting techniques, implementation technologies, programming language (such as PHP, HTML, CSS, JavaScript) and MySQL relational databases. This is a project with the objective to develop a basic website where a consumer is provided with a shopping cart website and also to know about the technologies used to develop such a website.
This document will discuss each of the underlying technologies to create and implement an e- commerce website.
Saudi Arabia stands as a titan in the global energy landscape, renowned for its abundant oil and gas resources. It's the largest exporter of petroleum and holds some of the world's most significant reserves. Let's delve into the top 10 oil and gas projects shaping Saudi Arabia's energy future in 2024.
Hierarchical Digital Twin of a Naval Power SystemKerry Sado
A hierarchical digital twin of a Naval DC power system has been developed and experimentally verified. Similar to other state-of-the-art digital twins, this technology creates a digital replica of the physical system executed in real-time or faster, which can modify hardware controls. However, its advantage stems from distributing computational efforts by utilizing a hierarchical structure composed of lower-level digital twin blocks and a higher-level system digital twin. Each digital twin block is associated with a physical subsystem of the hardware and communicates with a singular system digital twin, which creates a system-level response. By extracting information from each level of the hierarchy, power system controls of the hardware were reconfigured autonomously. This hierarchical digital twin development offers several advantages over other digital twins, particularly in the field of naval power systems. The hierarchical structure allows for greater computational efficiency and scalability while the ability to autonomously reconfigure hardware controls offers increased flexibility and responsiveness. The hierarchical decomposition and models utilized were well aligned with the physical twin, as indicated by the maximum deviations between the developed digital twin hierarchy and the hardware.
Welcome to WIPAC Monthly the magazine brought to you by the LinkedIn Group Water Industry Process Automation & Control.
In this month's edition, along with this month's industry news to celebrate the 13 years since the group was created we have articles including
A case study of the used of Advanced Process Control at the Wastewater Treatment works at Lleida in Spain
A look back on an article on smart wastewater networks in order to see how the industry has measured up in the interim around the adoption of Digital Transformation in the Water Industry.
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Dr.Costas Sachpazis
Terzaghi's soil bearing capacity theory, developed by Karl Terzaghi, is a fundamental principle in geotechnical engineering used to determine the bearing capacity of shallow foundations. This theory provides a method to calculate the ultimate bearing capacity of soil, which is the maximum load per unit area that the soil can support without undergoing shear failure. The Calculation HTML Code included.
2. Unit – 7 Sorting
Contents Hours Marks
a. Introduction
b. Bubble sort
c. Insertion sort
d. Selection sort
e. Quick sort
f. Merge sort
g. Comparison and efficiency of sorting
5 7
2
3. Sorting is among the most basic problems in algorithm design.
Sorting is important because it is often the first step in more complex
algorithms.
Sorting is to take an unordered set of comparable items and arrange them
in some order.
That is, sorting is a process of arranging the items in a list in some order
that is either ascending or descending order.
Let a[n] be an array of n elements a0,a1,a2,a3........,an-1 in memory. The
sorting of the array a[n] means arranging the content of a[n] in either
increasing or decreasing order.
i.e. a0<=a1<=a2<=a3<.=.......<=an-1
Introduction
3
• Efficient sorting is important for optimizing the use of other algorithms
(such as search and merge algorithms) that require sorted lists to work
correctly.
4. 512354277 101
1 2 3 4 5 6
5 12 35 42 77 101
1 2 3 4 5 6
• Input: A sequence of n numbers a1, a2, . . . , an
• Output: A permutation (reordering) a1’, a2’, . . . , an’ of the input sequence such that
a1’ ≤ a2’ ≤ · · · ≤ an’
The Sorting Problem
4
5. Terminology
● Internal Sort:
Internal sorting algorithms assume that data is stored in an array in main memory of
computer. These methods are applied to small collection of data. That is, the entire
collection of data to be sorted is small enough that the sorting can take place within
main memory. Examples are: Bubble, Insertion, Selection, Quick, merge etc.
• External Sort:
When collection of records is too large to fit in the main memory, records must reside
in peripheral or external memory. The only practical way to sort it is to read some
records from the disk do some rearranging then write back to disk. This process is
repeated until the file is sorted. The sorting techniques to deal with these problems are
called external sorting. Sorting large collection of records is central to many
applications, such as processing of payrolls and other business databases. . Example:
external merge sort
5
6. • In-place Sort
The algorithm uses no additional array storage, and hence (other than perhaps the system’s recursion
stack) it is possible to sort very large lists without the need to allocate additional working storage.
Examples are: Bubble sort, Insertion Sort, Selection sort
• Stable Sort:
Sort is said to be stable if elements with equal keys in the input list are kept in the same order in
the output list. If all keys are different then this distinction is not necessary.
But if there are equal keys, then a sorting algorithm is stable if whenever there are two records (let's
say R and S) with the same key, and R appears before S in the original list, then R will always appear
before S in the sorted list.
However, assume that the following pairs of numbers are to be sorted by their first component:
• (4, 2) (3, 7) (3, 1) (5, 6)
• (3, 7) (3, 1) (4, 2) (5, 6) (order maintained)
• (3, 1) (3, 7) (4, 2) (5, 6) (order changed)
• Adaptation to Input:
if the sorting algorithm takes advantage of the sorted or nearly sorted input, then the algorithm is
called adaptive otherwise not. Example: insertion sort is adaptive
6
Terminology
7. Bubble Sort
• The basic idea of this sort is to pass through the array sequentially several
times.
• Each pass consists of comparing each element in the array with its successor
(for example a[i] with a[i + 1]) and interchanging the two elements if they
are not in the proper order.
• After each pass an element is placed in its proper place and is not considered
in succeeding passes.
7
8. "Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using pair-wise
comparisons and swapping
512354277 101
1 2 3 4 5 6
8
9. "Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using pair-wise comparisons
and swapping
512354277 101
1 2 3 4 5 6
Swap42 77
9
10. "Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using pair-wise comparisons
and swapping
512357742 101
1 2 3 4 5 6
Swap35 77
10
11. "Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using pair-wise comparisons
and swapping
512773542 101
1 2 3 4 5 6
Swap12 77
11
12. "Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using pair-wise comparisons
and swapping
577123542 101
1 2 3 4 5 6
No need to swap
12
13. "Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using pair-wise comparisons
and swapping
577123542 101
1 2 3 4 5 6
Swap5 101
13
14. "Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using pair-wise comparisons
and swapping
77123542 5
1 2 3 4 5 6
101
Largest value correctly placed
14
15. Items of Interest
• Notice that only the largest value is correctly
placed
• All other values are still out of order
• So we need to repeat this process
77123542 5
1 2 3 4 5 6
101
Largest value correctly placed
15
16. Repeat “Bubble Up” How Many Times?
• If we have N elements…
• And if each time we bubble an element, we place it in its
correct location…
• Then we repeat the “bubble up” process N – 1 times.
• This guarantees we’ll correctly place all N elements.
16
20. Properties:
Stable
O(1) extra space
O(n2) comparisons and swaps
Adaptive: O(n) when nearly sorted input
Time Complexity:
• Inner loop executes for (n-1) times when i=0, (n-2) times when i=1 and so on:
Time complexity = (n-1) + (n-2) + (n-3) + …………………………. +2 +1
= O(n2)
Space Complexity:
• Since no extra space besides 3 variables is needed for sorting, Space complexity =
O(n)
20
22. Selection Sort
• Idea:
Find the least (or greatest) value in the array, swap it into the leftmost(or
rightmost)component (where it belongs), and then forget the leftmost
component. Do this repeatedly.
• Process:
Let a[n] be a linear array of n elements. The selection sort works as follows:
Pass 1: Find the location loc of the maximum element in the list of n
elements a[0], a[1], a[2], a[3], …......,a[n-1] and then interchange a[loc] and
a[n-1].
Pass 2: Find the location loc of the max element in the sub-list of n-1
elements a[0], a[1], a[2], a[3], …......,a[n-2] and then interchange a[loc] and
a[n-2].
Continue in the same way. Finally, we will get the sorted list:
a[0]<=a[1]<= a[2]<=a[3]<= .....<= a[n-1]. 22
59. Algorithm:
SelectionSort(A)
{
for( i = 0; i < n-1 ; i++)
{
max = A[0];
loc=0;
for ( j = 1; j < =n-1-i ; j++)
{
if (A[j] > max)
{
max = A[j];
loc=j;
}
}
if( n-1-i!=loc)
swap(A[n-1-i],A[loc]);
}
}
59
60. 60
Example:
Consider the array: 15, 10, 20, 25, 5
After Pass 1: 15, 10, 20, 5, 25
After Pass 2: 15, 10, 5, 20, 25
After Pass 3: 5, 10, 15, 20, 25
After Pass 4: 5, 10, 15, 20, 25
Time Complexity:
• Inner loop executes for (n-1) times when i=0, (n-2) times when i=1 and so on: Time
complexity = (n-1) + (n-2) + (n-3) + …………………………. +2 +1
= O(n2)
• The complexity of this algorithm is same as that of bubble sort, but number of swap
operations is reduced greatly.
Exchange largest element and
rightmost one
61. 61
Properties:
Not- stable
In-place sorting (O(1) extra space)
Most time depends upon comparisons O(n2) comparisons
Not adaptive
Minimum number of swaps, so in the applications where cost of
swapping items is high selection sort is the algorithm of choice.
Space Complexity:
Since no extra space besides 5 variables is needed for
sorting, Space complexity = O(n)
62. • Idea: Like sorting a hand of playing cards start with an empty left hand and the cards
facing down on the table. Remove one card at a time from the table, Compare it with
each of the cards already in the hand, from right to left and insert it into the correct
position in the left hand. The cards held in the left hand are sorted.
• Suppose an array a[n] with n elements. The insertion sort works as follows:
• Pass 1: a[0] by itself is trivially sorted.
• Pass 2: a[1] is inserted either before or after a[0] so that a[0], a[1] is sorted.
• Pass 3: a[2] is inserted into its proper place in a[0],a[1] that is before a[0], between a[0]
and a[1], or after a[1] so that a[0],a[1],a[2] is sorted.
.......................................
• Pass N: a[n-1] is inserted into its proper place in a[0],a[1],a[2],........,a[n-2] so that
a[0],a[1],a[2],............,a[n-1] is sorted with n elements.
62
Insertion Sort
66. 66
• Best case:
If array elements are already sorted, inner loop executes only 1 time for i=1,2,3,… , n-1 for
each. So, total time complexity = 1+1+1+ …………..+1 (n-1) times = n-1 = O(n)
• Space Complexity:
Since no extra space besides 3variables is needed for sorting,
Space complexity = O(n)
Properties:
Stable Sorting
In-place sorting (O(1) extra space)
Most time depends upon comparisons O(n2) comparisons
Run time depends upon input (O(n) when nearly sorted input)
O(n2) comparisons and swaps
Complexity
67. 67
• This algorithm is based on the divide and conquer paradigm.
• The main idea behind this sorting is: partitioning of the elements into two
groups and sort these parts recursively. The two partitions contain values that
are either greater or smaller than the key .
• It possesses very good average case complexity among all the sorting
algorithms.
Quick-Sort
Steps for Quick Sort:
• Divide: partition the array into two non-empty sub
arrays.
• Conquer: two sub arrays are sorted recursively.
• Combine: two sub arrays are already sorted in place so
no need to combine
68. 68
Quicksort Algorithm
To sort a[left...right]:
1. if left < right:
1.1. Partition a[left...right] such that: all a[left...p-1] are less
than a[p], and all a[p+1...right] are >= a[p]
1.2. Quicksort a[left...p-1]
1.3. Quicksort a[p+1...right]
2. Terminate
p
numbers less
than p
numbers greater than or
equal to p
p
69. 69
Partitioning in Quicksort
• Choose an array value (say, the first) to use as the pivot
• Starting from the left end, find the first element that is greater
than the pivot
• Searching backward from the right end, find the first element that
is less than or equal to the pivot
• Interchange (swap) these two elements
• Repeat, searching from where we left off, until done
70. 70
Partitioning
To partition a[left...right]:
1. Set pivot = a[left], l = left + 1, r = right;
2. while l < r, do
2.1. while l < right & a[l] <= pivot , set l = l + 1
2.2. while r > left & a[r] > pivot , set r = r - 1
2.3. if l < r, swap a[l] and a[r]
3. Set a[left] = a[r], a[r] = pivot
4. Terminate
80. quickSort(arr,0,5)
6 5 9 12 3 4
0 1 2 3 4 5
partition(arr,0,5)
left right
pivot=6
right moves to the left until
value that should be to left
of pivot...
80
82. quickSort(arr,0,5)
6 5 9 12 3 4
0 1 2 3 4 5
partition(arr,0,5)
left right
pivot=6
left moves to the right until
value that should be to right
of pivot...
82
85. quickSort(arr,0,5)
6 5 4 12 3 9
0 1 2 3 4 5
partition(arr,0,5)
left right
pivot=6
left moves to the right until
value that should be to right
of pivot...
85
87. quickSort(arr,0,5)
6 5 4 12 3 9
0 1 2 3 4 5
partition(arr,0,5)
left right
pivot=6
swap arr[left] and arr[right]
87
88. quickSort(arr,0,5)
6 5 4 3 12 9
0 1 2 3 4 5
partition(arr,0,5)
left
right
pivot=6
right & left CROSS!!!
88
89. quickSort(arr,0,5)
6 5 4 3 12 9
0 1 2 3 4 5
partition(arr,0,5)
left
right
pivot=6
right & left CROSS!!!
1 - Swap pivot and arr[right]
89
90. quickSort(arr,0,5)
3 5 4 6 12 9
0 1 2 3 4 5
partition(arr,0,5)
left
right
pivot=6
right & left CROSS!!!
1 - Swap pivot and arr[right]
90
91. quickSort(arr,0,5)
3 5 4 6 12 9
0 1 2 3 4 5
partition(arr,0,5)
left
right
pivot=6
right & left CROSS!!!
1 - Swap pivot and arr[right]
2 - Return new location of pivot to caller
return 3
91
92. quickSort(arr,0,5) 3 5 4 6 12 9
0 1 2 3 4 5
Recursive calls to quickSort()
using partitioned array...
pivot
position
92
99. right moves to the left until
value that should be to left
of pivot...
quickSort(arr,0,3)
3 5 4 6
0 1 2 3
quickSort(arr,4,5)
12 9
4 5
partition(arr,0,3)
left right
quickSort(arr,0,5)
99
100. quickSort(arr,0,3)
3 5 4 6
0 1 2 3
quickSort(arr,4,5)
12 9
4 5
partition(arr,0,3)
left right
quickSort(arr,0,5)
100
101. quickSort(arr,0,3)
3 5 4 6
0 1 2 3
quickSort(arr,4,5)
12 9
4 5
partition(arr,0,3)
left right
quickSort(arr,0,5)
101
102. quickSort(arr,0,3)
3 5 4 6
0 1 2 3
quickSort(arr,4,5)
12 9
4 5
partition(arr,0,3)
left
right
quickSort(arr,0,5)
102
103. quickSort(arr,0,3)
3 5 4 6
0 1 2 3
quickSort(arr,4,5)
12 9
4 5
partition(arr,0,3)
left
right
right & left CROSS!!!
quickSort(arr,0,5)
103
104. quickSort(arr,0,3)
3 5 4 6
0 1 2 3
quickSort(arr,4,5)
12 9
4 5
partition(arr,0,3)
left
right
right & left CROSS!!!
1 - Swap pivot and arr[right]
quickSort(arr,0,5)
104
105. quickSort(arr,0,3)
3 5 4 6
0 1 2 3
quickSort(arr,4,5)
12 9
4 5
partition(arr,0,3)
left
right
right & left CROSS!!!
1 - Swap pivot and arr[right]
right & left CROSS!!!
1 - Swap pivot and arr[right]
2 - Return new location of pivot to caller
return 0
quickSort(arr,0,5)
105
113. quickSort(arr,0,3) quickSort(arr,4,5)
12 9
4 5
quickSort(arr,0,5)
quickSort(arr,0,0)
3
0
quickSort(arr,1,3)
5 4 6
1 2 3
partition(arr,1,3)
left right
right moves to the left until
value that should be to left
of pivot...
113
115. quickSort(arr,0,3) quickSort(arr,4,5)
12 9
4 5
quickSort(arr,0,5)
quickSort(arr,0,0)
3
0
quickSort(arr,1,3)
5 4 6
1 2 3
partition(arr,1,3)
left right
left moves to the right until
value that should be to right
of pivot...
115
118. quickSort(arr,0,3) quickSort(arr,4,5)
12 9
4 5
quickSort(arr,0,5)
quickSort(arr,0,0)
3
0
quickSort(arr,1,3)
5 4 6
1 2 3
partition(arr,1,3)
left
right
right & left CROSS!
1- swap pivot and arr[right]
118
119. quickSort(arr,0,3) quickSort(arr,4,5)
12 9
4 5
quickSort(arr,0,5)
quickSort(arr,0,0)
3
0
quickSort(arr,1,3)
4 5 6
1 2 3
partition(arr,1,3)
left
right
right & left CROSS!
1- swap pivot and arr[right]
119
120. quickSort(arr,0,3) quickSort(arr,4,5)
12 9
4 5
quickSort(arr,0,5)
quickSort(arr,0,0)
3
0
quickSort(arr,1,3)
4 5 6
1 2 3
partition(arr,1,3)
left
right
right & left CROSS!
1- swap pivot and arr[right]
2 – return new position of pivot
return 2
120
131. Quick Sort
131
Properties:
• Not -stable Sorting
• In-place sorting (O(log n) extra space)
• Not Adaptive
• O(n2) time, typically O(nlogn)
132. 132
• Best Case:
Divides the array into two partitions of equal size, therefore
T(n) = 2T(n/2) + O(n) , Solving this recurrence we get,
T(n)=O(nlogn)
• Worst case:
when one partition contains the n-1 elements and another partition contains
only one element. Therefore its recurrence relation is:
T(n) = T(n-1) + O(n), Solving this recurrence we get, T(n)=O(n2)
• Average case:
Good and bad splits are randomly distributed across throughout the tree
T1(n)= 2T'(n/2) + O(n) Balanced
T'(n)= T(n –1) + O(n) Unbalanced
Solving:
B(n)= 2(B(n/2 –1) + Θ(n/2)) + Θ(n)
= 2B(n/2 –1) + Θ(n)
= O(nlogn)
=>T(n)=O(nlogn)
Analysis of Quick Sort
133. 133
Merge Sort
Merge sort is divide and conquer algorithm. It is recursive algorithm
having three steps. To sort an array A[l . . r]:
• Divide
Divide the n-element sequence to be sorted into two sub-sequences of
n/2 elements
• Conquer
Sort the sub-sequences recursively using merge sort. When the size of
the sequences is 1 there is nothing more to do
•Combine
Merge the two sorted sub-sequences into single sorted array. Keep
track of smallest element in each sorted half and inset smallest of two
elements into auxiliary array. Repeat this until done.
139. Merge Sort
139
Time Complexity:
Recurrence Relation for Merge sort:
T(n) = 1 if n=1
T(n) = 2 T(n/2) + O(n) if n>1
Solving this recurrence we get
T(n) = O(nlogn)
Space Complexity:
It uses one extra array and some extra variables during sorting,
therefore,
Space Complexity = 2n + c = O(n)