This document provides an overview of sorting algorithms. It defines sorting as arranging data in a particular order like ascending or descending. Common sorting algorithms discussed include bubble sort, selection sort, insertion sort, merge sort, and quick sort. For each algorithm, the working method, implementation in C, time and space complexity is explained. The document also covers sorting terminology like stable vs unstable sorting and adaptive vs non-adaptive algorithms. Overall, the document serves as a comprehensive introduction to sorting and different sorting techniques.
Prepared By:
Dr. ChandanKumar
Assistant Professor, Computer Science & Engineering Department
Invertis University, Bareilly
2.
Introduction
It isa 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 sortingmethods 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 importantterminology
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 sortingalgorithm 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 complexityof 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 getthe 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
Bubble sort
Simpleand 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
Startwith 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 119 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 fifthand 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 allelements 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>
voidmain()
{
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;
}
}
}
Time complexityof 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
Straightforwardprocess 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
The0th 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 completingthe 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>
voidmain()
{
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;
}
}
}
Time complexityof 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-placecomparison-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
The0th 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] anda[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>
voidmain()
{
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;
}
Merge sort
Previoussorting 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
Forexample- 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 : Thismethod 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 1223
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++];
}
void main() {
inti;
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
Highlyefficient 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 continuesto 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
Fromthe 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 selectlast 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() {
inti;
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;
Heap sort
Comparisonbased 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 Ascendingorder
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.
Now swapthe 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 step3 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>
inttemp;
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 largestis 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 byone 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]);
}
Shell sort
Highlyefficient 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
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