Module 04
Module 04
L
Partha Pratim
Das
Programming in Modern C++
E
Objectives &
Outline
Module M04: Sorting and Searching
T
Sorting
Bubble Sort
Standard Library
P
Searching
Standard Library Partha Pratim Das
N
STL: algorithm
All url’s in this module have been accessed in September, 2021 and found to be functional
Module M04
• Working with variable sized arrays is more flexible with vectors in C++
L
Partha Pratim
Das
• String operations are easier with C++ standard library
E
Objectives &
Outline
T
Sorting
Bubble Sort
Standard Library
P
Searching
Standard Library
N
STL: algorithm
Module Summary
Module M04
L
Partha Pratim
Das
E
Objectives &
Outline
T
Sorting
Bubble Sort
Standard Library
P
Searching
Standard Library
N
STL: algorithm
Module Summary
Module M04
L
Partha Pratim
Das
1 Sorting in C and C++
E
Objectives &
Outline
Bubble Sort
Using Standard Library
T
Sorting
Bubble Sort
Standard Library
P
Searching
Standard Library
2 Searching in C and C++
N
STL: algorithm
Using Standard Library
Module Summary
4 Module Summary
Module M04
L
Partha Pratim
Das
E
Objectives &
Outline
T
Sorting
Bubble Sort
Standard Library
P
Searching
Standard Library
N
STL: algorithm
Module Summary
L
Partha Pratim #include <stdio.h> #include <iostream>
Das using namespace std;
int main() { int data[] = {32, 71, 12, 45, 26}; int main() { int data[] = {32, 71, 12, 45, 26};
E
Objectives &
Outline
int i, step, n = 5, temp; int n = 5, temp;
for(step = 0; step < n - 1; ++step) for(int step = 0; step < n - 1; ++step)
T
Sorting for(i = 0; i < n-step-1; ++i) { for(int i = 0;i < n-step-1; ++i) {
Bubble Sort
if(data[i] > data[i+1]) { if (data[i] > data[i+1]) {
Standard Library
P
temp = data[i]; temp = data[i];
Searching data[i] = data[i+1]; data[i] = data[i+1];
Standard Library data[i+1] = temp; data[i+1] = temp;
N
STL: algorithm } }
} }
Module Summary
12 26 32 45 71 12 26 32 45 71
• Implementation is same in both C and C++ apart from differences in header files, I/O functions explained in Module 02
L
Partha Pratim
Das #include <stdlib.h> // qsort function #include <algorithm> // sort function
using namespace std;
E
Objectives & // compare Function Pointer // compare Function Pointer
Outline int compare( bool compare(
T
Sorting const void *a, const void *b) { // Type unsafe int i, int j) { // Type safe
Bubble Sort return (*(int*)a < *(int*)b); // Cast needed return (i > j); // No cast needed
Standard Library } }
P
Searching int main () { int data[] = {32, 71, 12, 45, 26}; int main() { int data[] = {32, 71, 12, 45, 26};
Standard Library // Start ptr., # elements, size, func. ptr. // Start ptr., end ptr., func. ptr.
N
STL: algorithm
qsort(data, 5, sizeof(int), compare); sort(data, data+5, compare);
Module Summary
for(int i = 0; i < 5; i++) for (int i = 0; i < 5; i++)
printf ("%d ", data[i]); cout << data[i] << " ";
} }
71 45 32 26 12 71 45 32 26 12
• sizeof(int) and compare function passed to qsort • Only compare passed to sort. No size is needed
• Only Size is inferred from the type int of data
• compare function is type unsafe & needs complicated cast • compare function is type safe & simple with no cast
Programming in Modern C++ Partha Pratim Das M04.7
Program 04.03: Using default sort of algorithm
Module M04
C++ Program (Asc Order)
L
Partha Pratim
Das // sort.cpp
#include <iostream>
E
Objectives & #include <algorithm> // sort function
Outline
using namespace std;
T
Sorting
Bubble Sort int main () {
Standard Library int data[] = {32, 71, 12, 45, 26};
P
Searching
Standard Library sort(data, data+5);
N
STL: algorithm
for (int i = 0; i < 5; i++)
Module Summary cout << data[i] << " ";
return 0;
}
12 26 32 45 71
• Sort using the default sort function of algorithm library which does the sorting in ascending order only
• No compare function is needed
Programming in Modern C++ Partha Pratim Das M04.8
Searching in C and C++
Module M04
L
Partha Pratim
Das
E
Objectives &
Outline
T
Sorting
Bubble Sort
Standard Library
P
Searching
Standard Library
N
STL: algorithm
Module Summary
L
Partha Pratim #include <stdio.h> #include <iostream>
Das #include <stdlib.h> // bsearch function #include <algorithm> // binary_search function
using namespace std;
E
Objectives &
Outline
// compare Function Pointer
int compare(
T
Sorting const void * a, const void * b) { // Type unsafe
Bubble Sort
if (*(int*)a<*(int*)b) return -1; // Cast needed
Standard Library
P
if (*(int*)a==*(int*)b) return 0; // Cast needed
Searching if (*(int*)a>*(int*)b) return 1; // Cast needed
Standard Library }
N
STL: algorithm int main () { int data[] = {1,2,3,4,5}, k = 3; int main() { int data[] = {1,2,3,4,5}, k = 3;
Module Summary
if (bsearch(&k, data, 5, sizeof(int), compare)) if (binary_search(data, data+5, k))
printf("found!\n"); cout << "found!\n";
else printf("not found\n"); else cout << "not found\n";
} }
found! found!
• compare function is type unsafe & needs complicated cast • No compare function needed
Module M04
L
Partha Pratim
Das
E
Objectives &
Outline
T
Sorting
Bubble Sort
Standard Library
P
Searching
Standard Library
N
STL: algorithm
Module Summary
Module M04
The algorithm library of c++ helps us to easily implement commonly used complex
L
Partha Pratim
Das functions. We discussed the functions for sort and search. Let us look at some more useful
functions.
E
Objectives &
Outline
• Replace element in an array
T
Sorting
Bubble Sort
Standard Library
• Rotates the order of the elements
P
Searching
Standard Library
N
STL: algorithm
Module Summary
Module M04
Replace Rotate
L
Partha Pratim
Das // Replace.cpp // Rotate.cpp
#include <iostream> #include <iostream>
E
Objectives & #include <algorithm> // replace function #include <algorithm> // rotate function
Outline
using namespace std; using namespace std;
T
Sorting
Bubble Sort int main() { int main() {
Standard Library int data[] = {1, 2, 3, 4, 5}; int data[] = {1, 2, 3, 4, 5};
P
Searching
Standard Library replace(data, data+5, 3, 2); rotate(data, data+2, data+5);
N
STL: algorithm
for(int i = 0; i < 5; ++i) for(int i = 0; i < 5; ++i)
Module Summary cout << data[i] << " "; cout << data[i] << " ";
return 0; return 0;
} }
1 2 2 4 5 3 4 5 1 2
• 3rd element replaced with 2 • Array circular shifted around 3rd element
Module M04
L
Partha Pratim
Das
search functions defined in the algorithm library
E
Objectives &
Outline • Predefined optimised versions of these sort and search functions can also be used
T
Sorting
Bubble Sort
• There are a number of useful functions like rotate, replace, merge, swap, remove
Standard Library etc. in algorithm library
P
Searching
Standard Library
N
STL: algorithm
Module Summary