SlideShare a Scribd company logo
Unit 2
Searching and Sorting Methods
Agenda
• Linear Search, Binary Search
• Bubble Sort, Merge Sort
• Quick Sort, Selection Sort
• Insertion Sort, Radix Sort, Bucket Sort
• Complexity Issues of algorithms
Linear Search
 Linear Search is defined as a sequential search algorithm that starts at one
end and goes through each element of a list until the desired element is
found, otherwise the search continues till the end of the data set. It is the
easiest searching algorithm.
Algorithm Linear(A,n,x)
{
for(i:=0;i<n;i++)
{
If(a[i]=x) then
return i;
}
return -1;
}
Complexity Analysis : Summary
4
Case
Number of key
comparisons
Asymptotic complexity Remark
Case 1 T(n) = 1 T(n) = O(1) Best case
Case 2 T(n) = n T(n) = O(n) Worst case
Case 3 T(n) = O(n) Average case
2
1
)
(


n
n
T
Binary Search
Different Searching and Sorting Methods.pptx
Different Searching and Sorting Methods.pptx
Example
-15,-6,0,7, 9, 23,54, 82,101,112,125, 131,142,151 and x=151, x=-14, x=9
Complexity Analysis:
 Best Case:
In Binary search, best case occurs when the element to search is found in first
comparison, i.e., when the first middle element itself is the element to be searched. The
best-case time complexity of Binary search is O(1).
 Average Case :
The average case time complexity of Binary search is O(logn)
 Worst Case:
In Binary search, the worst case occurs, when we have to keep reducing the search space
till it has only one element. The worst-case time complexity of Binary search is O(logn).
Sorting
 Sorting takes an unordered collection and makes it an
ordered one.
5
12
35
42
77 101
1 2 3 4 5 6
5 12 35 42 77 101
1 2 3 4 5 6
Bubble Sort
 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
5
12
35
42
77 101
1 2 3 4 5 6
Example : 72 42 35 12 101 5
Pass 1 Pass 2
42 72 35 12 101 5 35 42 12 72 5 101
42 35 72 12 101 5 35 12 42 72 5 101
42 35 12 72 101 5 35 12 42 72 5 101
42 35 12 72 101 5 35 12 42 5 72 101
42 35 12 72 5 101
Pass 3 Pass 4
12 35 42 5 72 101 12 35 5 42 72 101
12 35 42 5 72 101 12 5 35 42 72 101
12 35 5 42 72 101
Pass 5
5 12 35 42 72 101
“Bubbling” All the Elements
77
12
35
42 5
1 2 3 4 5 6
101
5
42
12
35 77
1 2 3 4 5 6
101
42
5
35
12 77
1 2 3 4 5 6
101
42
35
5
12 77
1 2 3 4 5 6
101
42
35
12
5 77
1 2 3 4 5 6
101
N
-
1
Reducing the Number of Comparisons
12
35
42
77 101
1 2 3 4 5 6
5
77
12
35
42 5
1 2 3 4 5 6
101
5
42
12
35 77
1 2 3 4 5 6
101
42
5
35
12 77
1 2 3 4 5 6
101
42
35
5
12 77
1 2 3 4 5 6
101
Bubble Sort Algorithm
Algorithm Bubble_Sort(a, n)
{
for i := 1 to n – 1 do
{
// to keep track of the number of iterations
for j := 0 to n –i-1
{
// to compare the elements inside the particular iteration
if (a[j] > a[j + 1]) then
swap(a[j], a[j + 1]); // swap if any element is greater than its adjacent element
}
}
}
Complexity Analysis
• Best Case: The best case occurs when the array is already sorted. So the
number of comparisons required is N-1 and the number of swaps required
= 0. Hence the best case complexity is O(N).
• Worst Case: The worst-case condition for bubble sort occurs when
elements of the array are arranged in decreasing order.
In the worst case, the total number of iterations or passes required to sort
a given array is (N-1). where ‘N’ is the number of elements present in the
array.
• In each iteration Total number of swaps = Total number of comparison
• Total number of comparison (Worst case) = N(N-1)/2
Total number of swaps (Worst case) = N(N-1)/2
• So worst case and average case time complexity is O(N2
) as N2
is the highest
order term.
Merge Sort
 array A[1..n] of n elements given, idea is to split the array in two sets say A[1]……
A[n/2] and A[n/2 + 1]……A[n].
 Sort two sets of arrays individually.
 Merge two sorted sets of to produce a single sorted list of n elements as follows:
• Repeat the following until no elements remain in one of the arrays:
-compare the first elements in the remaining unprocessed portions of the arrays
- copy the smaller of the two into A, while incrementing the index indicating the
unprocessed portion of that array
• Once all elements in one of the arrays are processed, copy the remaining
unprocessed elements from the other array into A
Merge Sort Algorithm
Merge Algorithm
}
{
}
{
}
Example
a[1:10]=(310, 285, 179, 652, 351, 423, 861, 254, 450, 520)
• Hand simulation
• (310 285 179 652 351 | 423 861 254 450 520) split
• (310 285 179 | 652 351 | 423 861 254 450 520) split
• (310 285 | 179 | 652 351 | 423 861 254 450 520) split
• (310 | 285 | 179 | 652 351 | 423 861 254 450 520) split
• (285 310 | 179 | 652 351 | 423 861 254 450 520) merge
• (179 285 310 | 652 351 | 423 861 254 450 520) merge
• (179 285 310 | 652 | 351 | 423 861 254 450 520) split
• (179 285 310 | 351 652 | 423 861 254 450 520) merge
• (179 285 310 351 652 | 423 861 254 450 520) merge
• (179 285 310 351 652 | 423 861 254 | 450 520) split
• ……
Tree of calls of Merge Sort
a[1:10]=(310, 285, 179, 652, 351, 423, 861, 254, 450, 520)
Tree of calls of Merge
a[1:10]=(310, 285, 179, 652, 351, 423, 861, 254, 450, 520)
Example:
a[1:8] = 50 10 25 30 15 70 35 55
 Tree of calls of MergeSort
 Tree of calls of Merge
Analysis of Mergesort
 All cases have same efficiency O(n log n)
 Space complexity O(n)
Quick Sort
 Partitioning a[1:n] into two subarrays in such a way that sorted subarrays do not need
to be merged later.
 This is accomplished by rearranging the elements in a[1:n] such that a[i] <= a[j] for all i
between 1 to m, and for all j between m+1 to n where 1<=m<=n.
 Thus, the elements in a[1:m] and a[m+1:n] can be sorted independently
 No need for merging
 The rearrangement of the elements is accomplished by picking some element of a[],say
t = a[s],and then reordering the other elements so that all elements appearing before t
in a[1:n] are less than or equal to t and all elements appearing after t are greater than
or equal to t.
 This rearranging is referred to as partitioning.
Quick Sort Example
Example
15 22 13 27 12 10 20 25
Quick Sort Algorithm
Algorithm Quicksort(first, last)
{
If(first < last)
{
J:= Partition(a, first, last);
Quicksort(first, j-1);
Quicksort(j+1, last);
}
}
Partition Algorithm
Algorithm Partition(a, m, p)
{
v:= a[m], i:= m, j:= p;
while(i < j)
{
while(a[i]<=v)
i++;
while(a[j]> v)
J--;
if(i<j)
{
temp:= a[i];
a[i]:= a[j];
a[j] := temp;
}
}
a[m]:= a[j];
a[j]= v;
return j;
}
Analysis of Quicksort
 Worst-case time is
 The average time is O(n log n).
Selection Sort
 In selection sort, the smallest value among the unsorted elements of the array is
selected in every pass and inserted to its appropriate position into the array.
 In this algorithm, the array is divided into two parts, first is sorted part, and
another one is the unsorted part.
 Initially, the sorted part of the array is empty, and unsorted part is the given array.
Sorted part is placed at the left, while the unsorted part is placed at the right.
 In selection sort, the first smallest element is selected from the unsorted array and
placed at the first position. After that second smallest element is selected and
placed in the second position. The process continues until the array is entirely
sorted.
Example
12 29 25 8 32 17 40
8 29 25 12 32 17 40
8 12 25 29 32 17 40
8 12 17 29 32 25 40
8 12 17 25 32 29 40
8 12 17 25 29 32 40
8 12 17 25 29 32 40
Selection Sort Algorithm
Algorithm Selectionsort(a,n)
{
for i:=1 to n-1 do
{
pos := i ;
pos:=Smallest(a, i, n, pos) ;
swap( a[i], a[pos] );
}
}
Algorithm Smallest
Algorithm Smallest (a, i, n, pos)
small := a[i] ;
for j := i+1 to n do
{
if (small > a[j]) then
{
small = a[j] ;
pos := j ;
}
}
return pos;
}
Analysis of Selection Sort
 Best case, average case and worst case time complexity is
Insertion Sort
 Insertion sort, is an efficient algorithm for sorting a small number of elements.
 Insertion sort works the way many people sort a hand of playing cards.
 We start with an empty left hand and the cards face down on the
table.
 We then remove one card at a time from the table and insert it into the correct
position in the left hand.
 To find the correct position for a card, we compare it with each of the cards
already in the hand, from right to left.
Example
Algorithm
{
{
{
}
}}
Analysis
 Best case time complexity is Θ(n)
 Average case and worst case time complexity is
Radix Sort
 Digit-by-digit sort.
 Idea: Sort on least-significant digit first with auxiliary stable sort.
 Go from least to most significant digit.
Example
Radix Sort Example
170, 45, 75, 90, 802, 24, 2, 66
Algorithm
Bucket Sort
 Bucket sort assumes that the input is generated by a random process that
distributes elements uniformly and independently over the interval (0, 1).
 Bucket sort divides the interval (0, 1) into n equal-sized subintervals, or
buckets, and then distributes the n input numbers into the buckets.
 Since the inputs are uniformly and independently distributed over (0, 1),
we do not expect many numbers to fall into each bucket.
 To produce the output, we simply sort the numbers in each bucket and then
go through the buckets in order, listing the elements in each.
Algorithm
Example
Example
0.13, 0.8, 0.2, 0.7, 0.16, 0.18, 0.12, 0.1, 0.23, 0.11

More Related Content

Similar to Different Searching and Sorting Methods.pptx (20)

PPT
Unit-2-Sorting (Merge+Quick+Heap+Binary Searach).ppt
sajalsinghal1512
 
PPTX
sorting.pptx
DrRanjeetKumar51721
 
PPTX
Dsa – data structure and algorithms sorting
sajinis3
 
PDF
Analysis and design of algorithms part2
Deepak John
 
PPTX
Sorting2
Saurabh Mishra
 
PPT
Data Structure (MC501)
Kamal Singh Lodhi
 
PPT
Cis435 week06
ashish bansal
 
PPTX
Merge sort and quick sort
Shakila Mahjabin
 
PDF
Sorting
Gopi Saiteja
 
PPTX
CSE680-07QuickSort.pptx
DeepakM509554
 
PPT
Insersion & Bubble Sort in Algoritm
Ehsan Ehrari
 
PPTX
Data structure 8.pptx
SajalFayyaz
 
PPT
lecture 10
sajinsc
 
PPT
Unit6 C
arnold 7490
 
PPT
DAA-Divide and Conquer methodology, DAA 2024
RUHULAMINHAZARIKA
 
PPT
Data Structures 6
Dr.Umadevi V
 
PDF
Daa chapter5
B.Kirron Reddi
 
PPTX
Algorithms - "Chapter 2 getting started"
Ra'Fat Al-Msie'deen
 
PDF
Sorting and Hashing Algorithm full pdfs.
NikhilSoni177492
 
PPT
Lec35
Nikhil Chilwant
 
Unit-2-Sorting (Merge+Quick+Heap+Binary Searach).ppt
sajalsinghal1512
 
sorting.pptx
DrRanjeetKumar51721
 
Dsa – data structure and algorithms sorting
sajinis3
 
Analysis and design of algorithms part2
Deepak John
 
Sorting2
Saurabh Mishra
 
Data Structure (MC501)
Kamal Singh Lodhi
 
Cis435 week06
ashish bansal
 
Merge sort and quick sort
Shakila Mahjabin
 
Sorting
Gopi Saiteja
 
CSE680-07QuickSort.pptx
DeepakM509554
 
Insersion & Bubble Sort in Algoritm
Ehsan Ehrari
 
Data structure 8.pptx
SajalFayyaz
 
lecture 10
sajinsc
 
Unit6 C
arnold 7490
 
DAA-Divide and Conquer methodology, DAA 2024
RUHULAMINHAZARIKA
 
Data Structures 6
Dr.Umadevi V
 
Daa chapter5
B.Kirron Reddi
 
Algorithms - "Chapter 2 getting started"
Ra'Fat Al-Msie'deen
 
Sorting and Hashing Algorithm full pdfs.
NikhilSoni177492
 

More from Minakshee Patil (18)

PPTX
Introduction, characteristics, Pseudocode.pptx
Minakshee Patil
 
PPTX
0-1_knapsack_using_Dynamic Programming.pptx
Minakshee Patil
 
PPTX
Introduction to Computational Complexity Theory pptx
Minakshee Patil
 
PPTX
Analysis of Algorithms (1).pptx, asymptotic
Minakshee Patil
 
PPTX
0-1_knapsack_using_DP, types of knapsack
Minakshee Patil
 
PPT
Linear Data Structures, array, stack, queue
Minakshee Patil
 
PPTX
Unit 5-BACKTRACKING- n queens, sum of subset, graph coloring problems
Minakshee Patil
 
PPT
stack, opeartions on stack, applications of stack
Minakshee Patil
 
PPTX
Algorithm Design Techiques, divide and conquer
Minakshee Patil
 
PPTX
Analysis of Algorithms, recurrence relation, solving recurrences
Minakshee Patil
 
PPT
Lecture2 (9).ppt
Minakshee Patil
 
PPTX
oracle.pptx
Minakshee Patil
 
PPT
Lecture1.ppt
Minakshee Patil
 
PPT
Unit 1.ppt
Minakshee Patil
 
PPTX
Hierarchical clustering algorithm.pptx
Minakshee Patil
 
PPT
Lecture2 (1).ppt
Minakshee Patil
 
PPT
Lecture3 (3).ppt
Minakshee Patil
 
PPT
Lecture4.ppt
Minakshee Patil
 
Introduction, characteristics, Pseudocode.pptx
Minakshee Patil
 
0-1_knapsack_using_Dynamic Programming.pptx
Minakshee Patil
 
Introduction to Computational Complexity Theory pptx
Minakshee Patil
 
Analysis of Algorithms (1).pptx, asymptotic
Minakshee Patil
 
0-1_knapsack_using_DP, types of knapsack
Minakshee Patil
 
Linear Data Structures, array, stack, queue
Minakshee Patil
 
Unit 5-BACKTRACKING- n queens, sum of subset, graph coloring problems
Minakshee Patil
 
stack, opeartions on stack, applications of stack
Minakshee Patil
 
Algorithm Design Techiques, divide and conquer
Minakshee Patil
 
Analysis of Algorithms, recurrence relation, solving recurrences
Minakshee Patil
 
Lecture2 (9).ppt
Minakshee Patil
 
oracle.pptx
Minakshee Patil
 
Lecture1.ppt
Minakshee Patil
 
Unit 1.ppt
Minakshee Patil
 
Hierarchical clustering algorithm.pptx
Minakshee Patil
 
Lecture2 (1).ppt
Minakshee Patil
 
Lecture3 (3).ppt
Minakshee Patil
 
Lecture4.ppt
Minakshee Patil
 
Ad

Recently uploaded (20)

PDF
SG1-ALM-MS-EL-30-0008 (00) MS - Isolators and disconnecting switches.pdf
djiceramil
 
PPTX
Basics of Auto Computer Aided Drafting .pptx
Krunal Thanki
 
PPTX
Precedence and Associativity in C prog. language
Mahendra Dheer
 
PPTX
Water resources Engineering GIS KRT.pptx
Krunal Thanki
 
PPTX
Sensor IC System Design Using COMSOL Multiphysics 2025-July.pptx
James D.B. Wang, PhD
 
PPTX
Unit 2 Theodolite and Tachometric surveying p.pptx
satheeshkumarcivil
 
PPTX
Unit II: Meteorology of Air Pollution and Control Engineering:
sundharamm
 
PDF
2025 Laurence Sigler - Advancing Decision Support. Content Management Ecommer...
Francisco Javier Mora Serrano
 
PDF
Farm Machinery and Equipments Unit 1&2.pdf
prabhum311
 
PDF
Natural Language processing and web deigning notes
AnithaSakthivel3
 
PPTX
Ground improvement techniques-DEWATERING
DivakarSai4
 
PPTX
ETP Presentation(1000m3 Small ETP For Power Plant and industry
MD Azharul Islam
 
PPTX
filteration _ pre.pptx 11111110001.pptx
awasthivaibhav825
 
PDF
NOISE CONTROL ppt - SHRESTH SUDHIR KOKNE
SHRESTHKOKNE
 
PDF
Air -Powered Car PPT by ER. SHRESTH SUDHIR KOKNE.pdf
SHRESTHKOKNE
 
PPTX
sunil mishra pptmmmmmmmmmmmmmmmmmmmmmmmmm
singhamit111
 
PDF
July 2025 - Top 10 Read Articles in Network Security & Its Applications.pdf
IJNSA Journal
 
PDF
Zero Carbon Building Performance standard
BassemOsman1
 
PPTX
NEBOSH HSE Process Safety Management Element 1 v1.pptx
MohamedAli92947
 
PDF
Web Technologies - Chapter 3 of Front end path.pdf
reemaaliasker
 
SG1-ALM-MS-EL-30-0008 (00) MS - Isolators and disconnecting switches.pdf
djiceramil
 
Basics of Auto Computer Aided Drafting .pptx
Krunal Thanki
 
Precedence and Associativity in C prog. language
Mahendra Dheer
 
Water resources Engineering GIS KRT.pptx
Krunal Thanki
 
Sensor IC System Design Using COMSOL Multiphysics 2025-July.pptx
James D.B. Wang, PhD
 
Unit 2 Theodolite and Tachometric surveying p.pptx
satheeshkumarcivil
 
Unit II: Meteorology of Air Pollution and Control Engineering:
sundharamm
 
2025 Laurence Sigler - Advancing Decision Support. Content Management Ecommer...
Francisco Javier Mora Serrano
 
Farm Machinery and Equipments Unit 1&2.pdf
prabhum311
 
Natural Language processing and web deigning notes
AnithaSakthivel3
 
Ground improvement techniques-DEWATERING
DivakarSai4
 
ETP Presentation(1000m3 Small ETP For Power Plant and industry
MD Azharul Islam
 
filteration _ pre.pptx 11111110001.pptx
awasthivaibhav825
 
NOISE CONTROL ppt - SHRESTH SUDHIR KOKNE
SHRESTHKOKNE
 
Air -Powered Car PPT by ER. SHRESTH SUDHIR KOKNE.pdf
SHRESTHKOKNE
 
sunil mishra pptmmmmmmmmmmmmmmmmmmmmmmmmm
singhamit111
 
July 2025 - Top 10 Read Articles in Network Security & Its Applications.pdf
IJNSA Journal
 
Zero Carbon Building Performance standard
BassemOsman1
 
NEBOSH HSE Process Safety Management Element 1 v1.pptx
MohamedAli92947
 
Web Technologies - Chapter 3 of Front end path.pdf
reemaaliasker
 
Ad

Different Searching and Sorting Methods.pptx

  • 1. Unit 2 Searching and Sorting Methods
  • 2. Agenda • Linear Search, Binary Search • Bubble Sort, Merge Sort • Quick Sort, Selection Sort • Insertion Sort, Radix Sort, Bucket Sort • Complexity Issues of algorithms
  • 3. Linear Search  Linear Search is defined as a sequential search algorithm that starts at one end and goes through each element of a list until the desired element is found, otherwise the search continues till the end of the data set. It is the easiest searching algorithm. Algorithm Linear(A,n,x) { for(i:=0;i<n;i++) { If(a[i]=x) then return i; } return -1; }
  • 4. Complexity Analysis : Summary 4 Case Number of key comparisons Asymptotic complexity Remark Case 1 T(n) = 1 T(n) = O(1) Best case Case 2 T(n) = n T(n) = O(n) Worst case Case 3 T(n) = O(n) Average case 2 1 ) (   n n T
  • 8. Example -15,-6,0,7, 9, 23,54, 82,101,112,125, 131,142,151 and x=151, x=-14, x=9
  • 9. Complexity Analysis:  Best Case: In Binary search, best case occurs when the element to search is found in first comparison, i.e., when the first middle element itself is the element to be searched. The best-case time complexity of Binary search is O(1).  Average Case : The average case time complexity of Binary search is O(logn)  Worst Case: In Binary search, the worst case occurs, when we have to keep reducing the search space till it has only one element. The worst-case time complexity of Binary search is O(logn).
  • 10. Sorting  Sorting takes an unordered collection and makes it an ordered one. 5 12 35 42 77 101 1 2 3 4 5 6 5 12 35 42 77 101 1 2 3 4 5 6
  • 11. Bubble Sort  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 5 12 35 42 77 101 1 2 3 4 5 6
  • 12. Example : 72 42 35 12 101 5 Pass 1 Pass 2 42 72 35 12 101 5 35 42 12 72 5 101 42 35 72 12 101 5 35 12 42 72 5 101 42 35 12 72 101 5 35 12 42 72 5 101 42 35 12 72 101 5 35 12 42 5 72 101 42 35 12 72 5 101 Pass 3 Pass 4 12 35 42 5 72 101 12 35 5 42 72 101 12 35 42 5 72 101 12 5 35 42 72 101 12 35 5 42 72 101 Pass 5 5 12 35 42 72 101
  • 13. “Bubbling” All the Elements 77 12 35 42 5 1 2 3 4 5 6 101 5 42 12 35 77 1 2 3 4 5 6 101 42 5 35 12 77 1 2 3 4 5 6 101 42 35 5 12 77 1 2 3 4 5 6 101 42 35 12 5 77 1 2 3 4 5 6 101 N - 1
  • 14. Reducing the Number of Comparisons 12 35 42 77 101 1 2 3 4 5 6 5 77 12 35 42 5 1 2 3 4 5 6 101 5 42 12 35 77 1 2 3 4 5 6 101 42 5 35 12 77 1 2 3 4 5 6 101 42 35 5 12 77 1 2 3 4 5 6 101
  • 15. Bubble Sort Algorithm Algorithm Bubble_Sort(a, n) { for i := 1 to n – 1 do { // to keep track of the number of iterations for j := 0 to n –i-1 { // to compare the elements inside the particular iteration if (a[j] > a[j + 1]) then swap(a[j], a[j + 1]); // swap if any element is greater than its adjacent element } } }
  • 16. Complexity Analysis • Best Case: The best case occurs when the array is already sorted. So the number of comparisons required is N-1 and the number of swaps required = 0. Hence the best case complexity is O(N). • Worst Case: The worst-case condition for bubble sort occurs when elements of the array are arranged in decreasing order. In the worst case, the total number of iterations or passes required to sort a given array is (N-1). where ‘N’ is the number of elements present in the array. • In each iteration Total number of swaps = Total number of comparison • Total number of comparison (Worst case) = N(N-1)/2 Total number of swaps (Worst case) = N(N-1)/2 • So worst case and average case time complexity is O(N2 ) as N2 is the highest order term.
  • 17. Merge Sort  array A[1..n] of n elements given, idea is to split the array in two sets say A[1]…… A[n/2] and A[n/2 + 1]……A[n].  Sort two sets of arrays individually.  Merge two sorted sets of to produce a single sorted list of n elements as follows: • Repeat the following until no elements remain in one of the arrays: -compare the first elements in the remaining unprocessed portions of the arrays - copy the smaller of the two into A, while incrementing the index indicating the unprocessed portion of that array • Once all elements in one of the arrays are processed, copy the remaining unprocessed elements from the other array into A
  • 20. Example a[1:10]=(310, 285, 179, 652, 351, 423, 861, 254, 450, 520) • Hand simulation • (310 285 179 652 351 | 423 861 254 450 520) split • (310 285 179 | 652 351 | 423 861 254 450 520) split • (310 285 | 179 | 652 351 | 423 861 254 450 520) split • (310 | 285 | 179 | 652 351 | 423 861 254 450 520) split • (285 310 | 179 | 652 351 | 423 861 254 450 520) merge • (179 285 310 | 652 351 | 423 861 254 450 520) merge • (179 285 310 | 652 | 351 | 423 861 254 450 520) split • (179 285 310 | 351 652 | 423 861 254 450 520) merge • (179 285 310 351 652 | 423 861 254 450 520) merge • (179 285 310 351 652 | 423 861 254 | 450 520) split • ……
  • 21. Tree of calls of Merge Sort a[1:10]=(310, 285, 179, 652, 351, 423, 861, 254, 450, 520)
  • 22. Tree of calls of Merge a[1:10]=(310, 285, 179, 652, 351, 423, 861, 254, 450, 520)
  • 23. Example: a[1:8] = 50 10 25 30 15 70 35 55  Tree of calls of MergeSort  Tree of calls of Merge
  • 24. Analysis of Mergesort  All cases have same efficiency O(n log n)  Space complexity O(n)
  • 25. Quick Sort  Partitioning a[1:n] into two subarrays in such a way that sorted subarrays do not need to be merged later.  This is accomplished by rearranging the elements in a[1:n] such that a[i] <= a[j] for all i between 1 to m, and for all j between m+1 to n where 1<=m<=n.  Thus, the elements in a[1:m] and a[m+1:n] can be sorted independently  No need for merging  The rearrangement of the elements is accomplished by picking some element of a[],say t = a[s],and then reordering the other elements so that all elements appearing before t in a[1:n] are less than or equal to t and all elements appearing after t are greater than or equal to t.  This rearranging is referred to as partitioning.
  • 27. Example 15 22 13 27 12 10 20 25
  • 28. Quick Sort Algorithm Algorithm Quicksort(first, last) { If(first < last) { J:= Partition(a, first, last); Quicksort(first, j-1); Quicksort(j+1, last); } }
  • 29. Partition Algorithm Algorithm Partition(a, m, p) { v:= a[m], i:= m, j:= p; while(i < j) { while(a[i]<=v) i++; while(a[j]> v) J--; if(i<j) { temp:= a[i]; a[i]:= a[j]; a[j] := temp; } } a[m]:= a[j]; a[j]= v; return j; }
  • 30. Analysis of Quicksort  Worst-case time is  The average time is O(n log n).
  • 31. Selection Sort  In selection sort, the smallest value among the unsorted elements of the array is selected in every pass and inserted to its appropriate position into the array.  In this algorithm, the array is divided into two parts, first is sorted part, and another one is the unsorted part.  Initially, the sorted part of the array is empty, and unsorted part is the given array. Sorted part is placed at the left, while the unsorted part is placed at the right.  In selection sort, the first smallest element is selected from the unsorted array and placed at the first position. After that second smallest element is selected and placed in the second position. The process continues until the array is entirely sorted.
  • 32. Example 12 29 25 8 32 17 40 8 29 25 12 32 17 40 8 12 25 29 32 17 40 8 12 17 29 32 25 40 8 12 17 25 32 29 40 8 12 17 25 29 32 40 8 12 17 25 29 32 40
  • 33. Selection Sort Algorithm Algorithm Selectionsort(a,n) { for i:=1 to n-1 do { pos := i ; pos:=Smallest(a, i, n, pos) ; swap( a[i], a[pos] ); } }
  • 34. Algorithm Smallest Algorithm Smallest (a, i, n, pos) small := a[i] ; for j := i+1 to n do { if (small > a[j]) then { small = a[j] ; pos := j ; } } return pos; }
  • 35. Analysis of Selection Sort  Best case, average case and worst case time complexity is
  • 36. Insertion Sort  Insertion sort, is an efficient algorithm for sorting a small number of elements.  Insertion sort works the way many people sort a hand of playing cards.  We start with an empty left hand and the cards face down on the table.  We then remove one card at a time from the table and insert it into the correct position in the left hand.  To find the correct position for a card, we compare it with each of the cards already in the hand, from right to left.
  • 39. Analysis  Best case time complexity is Θ(n)  Average case and worst case time complexity is
  • 40. Radix Sort  Digit-by-digit sort.  Idea: Sort on least-significant digit first with auxiliary stable sort.  Go from least to most significant digit.
  • 42. Radix Sort Example 170, 45, 75, 90, 802, 24, 2, 66
  • 44. Bucket Sort  Bucket sort assumes that the input is generated by a random process that distributes elements uniformly and independently over the interval (0, 1).  Bucket sort divides the interval (0, 1) into n equal-sized subintervals, or buckets, and then distributes the n input numbers into the buckets.  Since the inputs are uniformly and independently distributed over (0, 1), we do not expect many numbers to fall into each bucket.  To produce the output, we simply sort the numbers in each bucket and then go through the buckets in order, listing the elements in each.
  • 47. Example 0.13, 0.8, 0.2, 0.7, 0.16, 0.18, 0.12, 0.1, 0.23, 0.11