Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 54

Sorting Algorithms

Manish B. Gudadhe ,St. Vincent Pallotti College of Engineering & Technology, Nagpur
Contents
▸ Need of data structures

▸ What is Data Structures

▸ Examples of Data Structures

▸ Types of Data Structures

▸ Applications of Data Structures


Sorting Algorithms

▸ A Sorting Algorithm is used to rearrange a given array or list elements


according to a comparison operator on the elements. The comparison
operator is used to decide the new order of element in the respective data
structure.

▸ In place Sorting

▸ Internal & External Sorting


Bubble sort

▸ Bubble sort is an algorithm that compares the adjacent elements and swaps
their positions if they are not in the intended order.

▸ The order can be ascending or descending.

▸ Starting from the first index, compare the first and the second elements.If the
first element is greater than the second element, they are swapped.

▸ Now, compare the second and the third elements. Swap them if they are not in
order.

▸ The above process goes on until the last element.


12 3 45 84 41 63 25 87
Bubble sort
void bubbleSort(int array[], int size)
{ // run loops two times: one for walking throught the array
// and the other for comparison
for (int step = 0; step < size - 1; ++step)
{ for (int i = 0; i < size - step - 1; ++i)
{if (array[i] > array[i + 1])
{ // swap if greater is at the rear position
int temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
}
}
}
Insertion sort

▸ Insertion sort is a simple sorting algorithm that works similar to the way you
sort playing cards in your hands.

▸ The array is virtually split into a sorted and an unsorted part.

▸ Values from the unsorted part are picked and placed at the correct position
in the sorted part.
Insertion sort
Algorithm
void insertionSort(int array[], int size)
{ for (int step = 1; step < size; step++)
{
int key = array[step];
int j = step - 1;
// Compare key with each element on the left of it until an element smaller than // it is
found.
while (key < array[j] && j >= 0)
{
array[j + 1] = array[j];
--j;
}
array[j + 1] = key;
}}
Examples of commonly used Data Structures are:

▸ Array and Dynamic Array

▸ Stack and Queue

▸ Linked List: Singly linked list, Doubly linked lists, Circular list

▸ Tree: Binary tree, Binary Search Tree, Heap tree etc.

▸ Priority Queue

▸ Hash Table

▸ Graph
Types of Data structures?
Arrays

An array is a collection of items stored at contiguous memory locations.

The idea is to store multiple items of the same type together.


Stack

The stack is a data structure following the LIFO(Last In, First Out) principle.
Queues

The queue is a data structure following the FIFO(First In, First Out) principle.
Linked List

• A linked list is a dynamic data structure.

• The number of nodes in a list is not fixed and can grow and shrink on demand.
Trees

A tree is a hierarchical data structure that stores the information naturally in a


hierarchical style.
Trees

• A graph is a set of objects where some pairs of objects are connected by links.
• The interconnected objects are represented by points termed as vertices, and
the links that connect the vertices are called edges.
Application of data structures

• Stacks are used in “call stack” which is used when functions are called.

• Queues are used for interprocess communications.

• Heaps are used to implementing priority queues which are used for scheduler during

scheduling of the process in an operating system.

• Binary Search Trees are used for implementing maps and sets.

• B-Trees are used in Database designs

• Trees are used in File-System.

• Circular Doubly linked lists are used by the kernel to store lists of processes.
Searching & Sorting
Searching Algorithms

To search an element in a given array, there are two popular algorithms available:

• Linear Search

• Binary Search

• Exponential Search

• Interpolation Search

• Jump Search

• Linear Search

• Ternary Search
Linear Search

• Linear search is a very basic and simple search algorithm.


• In Linear search, we search an element or value in a given array by traversing
the array from the starting, till the desired element or value is found.
• It compares the element to be searched with all the elements present in the
array and when the element is matched successfully, it returns the index of the
element in the array, else it return -1.
• Linear Search is applied on unsorted or unordered lists, when there are fewer
elements in a list.
Linear Search

Following are the steps of implementation that we will be following:


• Traverse the array using a for loop.
• In every iteration, compare the target value with the current value of the array.
• If the values match, return the current index of the array.
• If the values do not match, move on to the next array element.
• If no match is found, return -1.
12 3 45 84 41 63 25 87

int linearSearch(int values[], int target, int n)


{
for(int i = 0; i < n; i++)
{
if (values[i] == target)
{
printf(“Element found at %d”,i+1);
return i;
}
}
return -1;
}
Binary Search

Following are the steps of implementation that we will be following:


1. Start with the middle element:
If the target value is equal to the middle element of the array, then return the index of the middle
element.
If not, then compare the middle element with the target value,
If the target value is greater than the number in the middle index, then pick the
elements to the right of the middle index, and start with Step 1.
If the target value is less than the number in the middle index, then pick the
elements to the left of the middle index, and start with Step 1.
2. When a match is found, return the index of the element matched.
3. If no match is found, then return -1
12

25

31

48

62

75

89

93
12

25

do until the pointers low and high meet each other.


31

mid = (low + high)/2


if (x == arr[mid]) 48
return mid
else 62
if (x > A[mid]) // x is on the right side
low = mid + 1 75
else // x is on the left side
high = mid - 1 89

93
12

binarySearch(arr, x, low, high) 25


if low > high
return False 31
else
mid = (low + high) / 2 48
if x == arr[mid]
return mid 62
else
if x < data[mid] // x is on the right side 75
return binarySearch(arr, x, mid + 1, high)
else // x is on the right side 89
return binarySearch(arr, x, low, mid - 1)

93

Quotations are commonly printed
as a means of inspiration and to
invoke philosophical thoughts from
the reader.
BIG CONCEPT
Bring the attention of your audience over
a key concept using icons or illustrations
You can also split your content

White Black
Is the color of milk and Is the color of ebony
fresh snow, the color and of outer space. It
produced by the has been the symbolic
combination of all the color of elegance,
colors of the visible solemnity and
spectrum. authority.
In two or three columns

Yellow Blue Red


Is the color of gold, Is the colour of the Is the color of
butter and ripe clear sky and the blood, and because
lemons. In the deep sea. It is of this it has
spectrum of visible located between historically been
light, yellow is violet and green on associated with
found between the optical sacrifice, danger
green and orange. spectrum. and courage.
A picture is worth a thousand words

A complex idea can be


conveyed with just a single
still image, namely making it
possible to absorb large
amounts of data quickly.
Want big impact? USE BIG IMAGE
Use charts to explain your ideas

White Gray Black


And tables to compare data

A B C

Yellow 10 20 7

Blue 30 15 10

Orange 5 24 16
Maps

our office
89,526,124
Whoa! That’s a big number,
aren’t you proud?
89,526,124$
That’s a lot of money

185,244 users
And a lot of users

100%
Total success!
Our process is easy

first second last


Let’s review some concepts

Yellow Blue Red


Is the color of gold, butter and Is the colour of the clear sky and Is the color of blood, and
ripe lemons. In the spectrum of the deep sea. It is located because of this it has historically
visible light, yellow is found between violet and green on the been associated with sacrifice,
between green and orange. optical spectrum. danger and courage.

Yellow Blue Red


Is the color of gold, butter and Is the colour of the clear sky and Is the color of blood, and
ripe lemons. In the spectrum of the deep sea. It is located because of this it has historically
visible light, yellow is found between violet and green on the been associated with sacrifice,
between green and orange. optical spectrum. danger and courage.
You can insert graphs from Excel and Google Sheets
Place your screenshot here

ANDROID
PROJECT
Show and explain your
web, app or software
projects using these
gadget templates.
Place your screenshot here
iPHONE
PROJECT
Show and explain your
web, app or software
projects using these
gadget templates.
Place your screenshot here
TABLET
PROJECT
Show and explain your
web, app or software
projects using these
gadget templates.
Place your screenshot here

DESKTOP
PROJECT
Show and explain your
web, app or software
projects using these
gadget templates.
THANKS!
Any questions?
You can find me at @username & user@mail.me
Credits

Special thanks to all the people who made and


released these awesome resources for free:

▸ Presentation template by SlidesCarnival


▸ Photographs by Startupstockphotos
Presentation design

This presentation uses the following typographies and colors:


▸ Titles: Dosis
▸ Body copy: Roboto
You can download the fonts on these pages:
https://www.fontsquirrel.com/fonts/dosis
https://material.google.com/resources/roboto-noto-fonts.html

▸ Orange #ff8700
You don’t need to keep this slide in your presentation. It’s only here to serve you as a design guide if you
need to create new slides or download the fonts to edit the presentation in PowerPoint®
SlidesCarnival icons are editable shapes.

This means that you can:


● Resize them without losing quality.
● Change line color, width and style.

Isn’t that nice? :)

Examples:
Diagrams and infographics
� Now you can use any emoji as an icon!
And of course it resizes without losing quality and you can change the color.


How? Follow Google instructions
https://twitter.com/googledocs/status/730087240156643328

✋👆👉👍👤👦👧👨👩👪💃🏃💑❤😂
😉😋😒😭👶😸🐟🍒🍔💣📌📖🔨🎃🎈
🎨🏈🏰🌏🔌🔑 and many more...
Free templates for all your presentation needs

For PowerPoint and 100% free for personal or Ready to use, professional Blow your audience away
Google Slides commercial use and customizable with attractive visuals

You might also like