SlideShare a Scribd company logo
Searching & Sorting
• Searching
Searching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structures
Linear Searching
Searching and sorting Techniques in Data structures
Linear Search Algorithm
Advantages & Disadvantage
When to use Linear Search?
• When we are dealing with a small dataset.
• When you are searching for a dataset stored in
contiguous memory.
Linear Searching
Linear Searching
Searching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structures
Analysis of Linear search
Example:2
Consider the array arr[] = {10, 50, 30, 70, 80,
20, 90, 40} and key = 30
// C code to linearly search x in arr[].
#include <stdio.h>
int search(int arr[], int N, int x)
{
for (int i = 0; i < N; i++)
if (arr[i] == x)
return i;
return -1; }
// Driver code
int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10;
int N = sizeof(arr) / sizeof(arr[0]);
// Function call
int result = search(arr, N, x);
(result == -1)
? printf("Element is not present in array")
: printf("Element is present at index %d", result);
return 0; }
Binary Search
Binary Search
Binary Search
Divide & Conquer
Binary Search Algorithm
Example: BS
Searching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structures
Example 2
Time complexity
Advantages
Disadvantages
Application of Binary Search
Bubble sort
Bubble sort
Bubble sort: orders a list of values by repetitively
comparing neighboring elements and swapping their
positions if necessary
• more specifically:
– scan the list, exchanging adjacent elements if they are
not in relative order; this bubbles the highest value to
the top
– scan the list again, bubbling up the second highest value
– repeat until all elements have been placed in their
proper order
33
• 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
0 1 2 3 4 5
Swap
42 77
"Bubbling" largest element
34
"Bubbling" 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
5
12
35
77
42 101
0 1 2 3 4 5
Swap
35 77
35
"Bubbling" 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
5
12
77
35
42 101
0 1 2 3 4 5
Swap
12 77
36
"Bubbling" 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
5
77
12
35
42 101
0 1 2 3 4 5
No need to swap
37
"Bubbling" 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
5
77
12
35
42 101
0 1 2 3 4 5
Swap
5 101
38
"Bubbling" 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
77
12
35
42 5
0 1 2 3 4 5
101
Largest value correctly placed
39
Bubble sort code
public static void bubbleSort(int a[])
{
for (int i = 0; i < a.length; i++)
{
for (int j = 1; j < a.length - i; j++)
{
// swap adjacent out-of-order elements
if (a[j-1] > a[j]) {
swap(a, j-1, j);
}
}
}
} 40
Example :1
Input: arr[] = {6, 3, 0, 5}
First Pass:
• The largest element is placed in its correct
position, i.e., the end of the array.
Second Pass:
• Place the second largest element at correct
position
Third Pass:
• Place the remaining two elements at their
correct positions.
Total no. of passes: n-1
• Total no. of comparisons: n*(n-1)/2
Complexity Analysis of
Bubble Sort:
Time Complexity: O(N2)
Auxiliary Space: O(1)
Implementation of Bubble sort
#include <stdbool.h>
#include <stdio.h>
void swap(int* xp, int* yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
// An optimized version of Bubble Sort
void bubbleSort(int arr[], int n)
{
int i, j;
bool swapped;
for (i = 0; i < n - 1; i++) {
swapped = false;
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(&arr[j], &arr[j + 1]);
swapped = true;
} }
// If no two elements were swapped by inner loop,
// then break
if (swapped == false)
break;
}}
/ Function to print an array
void printArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
printf("%d ", arr[i]);
}
// Driver program to test above
functions
int main()
{
int arr[] = { 64, 34, 25, 12, 22,
11, 90 };
int n = sizeof(arr) /
sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array: n");
printArray(arr, n);
return 0;
}
Output:Sorted array: 11 12 22 25 34 64 90
Advantages of Bubble Sort:
• Bubble sort is easy to understand and implement.
• It does not require any additional memory space.
• It is a stable sorting algorithm, meaning that elements
with the same key value maintain their relative order
in the sorted output.
• Disadvantages of Bubble Sort:
• Bubble sort has a time complexity of O(N2) which
makes it very slow for large data sets.
• Bubble sort is a comparison-based sorting algorithm,
which means that it requires a comparison operator to
determine the relative order of elements in the input
data set. It can limit the efficiency of the algorithm in
certain cases.
Selection sort
• selection sort: orders a list of values by
repetitively putting a particular value into its
final position
• more specifically:
• find the smallest value in the list
• switch it with the value in the first position
• find the next smallest value in the list
• switch it with the value in the second position
• repeat until all values are in their proper places
48
Selection sort example
49
Index
0 1 2 3 4 5 6 7
Value
27 63 1 72 64 58 14 9
1st pass
1 63 27 72 64 58 14 9
2nd pass
1 9 27 72 64 58 14 63
3rd pass
1 9 14 72 64 58 27 63
…
Selection sort example 2
50
Selection sort code
public static void selectionSort(int[] a) {
for (int i = 0; i < a.length; i++) {
// find index of smallest element
int min = i;
for (int j = i + 1; j < a.length; j++) {
if (a[j] < a[min]) {
min = j;
}
}
// swap smallest element with a[i]
swap(a, i, min);
} 51
Insertion sort
Insertion sort
• insertion sort: orders a list of values by repetitively
inserting a particular value into a sorted subset of the
list
• more specifically:
– consider the first item to be a sorted sublist of length 1
– insert the second item into the sorted sublist, shifting the first
item if needed
– insert the third item into the sorted sublist, shifting the other
items as needed
– repeat until all values have been inserted into their proper
positions
53
Insertion sort
• Simple sorting algorithm.
• n-1 passes over the array
• At the end of pass i, the elements that occupied A[0]…A[i]
originally are still in those spots and in sorted order.
2 8 15 1 17 10 12 5
0 1 2 3 4 5 6 7
1 2 8 15 17 10 12 5
0 1 2 3 4 5 6 7
after
pass 2
after
pass 3
2 15 8 1 17 10 12 5
0 1 2 3 4 5 6 7
54
Insertion sort example
55
Insertion sort code
public static void insertionSort(int[] a) {
for (int i = 1; i < a.length; i++) {
int temp = a[i];
// slide elements down to make room for a[i]
int j = i;
while (j > 0 && a[j - 1] > temp) {
a[j] = a[j - 1];
j--;
}
a[j] = temp;
}
}
56
Comparing sorts
• We've seen "simple" sorting algos. so far, such as:
– selection sort
– insertion sort
• They all use nested loops and perform approximately n2
comparisons
• They are relatively inefficient
comparisons swaps
selection n2/2 n
insertion
worst: n2/2
best: n
worst: n2/2
best: n
57
Average Case Analysis
• Given an array A of elements, an inversion is an
ordered pair (i, j) such that i < j, but
A[i] > A[j].(out of order elements)
• Assume no duplicate elements.
• Theorem: The average number of inversions in
an array of n distinct elements is n (n - 1) / 4.
• Corollary: Any algorithm that sorts by
exchanging adjacent elements requires O(n2)
time on average.
58
Shell Sort
Shell Sort description
• shell sort: orders a list of values by comparing
elements that are separated by a gap of >1 indexes
– a generalization of insertion sort
– invented by computer scientist Donald Shell in 1959
• based on some observations about insertion sort:
– insertion sort runs fast if the input is almost sorted
– insertion sort's weakness is that it swaps each element
just one step at a time, taking many swaps to get the
element into its correct position
60
Shell sort example
• Idea: Sort all elements that are 5 indexes apart,
then sort all elements that are 3 indexes apart, ...
61
Shell sort code
public static void shellSort(int[] a)
{
for (int gap = a.length / 2; gap > 0; gap /= 2)
{
for (int i = gap; i < a.length; i++)
{
// slide element i back by gap indexes
// until it's "in order"
int temp = a[i];
int j = i;
while (j >= gap && temp < a[j - gap]) {
a[j] = a[j – gap];
j -= gap;
}
a[j] = temp;
}
}
}
62
Sorting practice problem
• Consider the following array of int values.
[22, 11, 34, -5, 3, 40, 9, 16, 6]
(a) Write the contents of the array after 3 passes of the outermost loop of bubble
sort.
(b) Write the contents of the array after 5 passes of the outermost loop of
insertion sort.
(c) Write the contents of the array after 4 passes of the outermost loop of
selection sort.
(d) Write the contents of the array after 1 pass of shell sort, using gap = 3.
(e) Write the contents of the array after a pass of bogo sort. (Just kidding.)
63
Example:
• In the first loop, n is equal to 8 (size of the array),
so the elements are lying at the interval of 4 (n/2
= 4). Elements will be compared and swapped if
they are not in order
• At the interval of 4, the sublists are {33, 12}, {31,
17}, {40, 25}, {8, 42}.
• In the second loop, elements are lying at the
interval of 2 (n/4 = 2), where n = 8.
• With an interval of 2, two sublists will be
generated - {12, 25, 33, 40}, and {17, 8, 31, 42}.
• In the third loop, elements are lying at the interval
of 1 (n/8 = 1), where n = 8
Time Complexity
Case Time Complexity
Best Case O(n*logn)
Average Case O(n*log(n)
2
)
Worst Case O(n
2
)
Radix Sort
Radix Sort
• Radix sort is one of the sorting algorithms used to sort a
list of integer numbers in order.
• In radix sort algorithm, a list of integer numbers will be
sorted based on the digits of individual numbers.
• Sorting is performed from least significant digit to the
most significant digit.
Step by Step Process
• Step 1 - Define 10 queues each representing a
bucket for each digit from 0 to 9.
• Step 2 - Consider the least significant digit of
each number in the list which is to be sorted.
• Step 3 - Insert each number into their respective
queue based on the least significant digit.
• Step 4 - Group all the numbers from queue 0 to
queue 9 in the order they have inserted into their
respective queues.
• Step 5 - Repeat from step 3 based on the next
least significant digit.
• Step 6 - Repeat from step 2 until all the numbers
are grouped based on the most significant digit.
Example :1
Searching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structures
Sorted
Example:2
Searching and sorting Techniques in Data structures
Complexity of the Radix
Sort Algorithm
• To sort an unsorted list with 'n' number of
elements, Radix sort algorithm needs the
following complexities...
WorstCase:O(n)
BestCase:O(n)
Average Case : O(n)
Hashing
Searching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structures
102
Double Hashing Example
0
1
2
3
4
5
6 76
76
0
1
2
3
4
5
6
93
76
93
0
1
2
3
4
5
6
93
40
76
40
0
1
2
3
4
5
6
47
93
40
76
47
0
1
2
3
4
5
6
47
93
10
40
76
10
0
1
2
3
4
5
6
47
93
10
55
40
76
55
h(k) = k mod 7 and g(k) = 5 – (k mod 5)
Probes 1 1 1 2 1 2
Searching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structures

More Related Content

Similar to Searching and sorting Techniques in Data structures (20)

PPT
search_sort Search sortSearch sortSearch sortSearch sort
Shanmuganathan C
 
PPTX
Data Structures - Lecture 8 [Sorting Algorithms]
Muhammad Hammad Waseem
 
PPTX
Sorting Data structure And Algorithm.pptx
subhanalichand514
 
PPT
Sorting algorithms
CHANDAN KUMAR
 
PPT
search_sort.ppt
SwatiHans10
 
PPTX
Chapter-2.pptx
selemonGamo
 
PDF
Searching and Sorting in Data Structures with examples
vimalaimogen5850
 
PPTX
Different Searching and Sorting Methods.pptx
Minakshee Patil
 
PPT
simple-sorting algorithms
Ravirajsinh Chauhan
 
PPSX
Data Structure and Algorithm Chapter 2.ppsx Chapter 2.ppsx
SolomonEndalu
 
PPT
sorting_part1.ppt
ReehaamMalikArain
 
PDF
Sorting
Kariman Karm Gabaa
 
PPT
Data Structure (MC501)
Kamal Singh Lodhi
 
PPTX
Unit 7 sorting
Dabbal Singh Mahara
 
PPTX
Chapter 2. data structure and algorithm
SolomonEndalu
 
PPTX
Sorting method data structure
sunilchute1
 
DOCX
Sorting
BHARATH KUMAR
 
PPTX
sorting.pptx
DrRanjeetKumar51721
 
PDF
Advanced Topics In Java Core Concepts In Data Structures Noel Kalicharan
fickolatigo
 
PPTX
Lecture 13 data structures and algorithms
Aakash deep Singhal
 
search_sort Search sortSearch sortSearch sortSearch sort
Shanmuganathan C
 
Data Structures - Lecture 8 [Sorting Algorithms]
Muhammad Hammad Waseem
 
Sorting Data structure And Algorithm.pptx
subhanalichand514
 
Sorting algorithms
CHANDAN KUMAR
 
search_sort.ppt
SwatiHans10
 
Chapter-2.pptx
selemonGamo
 
Searching and Sorting in Data Structures with examples
vimalaimogen5850
 
Different Searching and Sorting Methods.pptx
Minakshee Patil
 
simple-sorting algorithms
Ravirajsinh Chauhan
 
Data Structure and Algorithm Chapter 2.ppsx Chapter 2.ppsx
SolomonEndalu
 
sorting_part1.ppt
ReehaamMalikArain
 
Data Structure (MC501)
Kamal Singh Lodhi
 
Unit 7 sorting
Dabbal Singh Mahara
 
Chapter 2. data structure and algorithm
SolomonEndalu
 
Sorting method data structure
sunilchute1
 
Sorting
BHARATH KUMAR
 
sorting.pptx
DrRanjeetKumar51721
 
Advanced Topics In Java Core Concepts In Data Structures Noel Kalicharan
fickolatigo
 
Lecture 13 data structures and algorithms
Aakash deep Singhal
 

Recently uploaded (20)

PPTX
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
PPTX
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
PPTX
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
PDF
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
PDF
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
PDF
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
PPTX
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
PDF
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
PDF
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
PDF
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
DOCX
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
DOCX
8th International Conference on Electrical Engineering (ELEN 2025)
elelijjournal653
 
PPTX
Introduction to Design of Machine Elements
PradeepKumarS27
 
PPTX
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
PPTX
Damage of stability of a ship and how its change .pptx
ehamadulhaque
 
PPTX
Evaluation and thermal analysis of shell and tube heat exchanger as per requi...
shahveer210504
 
PDF
smart lot access control system with eye
rasabzahra
 
PPTX
Thermal runway and thermal stability.pptx
godow93766
 
PPT
Carmon_Remote Sensing GIS by Mahesh kumar
DhananjayM6
 
PPTX
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
8th International Conference on Electrical Engineering (ELEN 2025)
elelijjournal653
 
Introduction to Design of Machine Elements
PradeepKumarS27
 
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
Damage of stability of a ship and how its change .pptx
ehamadulhaque
 
Evaluation and thermal analysis of shell and tube heat exchanger as per requi...
shahveer210504
 
smart lot access control system with eye
rasabzahra
 
Thermal runway and thermal stability.pptx
godow93766
 
Carmon_Remote Sensing GIS by Mahesh kumar
DhananjayM6
 
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
Ad

Searching and sorting Techniques in Data structures

  • 10. When to use Linear Search? • When we are dealing with a small dataset. • When you are searching for a dataset stored in contiguous memory.
  • 18. Example:2 Consider the array arr[] = {10, 50, 30, 70, 80, 20, 90, 40} and key = 30
  • 19. // C code to linearly search x in arr[]. #include <stdio.h> int search(int arr[], int N, int x) { for (int i = 0; i < N; i++) if (arr[i] == x) return i; return -1; } // Driver code int main(void) { int arr[] = { 2, 3, 4, 10, 40 }; int x = 10; int N = sizeof(arr) / sizeof(arr[0]); // Function call int result = search(arr, N, x); (result == -1) ? printf("Element is not present in array") : printf("Element is present at index %d", result); return 0; }
  • 33. Bubble sort Bubble sort: orders a list of values by repetitively comparing neighboring elements and swapping their positions if necessary • more specifically: – scan the list, exchanging adjacent elements if they are not in relative order; this bubbles the highest value to the top – scan the list again, bubbling up the second highest value – repeat until all elements have been placed in their proper order 33
  • 34. • 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 0 1 2 3 4 5 Swap 42 77 "Bubbling" largest element 34
  • 35. "Bubbling" 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 5 12 35 77 42 101 0 1 2 3 4 5 Swap 35 77 35
  • 36. "Bubbling" 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 5 12 77 35 42 101 0 1 2 3 4 5 Swap 12 77 36
  • 37. "Bubbling" 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 5 77 12 35 42 101 0 1 2 3 4 5 No need to swap 37
  • 38. "Bubbling" 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 5 77 12 35 42 101 0 1 2 3 4 5 Swap 5 101 38
  • 39. "Bubbling" 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 77 12 35 42 5 0 1 2 3 4 5 101 Largest value correctly placed 39
  • 40. Bubble sort code public static void bubbleSort(int a[]) { for (int i = 0; i < a.length; i++) { for (int j = 1; j < a.length - i; j++) { // swap adjacent out-of-order elements if (a[j-1] > a[j]) { swap(a, j-1, j); } } } } 40
  • 41. Example :1 Input: arr[] = {6, 3, 0, 5}
  • 42. First Pass: • The largest element is placed in its correct position, i.e., the end of the array.
  • 43. Second Pass: • Place the second largest element at correct position
  • 44. Third Pass: • Place the remaining two elements at their correct positions.
  • 45. Total no. of passes: n-1 • Total no. of comparisons: n*(n-1)/2 Complexity Analysis of Bubble Sort: Time Complexity: O(N2) Auxiliary Space: O(1)
  • 46. Implementation of Bubble sort #include <stdbool.h> #include <stdio.h> void swap(int* xp, int* yp) { int temp = *xp; *xp = *yp; *yp = temp; } // An optimized version of Bubble Sort void bubbleSort(int arr[], int n) { int i, j; bool swapped; for (i = 0; i < n - 1; i++) { swapped = false; for (j = 0; j < n - i - 1; j++) { if (arr[j] > arr[j + 1]) { swap(&arr[j], &arr[j + 1]); swapped = true; } } // If no two elements were swapped by inner loop, // then break if (swapped == false) break; }} / Function to print an array void printArray(int arr[], int size) { int i; for (i = 0; i < size; i++) printf("%d ", arr[i]); } // Driver program to test above functions int main() { int arr[] = { 64, 34, 25, 12, 22, 11, 90 }; int n = sizeof(arr) / sizeof(arr[0]); bubbleSort(arr, n); printf("Sorted array: n"); printArray(arr, n); return 0; } Output:Sorted array: 11 12 22 25 34 64 90
  • 47. Advantages of Bubble Sort: • Bubble sort is easy to understand and implement. • It does not require any additional memory space. • It is a stable sorting algorithm, meaning that elements with the same key value maintain their relative order in the sorted output. • Disadvantages of Bubble Sort: • Bubble sort has a time complexity of O(N2) which makes it very slow for large data sets. • Bubble sort is a comparison-based sorting algorithm, which means that it requires a comparison operator to determine the relative order of elements in the input data set. It can limit the efficiency of the algorithm in certain cases.
  • 48. Selection sort • selection sort: orders a list of values by repetitively putting a particular value into its final position • more specifically: • find the smallest value in the list • switch it with the value in the first position • find the next smallest value in the list • switch it with the value in the second position • repeat until all values are in their proper places 48
  • 50. Index 0 1 2 3 4 5 6 7 Value 27 63 1 72 64 58 14 9 1st pass 1 63 27 72 64 58 14 9 2nd pass 1 9 27 72 64 58 14 63 3rd pass 1 9 14 72 64 58 27 63 … Selection sort example 2 50
  • 51. Selection sort code public static void selectionSort(int[] a) { for (int i = 0; i < a.length; i++) { // find index of smallest element int min = i; for (int j = i + 1; j < a.length; j++) { if (a[j] < a[min]) { min = j; } } // swap smallest element with a[i] swap(a, i, min); } 51
  • 53. Insertion sort • insertion sort: orders a list of values by repetitively inserting a particular value into a sorted subset of the list • more specifically: – consider the first item to be a sorted sublist of length 1 – insert the second item into the sorted sublist, shifting the first item if needed – insert the third item into the sorted sublist, shifting the other items as needed – repeat until all values have been inserted into their proper positions 53
  • 54. Insertion sort • Simple sorting algorithm. • n-1 passes over the array • At the end of pass i, the elements that occupied A[0]…A[i] originally are still in those spots and in sorted order. 2 8 15 1 17 10 12 5 0 1 2 3 4 5 6 7 1 2 8 15 17 10 12 5 0 1 2 3 4 5 6 7 after pass 2 after pass 3 2 15 8 1 17 10 12 5 0 1 2 3 4 5 6 7 54
  • 56. Insertion sort code public static void insertionSort(int[] a) { for (int i = 1; i < a.length; i++) { int temp = a[i]; // slide elements down to make room for a[i] int j = i; while (j > 0 && a[j - 1] > temp) { a[j] = a[j - 1]; j--; } a[j] = temp; } } 56
  • 57. Comparing sorts • We've seen "simple" sorting algos. so far, such as: – selection sort – insertion sort • They all use nested loops and perform approximately n2 comparisons • They are relatively inefficient comparisons swaps selection n2/2 n insertion worst: n2/2 best: n worst: n2/2 best: n 57
  • 58. Average Case Analysis • Given an array A of elements, an inversion is an ordered pair (i, j) such that i < j, but A[i] > A[j].(out of order elements) • Assume no duplicate elements. • Theorem: The average number of inversions in an array of n distinct elements is n (n - 1) / 4. • Corollary: Any algorithm that sorts by exchanging adjacent elements requires O(n2) time on average. 58
  • 60. Shell Sort description • shell sort: orders a list of values by comparing elements that are separated by a gap of >1 indexes – a generalization of insertion sort – invented by computer scientist Donald Shell in 1959 • based on some observations about insertion sort: – insertion sort runs fast if the input is almost sorted – insertion sort's weakness is that it swaps each element just one step at a time, taking many swaps to get the element into its correct position 60
  • 61. Shell sort example • Idea: Sort all elements that are 5 indexes apart, then sort all elements that are 3 indexes apart, ... 61
  • 62. Shell sort code public static void shellSort(int[] a) { for (int gap = a.length / 2; gap > 0; gap /= 2) { for (int i = gap; i < a.length; i++) { // slide element i back by gap indexes // until it's "in order" int temp = a[i]; int j = i; while (j >= gap && temp < a[j - gap]) { a[j] = a[j – gap]; j -= gap; } a[j] = temp; } } } 62
  • 63. Sorting practice problem • Consider the following array of int values. [22, 11, 34, -5, 3, 40, 9, 16, 6] (a) Write the contents of the array after 3 passes of the outermost loop of bubble sort. (b) Write the contents of the array after 5 passes of the outermost loop of insertion sort. (c) Write the contents of the array after 4 passes of the outermost loop of selection sort. (d) Write the contents of the array after 1 pass of shell sort, using gap = 3. (e) Write the contents of the array after a pass of bogo sort. (Just kidding.) 63
  • 64. Example: • In the first loop, n is equal to 8 (size of the array), so the elements are lying at the interval of 4 (n/2 = 4). Elements will be compared and swapped if they are not in order • At the interval of 4, the sublists are {33, 12}, {31, 17}, {40, 25}, {8, 42}.
  • 65. • In the second loop, elements are lying at the interval of 2 (n/4 = 2), where n = 8. • With an interval of 2, two sublists will be generated - {12, 25, 33, 40}, and {17, 8, 31, 42}.
  • 66. • In the third loop, elements are lying at the interval of 1 (n/8 = 1), where n = 8
  • 67. Time Complexity Case Time Complexity Best Case O(n*logn) Average Case O(n*log(n) 2 ) Worst Case O(n 2 )
  • 69. Radix Sort • Radix sort is one of the sorting algorithms used to sort a list of integer numbers in order. • In radix sort algorithm, a list of integer numbers will be sorted based on the digits of individual numbers. • Sorting is performed from least significant digit to the most significant digit.
  • 70. Step by Step Process • Step 1 - Define 10 queues each representing a bucket for each digit from 0 to 9. • Step 2 - Consider the least significant digit of each number in the list which is to be sorted. • Step 3 - Insert each number into their respective queue based on the least significant digit. • Step 4 - Group all the numbers from queue 0 to queue 9 in the order they have inserted into their respective queues. • Step 5 - Repeat from step 3 based on the next least significant digit. • Step 6 - Repeat from step 2 until all the numbers are grouped based on the most significant digit.
  • 78. Complexity of the Radix Sort Algorithm • To sort an unsorted list with 'n' number of elements, Radix sort algorithm needs the following complexities... WorstCase:O(n) BestCase:O(n) Average Case : O(n)
  • 102. 102 Double Hashing Example 0 1 2 3 4 5 6 76 76 0 1 2 3 4 5 6 93 76 93 0 1 2 3 4 5 6 93 40 76 40 0 1 2 3 4 5 6 47 93 40 76 47 0 1 2 3 4 5 6 47 93 10 40 76 10 0 1 2 3 4 5 6 47 93 10 55 40 76 55 h(k) = k mod 7 and g(k) = 5 – (k mod 5) Probes 1 1 1 2 1 2