0% found this document useful (0 votes)
7 views

Module 04

Uploaded by

Diamond
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Module 04

Uploaded by

Diamond
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Module M04

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

Module Summary Department of Computer Science and Engineering


Indian Institute of Technology, Kharagpur

[email protected]

All url’s in this module have been accessed in September, 2021 and found to be functional

Programming in Modern C++ Partha Pratim Das M04.1


Module Recap

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

Programming in Modern C++ Partha Pratim Das M04.2


Module Objectives

Module M04

• Implementation of Sorting and Searching in C and C++

L
Partha Pratim
Das

E
Objectives &
Outline

T
Sorting
Bubble Sort
Standard Library

P
Searching
Standard Library

N
STL: algorithm

Module Summary

Programming in Modern C++ Partha Pratim Das M04.3


Module Outline

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

3 STL: algorithm - The algorithm Library

4 Module Summary

Programming in Modern C++ Partha Pratim Das M04.4


Sorting 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

Sorting in C and C++

Programming in Modern C++ Partha Pratim Das M04.5


Program 04.01: Bubble Sort

Module M04 C Program C++ Program

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

for(i = 0; i < n; ++i) for(int i = 0; i < n; ++i)


printf("%d ", data[i]); cout << data[i] << " ";
} }

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

Programming in Modern C++ Partha Pratim Das M04.6


Program 04.02: Using sort from standard library
C Program (Desc order) C++ Program (Desc order)
Module M04
#include <stdio.h> #include <iostream>

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

Searching in C and C++

Programming in Modern C++ Partha Pratim Das M04.9


Program 04.04: Binary Search

Module M04 C Program C++ Program

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

Programming in Modern C++ Partha Pratim Das M04.10


STL: algorithm - The algorithm Library

Module M04

L
Partha Pratim
Das

E
Objectives &
Outline

T
Sorting
Bubble Sort
Standard Library

P
Searching
Standard Library

N
STL: algorithm

Module Summary

STL: algorithm - The algorithm Library

Programming in Modern C++ Partha Pratim Das M04.11


The algorithm Library

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

Programming in Modern C++ Partha Pratim Das M04.12


Program 04.05: replace and rotate functions

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

Programming in Modern C++ Partha Pratim Das M04.13


Module Summary

Module M04

• Flexibility of defining customised sort algorithms to be passed as parameter to sort and

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

Programming in Modern C++ Partha Pratim Das M04.14

You might also like