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
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
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)