SlideShare a Scribd company logo
Prepared By:
Dr. Chandan Kumar
Assistant Professor, Computer Science & Engineering Department
Invertis University, Bareilly
Introduction
 It is a process to arrange or rearrange a set of data or
list in particular order i.e. ascending or descending
order
 If a list contain 10,3,2,5,8,9 elements then
2 3 5 8 9 10
10 9 8 5 3 2
Ascending Order
Descending Order
 For example-
 The telephone directory stores the number of people
sorted by their names so that the names can be quickly
searched
 The dictionary stores word in alphabetical order so that
searching for any word becomes easy.
 The sorting methods can be broken up into two
groups.
 Internal sorting- Here data is sorted at a time in the
main memory
 External sorting- In this condition first data is stored in
auxiliary memory such as hard disk then the sorting
method is performed
Some other important terminology
12 3 56 44 77 56
3 12 44 56 56 77
12 3 56 44 77 56
List before sorting
Stable sorting; does not change the sequence of similar
content in which they appear after sorting the contents
3 12 44 56 56 77
Unstable sorting; If a sorting algorithm changes the sequence of
similar content they appear in after sorting the items
 the sorting algorithm is said to be adaptive because it
takes advantage of elements already 'sorted' in the list
to be sorted. That is, when sorting if any item is
already sorted in the source list, adaptive algorithms
will take that into account and will try not to reorder
it.
 A non-adaptive algorithm is one which does not take
into account the already sorted elements. They try to
force a reordering of every single item to validate their
sordidness.
 Non-increasing order- It happens when there are
duplicate values in the list. The successive elements in
the sequence are to be less than or equal to its former
element.
15 13 11 9 9 7
 Non-decreasing order- It happens when there are
duplicate values in the list. The successive elements in
the sequence are to be greater than or equal to its
former element.
7 9 9 11 13 15
Complexity
 The complexity of the sorting algorithm measures the
runtime of a function where the number of items to be
sorted is 'n’.
 The choice of which sorting method is appropriate for
a problem depends on many dependency
configurations. The most notable of these factors are:
 The amount of time the programmer spends in
programming a specific sorting system
 Menge of the computer time needed to run the program
 The amount of memory used to run the program
Efficiency
 To get the amount of time it takes for a specific system
to sort an array of 'n' elements, the usual approach is to
evaluate the process to find the number of
comparisons (or exchanges) it requires.
 Most of the sorting techniques are sensitive to data,
and so their metrics depend on the order in which they
appear in an input sequence.
 In different cases, different sorting methods are
evaluated, and these cases are called as follows:
 Best case
 Worst case
 Average case
Types
 Bubble sort
 Selection sort
 Insertion sort
 Merge sort
 Quick sort
 Heap sort
 Shell sort
Bubble sort
 Simple and comparison based sorting algorithm
 Each pair of adjacent elements is compared and if they
are not in order then the elements are swapped. For
example, start with the 0th element and then compare
it to the 1st element
 Not suitable for large data sets
Working Method
 Start with first element and compare with second
element
 If it is found to be greater than the second element,
then they are swapped i.e. interchange.
 In the same way all elements are compared (excluding
last element) with their next element and are swapped
if required.
 After completing the first iteration, largest element
gets placed at the last position.
 Similarly in second iteration second largest element
gets placed at the second last position and so on.
Bubble sort example
 Now we consider an unsorted array having 7 elements
 First Pass or iteration
start with the first element i.e. 0th element of the array which is
compared with the adjacent element i.e. second element (1th
element of the array) and compare them to check which one is
greater. Here 15>7, hence swapping done. After that the array looks
like
15 7 11 9 13 18 1
0 1 2 3 4 5 6
15 7 11 9 13 18 1
7 15 11 9 13 18 1
Now compare second and third element i.e. 15 >11, so swapped
7 11 15 9 13 18 1
Now compare third and fourth element i.e. 15 >9, so swapped
7 11 9 15 13 18 1
Now compare fourth and fifth element i.e. 15 >13, so swapped
7 11 9 13 15 18 1
Now compare fifth and sixth element i.e. 15 >18, so no need to swap.
7 11 9 13 15 18 1
Now compare sixth and seventh element i.e. 15 >18, so swapped.
7 11 9 13 15 1 18
After completing first pass or iteration, the largest element of the
array placed at the last position.
Similarly we start second pass and we get second largest element
at the second last position in the array. This process repeated until
we get sorted list.
It takes (n-1) pass to sort a given array where n is the number of
element in the array.
Algorithm
begin BubbleSort(A)
for all elements of A
if A[i] > A[i+1]
swap(A[i], A[i+1])
end if
end for
return A
end BubbleSort
Here, A – Denotes the array of n elements
swap- A function which is used to swap the values of
the given array elements
Implementation in C
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i, j, temp;
int arr[10];
clrscr();
printf("Enter number of elements in a list");
scanf("%d", &n);
printf("nThe entered elements are :n");
for(i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
for(i = 0; i < n - 1; i++) // to keep track of number of cycles
{
for(j = 0; j < n - i - 1; j++) // to compare the elements within the
particular cycle
{
// swap if one element is greater than its adjacent element
if(arr[j] > arr[j + 1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
printf("nThe sorted elements are:n");
for(i = 0; i < n; i++)
{
printf("t%d ", arr[i]);
}
getch();
}
Output:
 Time complexity of bubble sort
 Worst case : O(n2 )
 Average case : O(n2 )
 Best case : O(n)
 Space complexity : O(1)
Note: The best-case occurs when the given array is
already sorted.
Selection sort
 Straightforward process of sorting values
 In-place comparison -based algorithm
 The list is divided into two parts – left end and right
end
 Left end- contain sorted element of the list or an array
 Right end- contain unsorted elements of the list
Note: The sorted part is initially empty, and the
unsorted part is the full list.
Working Method
 The 0th element of the list or an array is compared with
all the other elements of the list
 If the 0th element is found to be greater than the
compared element, the two values get swapped( in
case of ascending order or not swapped in case of
descending order).
 After completing first pass or iteration the smallest
element is placed at 0th position (in ascending order)or
largest element is placed at 0th position (in descending
order).
 This method is repeated until the full array get sorted
 For example- now we consider an array A ={15,7,11,9,13,18,1}
need to be sorted in ascending order.
 Initially, unsorted array represented as
15 7 11 9 13 18 1
First Pass or iteration: Initially taken the first element of the list i.e. 0th
position element of an array and compared with remaining elements of the
list. If we found any element to be smaller than the 0th position element, then
swap both of them.
Here, 15>7  swapped
7>11  no swapping
7>9  no swapping
7>13  no swapping
7>18  no swapping
7>1  swapped
0 1 2 3 4 5 6
7 15 11 9 13 18 1
1 15 11 9 13 18 7
 After completing the first pass, the 0th position has the
smallest element of an array or list
 Now, we start with second element i.e. 1st position
element of an array and compared with rightmost
elements of the list.
 After completing the second step, the second smallest
item in an array or list is in 1st place
 The same method applies to the rest of the products in
the list and we get a sorted list finally
Algorithm
begin selection sort (list,n)
for i = 0 to n - 2
min = i /* set current element as minimum*/
for j = i+1 to n-1
if list[j] < list[min] then
min = j;
swap list[min] and list[i] /* swap the minimum element with the current element*/
end if
end for
end for
end begin
Where, list : array of items and n : size of list
Implementation in C
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],n, i, j, temp, min;
clrscr();
printf("nEnter number of elements:");
scanf("%d", &n);
printf("nEnter %d elements in an array:",n);
for(i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
for (i = 0; i < n - 1; i++)
{
min = i;
for (j = i + 1; j < n; j++)
{
if (a[j] < a[min])
{
min = j;
temp = a[min];
a[min] = a[i];
a[i] = temp;
}
}
}
printf("nThe sorted array using selection sort:n");
for(i = 0; i < n; i++)
{
printf("%d ", a[i]);
}
getch();
}
Output:
 Time complexity of bubble sort
 Worst case : O(n2 )
 Average case : O(n2 )
 Best case : O(n2 )
 Space complexity : O(1)
 It has improved efficiency as compared to bubble sort
Insertion sort
 in-place comparison-based sorting algorithm
 A simple sorting algorithm that works exactly the way
we sort cards in our hands i.e. we take one card and
then look at the remaining card with the intention of
building up in our hand an organized set of cards.
 Here, a sub-list is preserved i.e. the lower part of an
array that is always sorted. An item to be 'inserted' ed
in this sorted sub-list has to find its suitable location
and then insert it there. Hence the name is a an
insertion sort.
Working Method
 The 0th position element of an array is compared with
1st position element.
 If the value of 1st position element is smaller than 0th
position element then swap (in ascending order) or
don’t swap (in descending order)
 Now, the 1st position element is compared with 2nd
position element, if the value of 2nd position element
is smaller than 1st position than swap or inserted
before 1st position otherwise doesn’t swap. If swapped
than again compared with the 0th position element
and do the same.
 This process repeated until we get sorted array
 For example- we assume an array having 6 elements.
Such as a[6]={4,2,5,3,6,7}; then in memory
represented as
4 2 5 3 6 7
0 1 2 3 4 5
First Pass or Iteration: a[0] i.e. the first element of the list or an array,
by itself is trivially sorted.
Second Pass: a[1] is compared with a[0] and inserted either before or
after a[0] so that a[0], a[1] is sorted. Here, a[1]<a[0] so a[1] will be
inserted before a[0].
2 4 5 3 6 7
Third Pass: a[2] is compared with left sorted sub-list i.e. firstly a[1] after
that a[0]. Now, a[2] is inserted into its proper place i.e. between a[0] and
a[1], or before a[0] , or after a[1]. Finally a[0],a[1],a[2] is got sorted.
Here, a[0] and a[1] <a[2] so a[0] , a[1] and a[2] are sorted.
2 4 5 3 6 7
Now repeat this process till N pass where the last element of the list i.e. a[n-1]
is inserted into its proper place and finally a[0],a[1],……a[n-1] is got sorted.
Here, the final sorted list will be
2 3 4 5 6 7
Algorithm
Step 1 − If it is the first element, it is already sorted.
return 1;
Step 2 − Pick next element
Step 3 − Compare with all elements in the sorted sub-list
Step 4 − Shift all the elements in the sorted sub-list that
is greater than the value to be sorted
Step 5 − Insert the value
Step 6 − Repeat until list is sorted
Implementation in C
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10], i, n, j, temp, min, key;
printf("nEnter number of elements:");
scanf("%d", &n);
printf("nPlease entered %d elements in a list:",n);
for(i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
for (i = 1; i < n; i++)
{
key = arr[i];
j = i - 1;
// comparing whether the first element is greater than the second
element
// if yes, then store the largest element to the next position
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j = j - 1;
}
// storing the smallest element in the correct position
arr[j + 1] = key;
}
printf("nThe sorted list using insertion sort is:n");
for(i = 0; i < n; i++)
{
printf("%4d ", arr[i]);
}
getch();
}
Output:
Merge sort
 Previous sorting algorithms have worst running time
when the size of the input list or array is large i.e. take
a long time to run
 Now we are going to discuss one of the sorting
algorithms whose running time is better
 Merge sort algorithm is based on divide and conquer
technique
 Like the strategy of divide and conquer, merge sort
first divide the list into equal halves unless the atomic
values are achieved and then combine them in a sorted
way.
Working Method
 For example- we assume an unsorted array i.e.
A[4]={10,5,23,12};
10 5 23 12
0 1 2 3
Because we know that the merge sort splits the entire array into equal
halves before the atomic values are reached. Here we see that an array
of 4 objects is split into two sizes 2 arrays.
23 1210 5
Note : This method does not alter the sequence of
things found in the original
Again divide these two arrays into halves.
10 12235
Because we attain atomic value that can not be
divided anymore. Now, we are merging them in
the same way they've been broken down.
For each list, we first compare the item and then
assemble them in a sorted manner into another
list. We see that 10 and 5 are not sorted manner,
so swap them. We now compare 23 and 12 and
swap the 23 and 12 orders.
5 10 12 23
In the next iteration of the combining phase, we compare lists of two data values,
and merge them into a list of found data values placing all in a sorted order. Here
we get final sorted list.
5 10 12 23
Algorithm
Step 1 − if it is only one element in the list it is already
sorted, return.
Step 2 − divide the list recursively into two halves until it
can no more be divided.
Step 3 − merge the smaller lists into new list in sorted
order.
Implementation in C
#include <stdio.h>
#include<conio.h>
#define max 10
int a[11] = { 1,4,6,99,77,55,101,44,56,77,88};
int b[10];
void merging(int low, int mid, int high) {
int l1, l2, i;
for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++) {
if(a[l1] <= a[l2])
b[i] = a[l1++];
else
b[i] = a[l2++];
}
while(l1 <= mid)
b[i++] = a[l1++];
while(l2 <= high)
b[i++] = a[l2++];
for(i = low; i <= high; i++)
a[i] = b[i];
}
void sort(int low, int high) {
int mid;
if(low < high) {
mid = (low + high) / 2;
sort(low, mid);
sort(mid+1, high);
merging(low, mid, high);
} else {
return;
void main() {
int i;
clrscr();
printf("Array elements before sortingn");
for(i = 0; i <= max; i++)
printf("%d ", a[i]);
sort(0, max);
printf("nArray elements after merge sortn");
for(i = 0; i <= max; i++)
printf("%d ", a[i]);
getch();
}
Output:
Quick sort
 Highly efficient and quickest sorting algorithm
 Also follow divide and conquer technique to sort the
given list
 Here list is divided into two parts using pivot element
and sort sub-list recursively
 There are different versions to choose pivot element
such as
 Often pick the pivoting factor first.
 Select the last factor as pivot at all times.
 Choose a pivot element by chance.
 The median pick as pivot.
 It continues to pick a pivot element and break down
the array into the array of single elements before
adding them together to create a single sorted array.
 This algorithm is pretty good for large data sets
 Works by partitioning the sequence to be sorted and a
recursive sorting of each partition in turn. Therefore
partition exchange sort is also called.
Working Method
 From the list select an item, called a pivot.
 Reorder the list so that all elements with values less
than the pivot come before the pivot and all elements
with values greater than the pivot comes after it (equal
values can go either way). The pivot is in its final
location, after this partitioning. This function is called
the partition.
 Recursively apply the above steps to the sub-list of
elements with smaller values, and the sub-list of
elements with higher values separately.
 For example- we assume an array of 8 elements, such
as
10 5 3 2 7 9 11 6
0 1 2 3 4 5 6 7
Now, we choose last element as pivot. So 7th position element i.e. 6 is
the value of pivot element.
Partition the list using pivot value i.e. smaller elements go to the left side
and greater element goes to the right side of the pivot element.
5 3 2 6 7 9 11 10
Pivot
 Again select last element as a pivot from both the list
5 3 2 6 7 9 11 10
pivot pivot
Portion the both list using pivot. Recursively follow these steps to sort
the given array, such as
2 3 5 6 7 9 10 11
Algorithm
Step 1 − Make the right-most index value pivot
Step 2 − partition the array using pivot value
Step 3 − quicksort left partition recursively
Step 4 − quicksort right partition recursively
Implementation using C
#include <stdio.h>
#include<conio.h>
#define MAX 7
int intArray[MAX]={10,20,15,19,7,11,35};
void printline(int count)
{
int i;
for(i = 0;i < count-1;i++)
{
printf("=");
}
printf("=n");
}
void display() {
int i;
printf("[");
// navigate through all items
for(i = 0;i < MAX;i++) {
printf("%d ",intArray[i]);
}
printf("]n");
}
void swap(int num1, int num2)
{
int temp = intArray[num1];
intArray[num1] = intArray[num2];
intArray[num2] = temp;
}
int partition(int left, int right, int pivot) {
int leftPointer = left -1;
int rightPointer = right;
while(1)
{
while(intArray[++leftPointer] < pivot)
{
//do nothing
}
while(rightPointer > 0 && intArray[--rightPointer] > pivot)
{
//do nothing
}
if(leftPointer >= rightPointer)
{
break;
else {
printf(" item swapped :%d,%dn",
intArray[leftPointer],intArray[rightPointer]);
swap(leftPointer,rightPointer);
}
}
printf("npivot swapped :%d,%dn",
intArray[leftPointer],intArray[right]);
swap(leftPointer,right);
printf("nUpdated list:");
display();
return leftPointer;
}
void quickSort(int left, int right) {
if(right-left <= 0) {
return;
}
else {
int pivot = intArray[right];
int partitionPoint = partition(left, right, pivot);
quickSort(left,partitionPoint-1);
quickSort(partitionPoint+1,right);
}
}
void main()
{
clrscr();
printf("nUnsorted list or array ");
display();
printline(50);
quickSort(0,MAX-1);
printf("Sorted list using quick sort : ");
display();
printline(50);
getch();
}
Output:
Heap sort
 Comparison based sorting algorithm
 Heap is a special type of binary tree where elements
are stored hierarchically
 It is similar to selection sort where we first find the
maximum element and place the maximum element at
the end
 it must either be ordered as a min heap (where parent
node is less than or equal to the value of its children
node) or a max-heap (where parent node is greater
than or equal to the value of its children node)
Working Method
For Ascending order
 Build a max heap from the input data.
 At this point, the largest item is stored at the root of
the heap. Replace it with the last item of the heap
followed by reducing the size of heap by 1. Finally,
heapify the root of tree.
 Repeat above steps while size of heap is greater than 1.
For example- We assume an array a[]={4,3,7,1,8,5}
 First, build max-heap from the given tree .
 Now swap the root node with the Heap node's last
element. This implies that the largest element has
moved to the appropriate location. So, delete from the
tree . The tree looks as shown below, after removal.
 Repeat step 3 until no elements are left in the heap
Sorting algorithms
Sorting algorithms
Algorithm
 Step 1 - Construct a Binary Tree with given list of
Elements.
 Step 2 - Transform the Binary Tree into Max Heap.
 Step 3 - Delete the root element from Min Heap
using Heapify method.
 Step 4 - Put the deleted element into the Sorted list.
 Step 5 - Repeat the same until Max Heap becomes
empty.
 Step 6 - Display the sorted list.
Implementation using C
#include<stdio.h>
int temp;
void swap_largest(int arr[], int n, int i)
{
int largest = i; // Initialize largest as root
int left = 2*i + 1;
int right = 2*i + 2;
// If left child is larger than root
if (left < n && arr[left] > arr[largest])
largest = left;
// If right child is larger than largest
if (right < n && arr[right] > arr[largest])
largest = right;
// If largest is not root
if (largest != i)
{
temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;
// Recursively call the swap_largest
swap_largest(arr, n, largest);
}
}
void heap(int arr[], int n)
{
int i;
// Build heap from an unsorted array (rearrange array)
for (i = n / 2 - 1; i >= 0; i--)
swap_largest(arr, n, i);
// One by one extract an element from heap
for (i = n - 1; i >= 0; i--)
{
// Move current root to end
temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
// call swap_largest on the reduced heap
swap_largest(arr, i, 0);
}
}
void print(int arr[], int n)
{
int i;
printf("Sorted list using heap sortn");
for(i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
}
void main()
{
int arr[10],n, i;
clrscr();
printf("Enter number of elements");
scanf("%d", &n);
for(i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
heap(arr, n);
print(arr, n);
getch();
}
Output:
Shell sort
 Highly efficient sorting algorithm
 Similar to insertion sort algorithm
 Shell sort starts by sorting pairs of elements far apart
from each other, then progressively reducing the gap
between elements to be compared
 Starting with far apart elements, it can move some out-
of-place elements into the position faster than a simple
nearest-neighbor exchange
Algorithm
 Step 1 − Initialize the value of h
 Step 2 − Divide the list into smaller sub-list of equal
interval h
 Step 3 − Sort these sub-lists using insertion sort
 Step 4 − Repeat until complete list is sorted
For example- we consider an array arr []={17,3,9,1,8}
Implementation using C
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[10], n, i, j, temp, gap;
clrscr();
printf("Enter number of elements");
scanf("%d", &n);
for(i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
for (gap = n/2; gap > 0; gap = gap / 2)
{
for (i = gap; i < n; i = i + 1)
{
int temp = arr[i];
for (j = i; j >= gap && arr[j - gap] > temp; j = j - gap)
arr[j] = arr[j - gap];
arr[j] = temp;
}
}
printf("Sorted array using shell sortn");
for(i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
getch();
Output:
Sorting algorithms complexities
Algorithm
Time Complexity
Best Case Average Case Worst Case
Bubble sort O(n) O(n2 ) O(n2 )
Selection sort O(n2 ) O(n2 ) O(n2 )
Insertion sort O(n) O(n2 ) O(n2 )
Heap sort O(nlogn) O(nlogn) O(nlogn)
Quick sort O(nlogn) O(nlogn) O(n2 )
Merge sort O(nlogn) O(nlogn) O(nlogn)
Shell sort O(n) O((nlogn))^2) O((nlogn))^2)
Application
 Uniqueness testing
 Deleting duplicates
 Prioritizing events
 Frequency counting
 Reconstructing the original order
 Set intersection/union
 Finding a target pair x, y such that x+y = z
References
 http://www.btechsmartclass.com/data_structures
 https://www.faceprep.in/c/sorting-algorithms
 https://www.tutorialspoint.com/data_structures_algor
ithms/sorting_algorithms.htm
Sorting algorithms
Ad

More Related Content

What's hot (20)

Insertion sort
Insertion sort Insertion sort
Insertion sort
Monalisa Patel
 
Stack project
Stack projectStack project
Stack project
Amr Aboelgood
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
Trupti Agrawal
 
Stack a Data Structure
Stack a Data StructureStack a Data Structure
Stack a Data Structure
ForwardBlog Enewzletter
 
Queue - Data Structure - Notes
Queue - Data Structure - NotesQueue - Data Structure - Notes
Queue - Data Structure - Notes
Omprakash Chauhan
 
Rahat &amp; juhith
Rahat &amp; juhithRahat &amp; juhith
Rahat &amp; juhith
Rj Juhith
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 
Presentation on the topic selection sort
Presentation on the topic selection sortPresentation on the topic selection sort
Presentation on the topic selection sort
District Administration
 
sorting and its types
sorting and its typessorting and its types
sorting and its types
SIVASHANKARIRAJAN
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
MYER301
 
Quick sort
Quick sortQuick sort
Quick sort
Dhruv Sabalpara
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
somendra kumar
 
Selection sort and insertion sort
Selection sort and insertion sortSelection sort and insertion sort
Selection sort and insertion sort
May Ann Mendoza
 
Demonstrate interpolation search
Demonstrate interpolation searchDemonstrate interpolation search
Demonstrate interpolation search
manojmanoj218596
 
Searching and sorting
Searching  and sortingSearching  and sorting
Searching and sorting
PoojithaBollikonda
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
almaqboli
 
Searching, Sorting and Hashing Techniques
Searching, Sorting and Hashing TechniquesSearching, Sorting and Hashing Techniques
Searching, Sorting and Hashing Techniques
Selvaraj Seerangan
 
Selection sort
Selection sortSelection sort
Selection sort
amna izzat
 
Sparse matrix
Sparse matrixSparse matrix
Sparse matrix
dincyjain
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
Tareq Hasan
 
Queue - Data Structure - Notes
Queue - Data Structure - NotesQueue - Data Structure - Notes
Queue - Data Structure - Notes
Omprakash Chauhan
 
Rahat &amp; juhith
Rahat &amp; juhithRahat &amp; juhith
Rahat &amp; juhith
Rj Juhith
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 
Presentation on the topic selection sort
Presentation on the topic selection sortPresentation on the topic selection sort
Presentation on the topic selection sort
District Administration
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
MYER301
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
somendra kumar
 
Selection sort and insertion sort
Selection sort and insertion sortSelection sort and insertion sort
Selection sort and insertion sort
May Ann Mendoza
 
Demonstrate interpolation search
Demonstrate interpolation searchDemonstrate interpolation search
Demonstrate interpolation search
manojmanoj218596
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
almaqboli
 
Searching, Sorting and Hashing Techniques
Searching, Sorting and Hashing TechniquesSearching, Sorting and Hashing Techniques
Searching, Sorting and Hashing Techniques
Selvaraj Seerangan
 
Selection sort
Selection sortSelection sort
Selection sort
amna izzat
 
Sparse matrix
Sparse matrixSparse matrix
Sparse matrix
dincyjain
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
Tareq Hasan
 

Similar to Sorting algorithms (20)

Sorting
SortingSorting
Sorting
BHARATH KUMAR
 
Ijcse13 05-01-048
Ijcse13 05-01-048Ijcse13 05-01-048
Ijcse13 05-01-048
vital vital
 
Ijcse13 05-01-048
Ijcse13 05-01-048Ijcse13 05-01-048
Ijcse13 05-01-048
vital vital
 
my docoment
my docomentmy docoment
my docoment
NeeshanYonzan
 
Sorting
SortingSorting
Sorting
Kariman Karm Gabaa
 
Sorting algorithums > Data Structures & Algorithums
Sorting algorithums  > Data Structures & AlgorithumsSorting algorithums  > Data Structures & Algorithums
Sorting algorithums > Data Structures & Algorithums
Ain-ul-Moiz Khawaja
 
Computer sciencebubble sorting algorithm
Computer sciencebubble sorting algorithmComputer sciencebubble sorting algorithm
Computer sciencebubble sorting algorithm
hebahosny20060467
 
Data Structures_ Sorting & Searching
Data Structures_ Sorting & SearchingData Structures_ Sorting & Searching
Data Structures_ Sorting & Searching
ThenmozhiK5
 
DS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptx
prakashvs7
 
Chapter 8 Sorting in the context of DSA.pptx
Chapter 8 Sorting in the context of DSA.pptxChapter 8 Sorting in the context of DSA.pptx
Chapter 8 Sorting in the context of DSA.pptx
Dibyesh1
 
Sorting
SortingSorting
Sorting
A. S. M. Shafi
 
Selection sort
Selection sortSelection sort
Selection sort
asra khan
 
Unit vii sorting
Unit   vii sorting Unit   vii sorting
Unit vii sorting
Tribhuvan University
 
Unit v data structure-converted
Unit  v data structure-convertedUnit  v data structure-converted
Unit v data structure-converted
Shri Shankaracharya College, Bhilai,Junwani
 
Chapter 3 - Elementary Searching and Sorting Algorithms.ppt
Chapter 3 - Elementary Searching and Sorting Algorithms.pptChapter 3 - Elementary Searching and Sorting Algorithms.ppt
Chapter 3 - Elementary Searching and Sorting Algorithms.ppt
AbdisaAwel
 
Selection Sort & Insertion Sorts Algorithms
Selection Sort & Insertion Sorts AlgorithmsSelection Sort & Insertion Sorts Algorithms
Selection Sort & Insertion Sorts Algorithms
Ahmad177077
 
Selection Sort & Insertion Sorts Algorithms
Selection Sort & Insertion Sorts AlgorithmsSelection Sort & Insertion Sorts Algorithms
Selection Sort & Insertion Sorts Algorithms
Ahmad177077
 
Chapter3.pptx
Chapter3.pptxChapter3.pptx
Chapter3.pptx
ASMAALWADEE2
 
Sorting-Algorithms-A-Comprehensive-Guide.pptx
Sorting-Algorithms-A-Comprehensive-Guide.pptxSorting-Algorithms-A-Comprehensive-Guide.pptx
Sorting-Algorithms-A-Comprehensive-Guide.pptx
ReemEmad26
 
366 it elective 4 (analysis of algoritm)
366 it elective 4 (analysis of algoritm)366 it elective 4 (analysis of algoritm)
366 it elective 4 (analysis of algoritm)
Neil Soliven
 
Ijcse13 05-01-048
Ijcse13 05-01-048Ijcse13 05-01-048
Ijcse13 05-01-048
vital vital
 
Ijcse13 05-01-048
Ijcse13 05-01-048Ijcse13 05-01-048
Ijcse13 05-01-048
vital vital
 
Sorting algorithums > Data Structures & Algorithums
Sorting algorithums  > Data Structures & AlgorithumsSorting algorithums  > Data Structures & Algorithums
Sorting algorithums > Data Structures & Algorithums
Ain-ul-Moiz Khawaja
 
Computer sciencebubble sorting algorithm
Computer sciencebubble sorting algorithmComputer sciencebubble sorting algorithm
Computer sciencebubble sorting algorithm
hebahosny20060467
 
Data Structures_ Sorting & Searching
Data Structures_ Sorting & SearchingData Structures_ Sorting & Searching
Data Structures_ Sorting & Searching
ThenmozhiK5
 
DS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptx
prakashvs7
 
Chapter 8 Sorting in the context of DSA.pptx
Chapter 8 Sorting in the context of DSA.pptxChapter 8 Sorting in the context of DSA.pptx
Chapter 8 Sorting in the context of DSA.pptx
Dibyesh1
 
Selection sort
Selection sortSelection sort
Selection sort
asra khan
 
Chapter 3 - Elementary Searching and Sorting Algorithms.ppt
Chapter 3 - Elementary Searching and Sorting Algorithms.pptChapter 3 - Elementary Searching and Sorting Algorithms.ppt
Chapter 3 - Elementary Searching and Sorting Algorithms.ppt
AbdisaAwel
 
Selection Sort & Insertion Sorts Algorithms
Selection Sort & Insertion Sorts AlgorithmsSelection Sort & Insertion Sorts Algorithms
Selection Sort & Insertion Sorts Algorithms
Ahmad177077
 
Selection Sort & Insertion Sorts Algorithms
Selection Sort & Insertion Sorts AlgorithmsSelection Sort & Insertion Sorts Algorithms
Selection Sort & Insertion Sorts Algorithms
Ahmad177077
 
Sorting-Algorithms-A-Comprehensive-Guide.pptx
Sorting-Algorithms-A-Comprehensive-Guide.pptxSorting-Algorithms-A-Comprehensive-Guide.pptx
Sorting-Algorithms-A-Comprehensive-Guide.pptx
ReemEmad26
 
366 it elective 4 (analysis of algoritm)
366 it elective 4 (analysis of algoritm)366 it elective 4 (analysis of algoritm)
366 it elective 4 (analysis of algoritm)
Neil Soliven
 
Ad

More from CHANDAN KUMAR (13)

Chart and graphs in R programming language
Chart and graphs in R programming language Chart and graphs in R programming language
Chart and graphs in R programming language
CHANDAN KUMAR
 
Raid technology
Raid technologyRaid technology
Raid technology
CHANDAN KUMAR
 
Pointers in c
Pointers in cPointers in c
Pointers in c
CHANDAN KUMAR
 
Searching in c language
Searching in c languageSearching in c language
Searching in c language
CHANDAN KUMAR
 
Greedy algorithm
Greedy algorithmGreedy algorithm
Greedy algorithm
CHANDAN KUMAR
 
Divide and conquer algorithm
Divide and conquer algorithmDivide and conquer algorithm
Divide and conquer algorithm
CHANDAN KUMAR
 
Arrays in c
Arrays in cArrays in c
Arrays in c
CHANDAN KUMAR
 
Loops in c programming
Loops in c programmingLoops in c programming
Loops in c programming
CHANDAN KUMAR
 
Linked List
Linked ListLinked List
Linked List
CHANDAN KUMAR
 
Stack and queue
Stack and queueStack and queue
Stack and queue
CHANDAN KUMAR
 
Technical questions for interview c programming
Technical questions for interview  c programmingTechnical questions for interview  c programming
Technical questions for interview c programming
CHANDAN KUMAR
 
Decision making using if statement
Decision making using if statementDecision making using if statement
Decision making using if statement
CHANDAN KUMAR
 
"A short and knowledgeable concept about Algorithm "
"A short and knowledgeable concept about Algorithm ""A short and knowledgeable concept about Algorithm "
"A short and knowledgeable concept about Algorithm "
CHANDAN KUMAR
 
Chart and graphs in R programming language
Chart and graphs in R programming language Chart and graphs in R programming language
Chart and graphs in R programming language
CHANDAN KUMAR
 
Searching in c language
Searching in c languageSearching in c language
Searching in c language
CHANDAN KUMAR
 
Divide and conquer algorithm
Divide and conquer algorithmDivide and conquer algorithm
Divide and conquer algorithm
CHANDAN KUMAR
 
Loops in c programming
Loops in c programmingLoops in c programming
Loops in c programming
CHANDAN KUMAR
 
Technical questions for interview c programming
Technical questions for interview  c programmingTechnical questions for interview  c programming
Technical questions for interview c programming
CHANDAN KUMAR
 
Decision making using if statement
Decision making using if statementDecision making using if statement
Decision making using if statement
CHANDAN KUMAR
 
"A short and knowledgeable concept about Algorithm "
"A short and knowledgeable concept about Algorithm ""A short and knowledgeable concept about Algorithm "
"A short and knowledgeable concept about Algorithm "
CHANDAN KUMAR
 
Ad

Recently uploaded (20)

One Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learningOne Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learning
momer9505
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
Exercise Physiology MCQS By DR. NASIR MUSTAFA
Exercise Physiology MCQS By DR. NASIR MUSTAFAExercise Physiology MCQS By DR. NASIR MUSTAFA
Exercise Physiology MCQS By DR. NASIR MUSTAFA
Dr. Nasir Mustafa
 
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdfIntroduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
james5028
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
03#UNTAGGED. Generosity in architecture.
03#UNTAGGED. Generosity in architecture.03#UNTAGGED. Generosity in architecture.
03#UNTAGGED. Generosity in architecture.
MCH
 
dynastic art of the Pallava dynasty south India
dynastic art of the Pallava dynasty south Indiadynastic art of the Pallava dynasty south India
dynastic art of the Pallava dynasty south India
PrachiSontakke5
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
Political History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptxPolitical History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
Real GitHub Copilot Exam Dumps for Success
Real GitHub Copilot Exam Dumps for SuccessReal GitHub Copilot Exam Dumps for Success
Real GitHub Copilot Exam Dumps for Success
Mark Soia
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
SPRING FESTIVITIES - UK AND USA -
SPRING FESTIVITIES - UK AND USA            -SPRING FESTIVITIES - UK AND USA            -
SPRING FESTIVITIES - UK AND USA -
Colégio Santa Teresinha
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
Link your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRMLink your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRM
Celine George
 
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdfBiophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
PKLI-Institute of Nursing and Allied Health Sciences Lahore , Pakistan.
 
One Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learningOne Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learning
momer9505
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
Exercise Physiology MCQS By DR. NASIR MUSTAFA
Exercise Physiology MCQS By DR. NASIR MUSTAFAExercise Physiology MCQS By DR. NASIR MUSTAFA
Exercise Physiology MCQS By DR. NASIR MUSTAFA
Dr. Nasir Mustafa
 
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdfIntroduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
james5028
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
03#UNTAGGED. Generosity in architecture.
03#UNTAGGED. Generosity in architecture.03#UNTAGGED. Generosity in architecture.
03#UNTAGGED. Generosity in architecture.
MCH
 
dynastic art of the Pallava dynasty south India
dynastic art of the Pallava dynasty south Indiadynastic art of the Pallava dynasty south India
dynastic art of the Pallava dynasty south India
PrachiSontakke5
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
Real GitHub Copilot Exam Dumps for Success
Real GitHub Copilot Exam Dumps for SuccessReal GitHub Copilot Exam Dumps for Success
Real GitHub Copilot Exam Dumps for Success
Mark Soia
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
Link your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRMLink your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRM
Celine George
 

Sorting algorithms

  • 1. Prepared By: Dr. Chandan Kumar Assistant Professor, Computer Science & Engineering Department Invertis University, Bareilly
  • 2. Introduction  It is a process to arrange or rearrange a set of data or list in particular order i.e. ascending or descending order  If a list contain 10,3,2,5,8,9 elements then 2 3 5 8 9 10 10 9 8 5 3 2 Ascending Order Descending Order
  • 3.  For example-  The telephone directory stores the number of people sorted by their names so that the names can be quickly searched  The dictionary stores word in alphabetical order so that searching for any word becomes easy.
  • 4.  The sorting methods can be broken up into two groups.  Internal sorting- Here data is sorted at a time in the main memory  External sorting- In this condition first data is stored in auxiliary memory such as hard disk then the sorting method is performed
  • 5. Some other important terminology 12 3 56 44 77 56 3 12 44 56 56 77 12 3 56 44 77 56 List before sorting Stable sorting; does not change the sequence of similar content in which they appear after sorting the contents 3 12 44 56 56 77 Unstable sorting; If a sorting algorithm changes the sequence of similar content they appear in after sorting the items
  • 6.  the sorting algorithm is said to be adaptive because it takes advantage of elements already 'sorted' in the list to be sorted. That is, when sorting if any item is already sorted in the source list, adaptive algorithms will take that into account and will try not to reorder it.  A non-adaptive algorithm is one which does not take into account the already sorted elements. They try to force a reordering of every single item to validate their sordidness.
  • 7.  Non-increasing order- It happens when there are duplicate values in the list. The successive elements in the sequence are to be less than or equal to its former element. 15 13 11 9 9 7
  • 8.  Non-decreasing order- It happens when there are duplicate values in the list. The successive elements in the sequence are to be greater than or equal to its former element. 7 9 9 11 13 15
  • 9. Complexity  The complexity of the sorting algorithm measures the runtime of a function where the number of items to be sorted is 'n’.  The choice of which sorting method is appropriate for a problem depends on many dependency configurations. The most notable of these factors are:  The amount of time the programmer spends in programming a specific sorting system  Menge of the computer time needed to run the program  The amount of memory used to run the program
  • 10. Efficiency  To get the amount of time it takes for a specific system to sort an array of 'n' elements, the usual approach is to evaluate the process to find the number of comparisons (or exchanges) it requires.  Most of the sorting techniques are sensitive to data, and so their metrics depend on the order in which they appear in an input sequence.  In different cases, different sorting methods are evaluated, and these cases are called as follows:  Best case  Worst case  Average case
  • 11. Types  Bubble sort  Selection sort  Insertion sort  Merge sort  Quick sort  Heap sort  Shell sort
  • 12. Bubble sort  Simple and comparison based sorting algorithm  Each pair of adjacent elements is compared and if they are not in order then the elements are swapped. For example, start with the 0th element and then compare it to the 1st element  Not suitable for large data sets
  • 13. Working Method  Start with first element and compare with second element  If it is found to be greater than the second element, then they are swapped i.e. interchange.  In the same way all elements are compared (excluding last element) with their next element and are swapped if required.  After completing the first iteration, largest element gets placed at the last position.  Similarly in second iteration second largest element gets placed at the second last position and so on.
  • 14. Bubble sort example  Now we consider an unsorted array having 7 elements  First Pass or iteration start with the first element i.e. 0th element of the array which is compared with the adjacent element i.e. second element (1th element of the array) and compare them to check which one is greater. Here 15>7, hence swapping done. After that the array looks like 15 7 11 9 13 18 1 0 1 2 3 4 5 6 15 7 11 9 13 18 1
  • 15. 7 15 11 9 13 18 1 Now compare second and third element i.e. 15 >11, so swapped 7 11 15 9 13 18 1 Now compare third and fourth element i.e. 15 >9, so swapped 7 11 9 15 13 18 1 Now compare fourth and fifth element i.e. 15 >13, so swapped 7 11 9 13 15 18 1
  • 16. Now compare fifth and sixth element i.e. 15 >18, so no need to swap. 7 11 9 13 15 18 1 Now compare sixth and seventh element i.e. 15 >18, so swapped. 7 11 9 13 15 1 18 After completing first pass or iteration, the largest element of the array placed at the last position. Similarly we start second pass and we get second largest element at the second last position in the array. This process repeated until we get sorted list. It takes (n-1) pass to sort a given array where n is the number of element in the array.
  • 17. Algorithm begin BubbleSort(A) for all elements of A if A[i] > A[i+1] swap(A[i], A[i+1]) end if end for return A end BubbleSort Here, A – Denotes the array of n elements swap- A function which is used to swap the values of the given array elements
  • 18. Implementation in C #include<stdio.h> #include<conio.h> void main() { int n, i, j, temp; int arr[10]; clrscr(); printf("Enter number of elements in a list"); scanf("%d", &n); printf("nThe entered elements are :n"); for(i = 0; i < n; i++) { scanf("%d", &arr[i]);
  • 19. for(i = 0; i < n - 1; i++) // to keep track of number of cycles { for(j = 0; j < n - i - 1; j++) // to compare the elements within the particular cycle { // swap if one element is greater than its adjacent element if(arr[j] > arr[j + 1]) { temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } }
  • 20. printf("nThe sorted elements are:n"); for(i = 0; i < n; i++) { printf("t%d ", arr[i]); } getch(); } Output:
  • 21.  Time complexity of bubble sort  Worst case : O(n2 )  Average case : O(n2 )  Best case : O(n)  Space complexity : O(1) Note: The best-case occurs when the given array is already sorted.
  • 22. Selection sort  Straightforward process of sorting values  In-place comparison -based algorithm  The list is divided into two parts – left end and right end  Left end- contain sorted element of the list or an array  Right end- contain unsorted elements of the list Note: The sorted part is initially empty, and the unsorted part is the full list.
  • 23. Working Method  The 0th element of the list or an array is compared with all the other elements of the list  If the 0th element is found to be greater than the compared element, the two values get swapped( in case of ascending order or not swapped in case of descending order).  After completing first pass or iteration the smallest element is placed at 0th position (in ascending order)or largest element is placed at 0th position (in descending order).  This method is repeated until the full array get sorted
  • 24.  For example- now we consider an array A ={15,7,11,9,13,18,1} need to be sorted in ascending order.  Initially, unsorted array represented as 15 7 11 9 13 18 1 First Pass or iteration: Initially taken the first element of the list i.e. 0th position element of an array and compared with remaining elements of the list. If we found any element to be smaller than the 0th position element, then swap both of them. Here, 15>7  swapped 7>11  no swapping 7>9  no swapping 7>13  no swapping 7>18  no swapping 7>1  swapped 0 1 2 3 4 5 6 7 15 11 9 13 18 1 1 15 11 9 13 18 7
  • 25.  After completing the first pass, the 0th position has the smallest element of an array or list  Now, we start with second element i.e. 1st position element of an array and compared with rightmost elements of the list.  After completing the second step, the second smallest item in an array or list is in 1st place  The same method applies to the rest of the products in the list and we get a sorted list finally
  • 26. Algorithm begin selection sort (list,n) for i = 0 to n - 2 min = i /* set current element as minimum*/ for j = i+1 to n-1 if list[j] < list[min] then min = j; swap list[min] and list[i] /* swap the minimum element with the current element*/ end if end for end for end begin Where, list : array of items and n : size of list
  • 27. Implementation in C #include<stdio.h> #include<conio.h> void main() { int a[10],n, i, j, temp, min; clrscr(); printf("nEnter number of elements:"); scanf("%d", &n); printf("nEnter %d elements in an array:",n); for(i = 0; i < n; i++) { scanf("%d", &a[i]); }
  • 28. for (i = 0; i < n - 1; i++) { min = i; for (j = i + 1; j < n; j++) { if (a[j] < a[min]) { min = j; temp = a[min]; a[min] = a[i]; a[i] = temp; } } }
  • 29. printf("nThe sorted array using selection sort:n"); for(i = 0; i < n; i++) { printf("%d ", a[i]); } getch(); } Output:
  • 30.  Time complexity of bubble sort  Worst case : O(n2 )  Average case : O(n2 )  Best case : O(n2 )  Space complexity : O(1)  It has improved efficiency as compared to bubble sort
  • 31. Insertion sort  in-place comparison-based sorting algorithm  A simple sorting algorithm that works exactly the way we sort cards in our hands i.e. we take one card and then look at the remaining card with the intention of building up in our hand an organized set of cards.  Here, a sub-list is preserved i.e. the lower part of an array that is always sorted. An item to be 'inserted' ed in this sorted sub-list has to find its suitable location and then insert it there. Hence the name is a an insertion sort.
  • 32. Working Method  The 0th position element of an array is compared with 1st position element.  If the value of 1st position element is smaller than 0th position element then swap (in ascending order) or don’t swap (in descending order)  Now, the 1st position element is compared with 2nd position element, if the value of 2nd position element is smaller than 1st position than swap or inserted before 1st position otherwise doesn’t swap. If swapped than again compared with the 0th position element and do the same.  This process repeated until we get sorted array
  • 33.  For example- we assume an array having 6 elements. Such as a[6]={4,2,5,3,6,7}; then in memory represented as 4 2 5 3 6 7 0 1 2 3 4 5 First Pass or Iteration: a[0] i.e. the first element of the list or an array, by itself is trivially sorted. Second Pass: a[1] is compared with a[0] and inserted either before or after a[0] so that a[0], a[1] is sorted. Here, a[1]<a[0] so a[1] will be inserted before a[0]. 2 4 5 3 6 7 Third Pass: a[2] is compared with left sorted sub-list i.e. firstly a[1] after that a[0]. Now, a[2] is inserted into its proper place i.e. between a[0] and a[1], or before a[0] , or after a[1]. Finally a[0],a[1],a[2] is got sorted.
  • 34. Here, a[0] and a[1] <a[2] so a[0] , a[1] and a[2] are sorted. 2 4 5 3 6 7 Now repeat this process till N pass where the last element of the list i.e. a[n-1] is inserted into its proper place and finally a[0],a[1],……a[n-1] is got sorted. Here, the final sorted list will be 2 3 4 5 6 7
  • 35. Algorithm Step 1 − If it is the first element, it is already sorted. return 1; Step 2 − Pick next element Step 3 − Compare with all elements in the sorted sub-list Step 4 − Shift all the elements in the sorted sub-list that is greater than the value to be sorted Step 5 − Insert the value Step 6 − Repeat until list is sorted
  • 36. Implementation in C #include<stdio.h> #include<conio.h> void main() { int a[10], i, n, j, temp, min, key; printf("nEnter number of elements:"); scanf("%d", &n); printf("nPlease entered %d elements in a list:",n); for(i = 0; i < n; i++) { scanf("%d", &arr[i]); }
  • 37. for (i = 1; i < n; i++) { key = arr[i]; j = i - 1; // comparing whether the first element is greater than the second element // if yes, then store the largest element to the next position while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j = j - 1; } // storing the smallest element in the correct position arr[j + 1] = key; }
  • 38. printf("nThe sorted list using insertion sort is:n"); for(i = 0; i < n; i++) { printf("%4d ", arr[i]); } getch(); } Output:
  • 39. Merge sort  Previous sorting algorithms have worst running time when the size of the input list or array is large i.e. take a long time to run  Now we are going to discuss one of the sorting algorithms whose running time is better  Merge sort algorithm is based on divide and conquer technique  Like the strategy of divide and conquer, merge sort first divide the list into equal halves unless the atomic values are achieved and then combine them in a sorted way.
  • 40. Working Method  For example- we assume an unsorted array i.e. A[4]={10,5,23,12}; 10 5 23 12 0 1 2 3 Because we know that the merge sort splits the entire array into equal halves before the atomic values are reached. Here we see that an array of 4 objects is split into two sizes 2 arrays. 23 1210 5
  • 41. Note : This method does not alter the sequence of things found in the original Again divide these two arrays into halves. 10 12235 Because we attain atomic value that can not be divided anymore. Now, we are merging them in the same way they've been broken down. For each list, we first compare the item and then assemble them in a sorted manner into another list. We see that 10 and 5 are not sorted manner, so swap them. We now compare 23 and 12 and swap the 23 and 12 orders.
  • 42. 5 10 12 23 In the next iteration of the combining phase, we compare lists of two data values, and merge them into a list of found data values placing all in a sorted order. Here we get final sorted list. 5 10 12 23
  • 43. Algorithm Step 1 − if it is only one element in the list it is already sorted, return. Step 2 − divide the list recursively into two halves until it can no more be divided. Step 3 − merge the smaller lists into new list in sorted order.
  • 44. Implementation in C #include <stdio.h> #include<conio.h> #define max 10 int a[11] = { 1,4,6,99,77,55,101,44,56,77,88}; int b[10]; void merging(int low, int mid, int high) { int l1, l2, i; for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++) { if(a[l1] <= a[l2]) b[i] = a[l1++]; else b[i] = a[l2++]; }
  • 45. while(l1 <= mid) b[i++] = a[l1++]; while(l2 <= high) b[i++] = a[l2++]; for(i = low; i <= high; i++) a[i] = b[i]; } void sort(int low, int high) { int mid; if(low < high) { mid = (low + high) / 2; sort(low, mid); sort(mid+1, high); merging(low, mid, high); } else { return;
  • 46. void main() { int i; clrscr(); printf("Array elements before sortingn"); for(i = 0; i <= max; i++) printf("%d ", a[i]); sort(0, max); printf("nArray elements after merge sortn"); for(i = 0; i <= max; i++) printf("%d ", a[i]); getch(); } Output:
  • 47. Quick sort  Highly efficient and quickest sorting algorithm  Also follow divide and conquer technique to sort the given list  Here list is divided into two parts using pivot element and sort sub-list recursively  There are different versions to choose pivot element such as  Often pick the pivoting factor first.  Select the last factor as pivot at all times.  Choose a pivot element by chance.  The median pick as pivot.
  • 48.  It continues to pick a pivot element and break down the array into the array of single elements before adding them together to create a single sorted array.  This algorithm is pretty good for large data sets  Works by partitioning the sequence to be sorted and a recursive sorting of each partition in turn. Therefore partition exchange sort is also called.
  • 49. Working Method  From the list select an item, called a pivot.  Reorder the list so that all elements with values less than the pivot come before the pivot and all elements with values greater than the pivot comes after it (equal values can go either way). The pivot is in its final location, after this partitioning. This function is called the partition.  Recursively apply the above steps to the sub-list of elements with smaller values, and the sub-list of elements with higher values separately.
  • 50.  For example- we assume an array of 8 elements, such as 10 5 3 2 7 9 11 6 0 1 2 3 4 5 6 7 Now, we choose last element as pivot. So 7th position element i.e. 6 is the value of pivot element. Partition the list using pivot value i.e. smaller elements go to the left side and greater element goes to the right side of the pivot element. 5 3 2 6 7 9 11 10 Pivot
  • 51.  Again select last element as a pivot from both the list 5 3 2 6 7 9 11 10 pivot pivot Portion the both list using pivot. Recursively follow these steps to sort the given array, such as 2 3 5 6 7 9 10 11
  • 52. Algorithm Step 1 − Make the right-most index value pivot Step 2 − partition the array using pivot value Step 3 − quicksort left partition recursively Step 4 − quicksort right partition recursively
  • 53. Implementation using C #include <stdio.h> #include<conio.h> #define MAX 7 int intArray[MAX]={10,20,15,19,7,11,35}; void printline(int count) { int i; for(i = 0;i < count-1;i++) { printf("="); } printf("=n"); }
  • 54. void display() { int i; printf("["); // navigate through all items for(i = 0;i < MAX;i++) { printf("%d ",intArray[i]); } printf("]n"); } void swap(int num1, int num2) { int temp = intArray[num1]; intArray[num1] = intArray[num2]; intArray[num2] = temp; }
  • 55. int partition(int left, int right, int pivot) { int leftPointer = left -1; int rightPointer = right; while(1) { while(intArray[++leftPointer] < pivot) { //do nothing } while(rightPointer > 0 && intArray[--rightPointer] > pivot) { //do nothing } if(leftPointer >= rightPointer) { break;
  • 56. else { printf(" item swapped :%d,%dn", intArray[leftPointer],intArray[rightPointer]); swap(leftPointer,rightPointer); } } printf("npivot swapped :%d,%dn", intArray[leftPointer],intArray[right]); swap(leftPointer,right); printf("nUpdated list:"); display(); return leftPointer; } void quickSort(int left, int right) { if(right-left <= 0) { return; }
  • 57. else { int pivot = intArray[right]; int partitionPoint = partition(left, right, pivot); quickSort(left,partitionPoint-1); quickSort(partitionPoint+1,right); } } void main() { clrscr(); printf("nUnsorted list or array "); display(); printline(50); quickSort(0,MAX-1);
  • 58. printf("Sorted list using quick sort : "); display(); printline(50); getch(); } Output:
  • 59. Heap sort  Comparison based sorting algorithm  Heap is a special type of binary tree where elements are stored hierarchically  It is similar to selection sort where we first find the maximum element and place the maximum element at the end  it must either be ordered as a min heap (where parent node is less than or equal to the value of its children node) or a max-heap (where parent node is greater than or equal to the value of its children node)
  • 60. Working Method For Ascending order  Build a max heap from the input data.  At this point, the largest item is stored at the root of the heap. Replace it with the last item of the heap followed by reducing the size of heap by 1. Finally, heapify the root of tree.  Repeat above steps while size of heap is greater than 1.
  • 61. For example- We assume an array a[]={4,3,7,1,8,5}
  • 62.  First, build max-heap from the given tree .
  • 63.  Now swap the root node with the Heap node's last element. This implies that the largest element has moved to the appropriate location. So, delete from the tree . The tree looks as shown below, after removal.
  • 64.  Repeat step 3 until no elements are left in the heap
  • 67. Algorithm  Step 1 - Construct a Binary Tree with given list of Elements.  Step 2 - Transform the Binary Tree into Max Heap.  Step 3 - Delete the root element from Min Heap using Heapify method.  Step 4 - Put the deleted element into the Sorted list.  Step 5 - Repeat the same until Max Heap becomes empty.  Step 6 - Display the sorted list.
  • 68. Implementation using C #include<stdio.h> int temp; void swap_largest(int arr[], int n, int i) { int largest = i; // Initialize largest as root int left = 2*i + 1; int right = 2*i + 2; // If left child is larger than root if (left < n && arr[left] > arr[largest]) largest = left; // If right child is larger than largest if (right < n && arr[right] > arr[largest]) largest = right;
  • 69. // If largest is not root if (largest != i) { temp = arr[i]; arr[i] = arr[largest]; arr[largest] = temp; // Recursively call the swap_largest swap_largest(arr, n, largest); } } void heap(int arr[], int n) { int i; // Build heap from an unsorted array (rearrange array) for (i = n / 2 - 1; i >= 0; i--) swap_largest(arr, n, i);
  • 70. // One by one extract an element from heap for (i = n - 1; i >= 0; i--) { // Move current root to end temp = arr[0]; arr[0] = arr[i]; arr[i] = temp; // call swap_largest on the reduced heap swap_largest(arr, i, 0); } } void print(int arr[], int n) { int i; printf("Sorted list using heap sortn");
  • 71. for(i = 0; i < n; i++) { printf("%d ", arr[i]); } } void main() { int arr[10],n, i; clrscr(); printf("Enter number of elements"); scanf("%d", &n); for(i = 0; i < n; i++) { scanf("%d", &arr[i]); }
  • 73. Shell sort  Highly efficient sorting algorithm  Similar to insertion sort algorithm  Shell sort starts by sorting pairs of elements far apart from each other, then progressively reducing the gap between elements to be compared  Starting with far apart elements, it can move some out- of-place elements into the position faster than a simple nearest-neighbor exchange
  • 74. Algorithm  Step 1 − Initialize the value of h  Step 2 − Divide the list into smaller sub-list of equal interval h  Step 3 − Sort these sub-lists using insertion sort  Step 4 − Repeat until complete list is sorted
  • 75. For example- we consider an array arr []={17,3,9,1,8}
  • 76. Implementation using C #include<stdio.h> #include<conio.h> void main() { int arr[10], n, i, j, temp, gap; clrscr(); printf("Enter number of elements"); scanf("%d", &n); for(i = 0; i < n; i++) { scanf("%d", &arr[i]); }
  • 77. for (gap = n/2; gap > 0; gap = gap / 2) { for (i = gap; i < n; i = i + 1) { int temp = arr[i]; for (j = i; j >= gap && arr[j - gap] > temp; j = j - gap) arr[j] = arr[j - gap]; arr[j] = temp; } } printf("Sorted array using shell sortn"); for(i = 0; i < n; i++) { printf("%d ", arr[i]); } getch();
  • 79. Sorting algorithms complexities Algorithm Time Complexity Best Case Average Case Worst Case Bubble sort O(n) O(n2 ) O(n2 ) Selection sort O(n2 ) O(n2 ) O(n2 ) Insertion sort O(n) O(n2 ) O(n2 ) Heap sort O(nlogn) O(nlogn) O(nlogn) Quick sort O(nlogn) O(nlogn) O(n2 ) Merge sort O(nlogn) O(nlogn) O(nlogn) Shell sort O(n) O((nlogn))^2) O((nlogn))^2)
  • 80. Application  Uniqueness testing  Deleting duplicates  Prioritizing events  Frequency counting  Reconstructing the original order  Set intersection/union  Finding a target pair x, y such that x+y = z
  • 81. References  http://www.btechsmartclass.com/data_structures  https://www.faceprep.in/c/sorting-algorithms  https://www.tutorialspoint.com/data_structures_algor ithms/sorting_algorithms.htm