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

More Related Content

PPT
Time andspacecomplexity
LAKSHMITHARUN PONNAM
 
PPTX
Sorting algorithms
Trupti Agrawal
 
PDF
Sorting Algorithms
Mohammed Hussein
 
PPTX
Merge sort and quick sort
Shakila Mahjabin
 
PPTX
Bubble sort, Selection sort SORTING .pptx
Kalpana Mohan
 
PPTX
Quick sort
Jehat Hassan
 
PPTX
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
Umesh Kumar
 
PPTX
sorting and searching.pptx
ParagAhir1
 
Time andspacecomplexity
LAKSHMITHARUN PONNAM
 
Sorting algorithms
Trupti Agrawal
 
Sorting Algorithms
Mohammed Hussein
 
Merge sort and quick sort
Shakila Mahjabin
 
Bubble sort, Selection sort SORTING .pptx
Kalpana Mohan
 
Quick sort
Jehat Hassan
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
Umesh Kumar
 
sorting and searching.pptx
ParagAhir1
 

What's hot (20)

PPT
Stack Data Structure & It's Application
Tech_MX
 
PPTX
Selection sort 1
asmhemu
 
PPTX
Linear search-and-binary-search
International Islamic University
 
PPT
Selection sort
amna izzat
 
PPTX
Selection sort algorithm presentation, selection sort example using power point
University of Science and Technology Chitttagong
 
PPTX
Insertion sort
almaqboli
 
PPTX
Linked list
akshat360
 
PPTX
Searching & Sorting Algorithms
Rahul Jamwal
 
PDF
linear search and binary search
Zia Ush Shamszaman
 
PPTX
Presentation on the topic selection sort
District Administration
 
PPTX
Insertion sort
MYER301
 
PDF
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 
PPT
C++ Arrays
أحمد محمد
 
PPT
Infix to Postfix Conversion Using Stack
Soumen Santra
 
PPT
Binary Search
kunj desai
 
PPT
Divide and conquer
Dr Shashikant Athawale
 
PDF
Searching and Sorting Techniques in Data Structure
Balwant Gorad
 
PPTX
Searching techniques in Data Structure And Algorithm
03446940736
 
PPT
Stack
srihariyenduri
 
PPTX
Insertion sort
Monalisa Patel
 
Stack Data Structure & It's Application
Tech_MX
 
Selection sort 1
asmhemu
 
Linear search-and-binary-search
International Islamic University
 
Selection sort
amna izzat
 
Selection sort algorithm presentation, selection sort example using power point
University of Science and Technology Chitttagong
 
Insertion sort
almaqboli
 
Linked list
akshat360
 
Searching & Sorting Algorithms
Rahul Jamwal
 
linear search and binary search
Zia Ush Shamszaman
 
Presentation on the topic selection sort
District Administration
 
Insertion sort
MYER301
 
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 
C++ Arrays
أحمد محمد
 
Infix to Postfix Conversion Using Stack
Soumen Santra
 
Binary Search
kunj desai
 
Divide and conquer
Dr Shashikant Athawale
 
Searching and Sorting Techniques in Data Structure
Balwant Gorad
 
Searching techniques in Data Structure And Algorithm
03446940736
 
Insertion sort
Monalisa Patel
 
Ad

Similar to Sorting algorithms (20)

PPTX
Unit vii sorting
Tribhuvan University
 
PPTX
sorting-160810203705.pptx
VarchasvaTiwari2
 
PPT
Sorting algorithums > Data Structures & Algorithums
Ain-ul-Moiz Khawaja
 
PPTX
Unit 7 sorting
Dabbal Singh Mahara
 
PPTX
Chapter-2.pptx
selemonGamo
 
PDF
Sorting
A. S. M. Shafi
 
PPT
358 33 powerpoint-slides_14-sorting_chapter-14
sumitbardhan
 
PDF
Chapter Two.pdf
abay golla
 
PPT
Data Structure (MC501)
Kamal Singh Lodhi
 
PPTX
searching in data structure.pptx
chouguleamruta24
 
PPTX
Chapter 8 Sorting in the context of DSA.pptx
Dibyesh1
 
PPTX
SORTING techniques.pptx
Dr.Shweta
 
PDF
L 14-ct1120
Zia Ush Shamszaman
 
PPTX
Sorting method data structure
sunilchute1
 
PPTX
Selection sort and insertion sort
May Ann Mendoza
 
PPTX
Selection Sort and Insertion Sort
tamayaoraquel
 
PDF
Sorting
A. S. M. Shafi
 
PPTX
1.4 Sorting.pptx
Sujan527908
 
PDF
465659705-15-DSA-PPT-Sorting-Techniques-I.pdf
anushasanapathi1
 
PPTX
Searching and sorting Techniques in Data structures
PRIANKA R
 
Unit vii sorting
Tribhuvan University
 
sorting-160810203705.pptx
VarchasvaTiwari2
 
Sorting algorithums > Data Structures & Algorithums
Ain-ul-Moiz Khawaja
 
Unit 7 sorting
Dabbal Singh Mahara
 
Chapter-2.pptx
selemonGamo
 
358 33 powerpoint-slides_14-sorting_chapter-14
sumitbardhan
 
Chapter Two.pdf
abay golla
 
Data Structure (MC501)
Kamal Singh Lodhi
 
searching in data structure.pptx
chouguleamruta24
 
Chapter 8 Sorting in the context of DSA.pptx
Dibyesh1
 
SORTING techniques.pptx
Dr.Shweta
 
L 14-ct1120
Zia Ush Shamszaman
 
Sorting method data structure
sunilchute1
 
Selection sort and insertion sort
May Ann Mendoza
 
Selection Sort and Insertion Sort
tamayaoraquel
 
1.4 Sorting.pptx
Sujan527908
 
465659705-15-DSA-PPT-Sorting-Techniques-I.pdf
anushasanapathi1
 
Searching and sorting Techniques in Data structures
PRIANKA R
 
Ad

More from CHANDAN KUMAR (13)

PPTX
Chart and graphs in R programming language
CHANDAN KUMAR
 
PPTX
Raid technology
CHANDAN KUMAR
 
PPTX
Pointers in c
CHANDAN KUMAR
 
PPT
Searching in c language
CHANDAN KUMAR
 
PPT
Greedy algorithm
CHANDAN KUMAR
 
PPT
Divide and conquer algorithm
CHANDAN KUMAR
 
PPTX
Arrays in c
CHANDAN KUMAR
 
PPTX
Loops in c programming
CHANDAN KUMAR
 
PPT
Linked List
CHANDAN KUMAR
 
PPTX
Stack and queue
CHANDAN KUMAR
 
PPTX
Technical questions for interview c programming
CHANDAN KUMAR
 
PPT
Decision making using if statement
CHANDAN KUMAR
 
PPTX
"A short and knowledgeable concept about Algorithm "
CHANDAN KUMAR
 
Chart and graphs in R programming language
CHANDAN KUMAR
 
Raid technology
CHANDAN KUMAR
 
Pointers in c
CHANDAN KUMAR
 
Searching in c language
CHANDAN KUMAR
 
Greedy algorithm
CHANDAN KUMAR
 
Divide and conquer algorithm
CHANDAN KUMAR
 
Arrays in c
CHANDAN KUMAR
 
Loops in c programming
CHANDAN KUMAR
 
Linked List
CHANDAN KUMAR
 
Stack and queue
CHANDAN KUMAR
 
Technical questions for interview c programming
CHANDAN KUMAR
 
Decision making using if statement
CHANDAN KUMAR
 
"A short and knowledgeable concept about Algorithm "
CHANDAN KUMAR
 

Recently uploaded (20)

PDF
Exploring-Forces 5.pdf/8th science curiosity/by sandeep swamy notes/ppt
Sandeep Swamy
 
PDF
7.Particulate-Nature-of-Matter.ppt/8th class science curiosity/by k sandeep s...
Sandeep Swamy
 
PPTX
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
PPTX
Congenital Hypothyroidism pptx
AneetaSharma15
 
PDF
Arihant Class 10 All in One Maths full pdf
sajal kumar
 
PPTX
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
DOCX
UPPER GASTRO INTESTINAL DISORDER.docx
BANDITA PATRA
 
PPTX
Care of patients with elImination deviation.pptx
AneetaSharma15
 
PDF
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PDF
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
DOCX
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
PPT
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
PPTX
ACUTE NASOPHARYNGITIS. pptx
AneetaSharma15
 
PPTX
PREVENTIVE PEDIATRIC. pptx
AneetaSharma15
 
PPTX
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
PPTX
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
PPTX
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
Exploring-Forces 5.pdf/8th science curiosity/by sandeep swamy notes/ppt
Sandeep Swamy
 
7.Particulate-Nature-of-Matter.ppt/8th class science curiosity/by k sandeep s...
Sandeep Swamy
 
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
Congenital Hypothyroidism pptx
AneetaSharma15
 
Arihant Class 10 All in One Maths full pdf
sajal kumar
 
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
UPPER GASTRO INTESTINAL DISORDER.docx
BANDITA PATRA
 
Care of patients with elImination deviation.pptx
AneetaSharma15
 
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
ACUTE NASOPHARYNGITIS. pptx
AneetaSharma15
 
PREVENTIVE PEDIATRIC. pptx
AneetaSharma15
 
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
How to Manage Leads in Odoo 18 CRM - Odoo Slides
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