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

PPTX
Sorting Algorithms to arrange data in particular format
PDF
Sorting algorithms bubble sort to merge sort.pdf
PPT
Data Structure (MC501)
PPTX
Sorting pnk
PPT
358 33 powerpoint-slides_14-sorting_chapter-14
PPTX
2.Problem Solving Techniques and Data Structures.pptx
PPTX
All Searching and Sorting Techniques in Data Structures
PPT
Quicksort
Sorting Algorithms to arrange data in particular format
Sorting algorithms bubble sort to merge sort.pdf
Data Structure (MC501)
Sorting pnk
358 33 powerpoint-slides_14-sorting_chapter-14
2.Problem Solving Techniques and Data Structures.pptx
All Searching and Sorting Techniques in Data Structures
Quicksort

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

PPTX
sorting-160810203705.pptx
PPTX
Unit vii sorting
PPTX
9.Sorting & Searching
PDF
L 14-ct1120
PPTX
Sorting algorithms
ODP
Sorting Algorithm
PDF
Quick sort,bubble sort,heap sort and merge sort
DOCX
Sorting
PPTX
Data structure using c module 3
PDF
Analysis and Comparative of Sorting Algorithms
PPT
Sorting algorithums > Data Structures & Algorithums
PPTX
Data Structure and algorithms for software
PPTX
Data structure.pptx
PPTX
Data Structures_Searching and Sorting.pptx
PDF
Sorting algorithms
PPTX
DSA-sortijejjejjdjjdjdjjsjsjsjsjsjsjng.pptx
PPT
quicksortnmsd cmz ,z m,zmm,mbfjjjjhjhfjsg
PPT
03_sorting123456789454545454545444543.ppt
PPT
03_sorting and it's types with example .ppt
PPTX
Sorting-Algorithms-A-Comprehensive-Guide.pptx
sorting-160810203705.pptx
Unit vii sorting
9.Sorting & Searching
L 14-ct1120
Sorting algorithms
Sorting Algorithm
Quick sort,bubble sort,heap sort and merge sort
Sorting
Data structure using c module 3
Analysis and Comparative of Sorting Algorithms
Sorting algorithums > Data Structures & Algorithums
Data Structure and algorithms for software
Data structure.pptx
Data Structures_Searching and Sorting.pptx
Sorting algorithms
DSA-sortijejjejjdjjdjdjjsjsjsjsjsjsjng.pptx
quicksortnmsd cmz ,z m,zmm,mbfjjjjhjhfjsg
03_sorting123456789454545454545444543.ppt
03_sorting and it's types with example .ppt
Sorting-Algorithms-A-Comprehensive-Guide.pptx
Ad

More from Minakshee Patil (18)

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

Recently uploaded (20)

PDF
Well-logging-methods_new................
DOCX
573137875-Attendance-Management-System-original
PPT
Mechanical Engineering MATERIALS Selection
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
Geodesy 1.pptx...............................................
PPTX
web development for engineering and engineering
PDF
Digital Logic Computer Design lecture notes
PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
PPTX
Construction Project Organization Group 2.pptx
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
Foundation to blockchain - A guide to Blockchain Tech
Well-logging-methods_new................
573137875-Attendance-Management-System-original
Mechanical Engineering MATERIALS Selection
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
OOP with Java - Java Introduction (Basics)
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
bas. eng. economics group 4 presentation 1.pptx
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Geodesy 1.pptx...............................................
web development for engineering and engineering
Digital Logic Computer Design lecture notes
Arduino robotics embedded978-1-4302-3184-4.pdf
Construction Project Organization Group 2.pptx
UNIT 4 Total Quality Management .pptx
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Foundation to blockchain - A guide to Blockchain Tech

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