You are on page 1of 42

Sorting

Kashiram Pokharel
Syllabus:
• Sorting refers to the operation or technique of arranging and
rearranging sets of data in some specific order. A collection of
records called a list where every record has one or more fields. The
fields which contain a unique value for each record is termed as
the key field.
• Sorting is the operation performed to arrange the records of a table
or list in some order according to some specific ordering criterion.
Sorting is performed according to some key value of each record.
• The records are either sorted either numerically or alphanumerically.
The records are then arranged in ascending or descending order
depending on the numerical value of the key.
Categories of Sorting
• The techniques of sorting can be divided into two categories. These are:
• Internal Sorting
• External Sorting
• Internal Sorting:
• If all the data that is to be sorted can be adjusted at a time in the main
memory, the internal sorting method is being performed.
• External Sorting:
• When the data that is to be sorted cannot be accommodated in the
memory at the same time and some has to be kept in auxiliary memory
such as hard disk, floppy disk, magnetic tapes etc, then external sorting
methods are performed.
Type of internal Sorting:
Insertion Sort:-
• The insertion sort inserts each element in proper place. If there are n element in array
and we place each element of array at proper place in the previously sorted element
list.
Algorithm:-
Consider N elements in the array are
• Pass 1: arr[0] is already sorted because of only one element.
• Pass 2 : arr[1] is inserted before or after arr[0]. So arr[0] & arr[i] are sorted.
• Pass 3: arr[2] is inserted before arr[0], in between arr[0] & arr[1] or after
• arr[0] . so arr[0] , arr[1] & arr [2] are sorted.
• Pass 4: arr[3] is inserted in to it’s proper place in array arr[0], arr[1], arr[2], arr[3] & are
sorted.
…………………
……………………
• Pass N: arr[ N-1] is inserted in to it’s proper place in array arr[0], arr[1], ……. Arr[N-1].
So arr[0], arr[N-1] are sorted.
void insertion(int x[],int n)
{
int i,j,temp;
for(i=0;i<n;i++)
{
temp=x[i];
for(j=i-1;j>=0;j--)
{
if(temp<x[j])
x[j+1]=x[j];
else
break;
}

x[j+1]=temp;
}
for (int k = 0; k < n; k++)
printf("%d ", x[k]);
}
Q. Trace algo. With the given data using insertion
sort.
Selection Sort:
• The selection is a straightforward process of sorting values.
• In this method, to sort the data in ascending order, the 0th element is
compared with all other elements. If the 0th element is found to be greater
than the compared element, the two values get interchanged.
• In this way after the first iteration, the smallest element is placed at 0th
position. The technique is repeated until the full array gets sorted.
• In selection sort, we Find the least (or greatest) value in the array, swap it
into the leftmost (or rightmost) component (where it belongs), and then
forget the leftmost component. Do this repeatedly.
• Let a[n] be a linear array of n elements. The selection sort works as follows
Algorithm
• Pass 1:- search the smallest element for arr[0] ……..arr[N-1].
• Interchange arr[0] with smallest element
• Result: arr[0] is sorted.
• Pass 2:- search the smallest element from arr[1],……….arr[N-1]
• Interchange arr[1] with smallest element
• Result: arr[0], arr[1] is sorted.
…………………
…………………
• Pass N-1:-
• - search the smallest element from arr[N-2] & arr[N-1]
• - Interchange arr[N-1] with smallest element
• Result: arr[0]…………. Arr[N-1] is sorted.
void selection(int x[], int n)
{
Algorithm: int i, j, larg,pos,c=0;
for (i = n-1; i>0; i--){
larg = x[0];
pos = 0;
for (j = 1; j<=i; j++)
{
if (x[j] > larg){
larg = x[j];
pos = j;
}
}
x[pos] = x[i];
x[i] = larg;
}
for (int k = 0; k < n; k++)
printf("%d ", x[k]);
}
Bubble sort
• The basic idea of this sort is to pass through the array sequentially several
times. Each pass consists of comparing each element in the array with its
successor (for example a[i] with a[i + 1]) and interchanging the two
elements if they are not in the proper order
• steps:-
• If N elements are given in memory then for sorting we do the following steps.
• Repeat through step 1 to 5 for N-1 elements
• First compare the 1st element and 2nd element of array. If 1st < 2nd then compare
the 2nd with 3rd.
• if 2nd > 3rd. Then interchange the value of 2nd & 3rd.
• Similarly compare until N-1th element is compared with nth element.
• Now, highest value element is reached at the Nth place. Elements will be compared
until N-1 elements.
void bubble(int x[],int n)
{ Time Complexity:
int i,j,tmp; Inner loop executes for (n-1) times when i=0, (n-2)
for(i=0;i<=n;i++) times when i=1 and so on:
{ Time complexity = (n-1) + (n-2) + (n-3) +
for(j=0;j<=n-i-1;j++) …………………………. +2 +1
{ = O(n2)
if(x[j]>x[j+1])
{
tmp=x[j];
x[j]=x[j+1];
x[j+1]=tmp;
}
}}
for (int k = 0; k <= n; k++)
printf("%d ", x[k]);
}
Shell sort
• The shell sort, sometimes called the “diminishing increment sort,” improves on the
insertion sort by breaking the original list into a number of smaller sub-lists, each of
which is sorted using an insertion sort.
• The unique way that these sub lists are chosen is the key to the shell sort. Instead of
breaking the list into sub lists of contiguous items, the shell sort uses an increment i,
sometimes called the gap, to create a sub list by choosing all items that are i items
apart.
• Shell sort is an algorithm that first sorts the elements far apart from each other and
successively reduces the interval between the elements to be sorted. It is a
generalized version of insertion sort.
• In shell sort, elements at a specific interval are sorted. The interval between the
elements is gradually decreased based on the sequence used. The performance of the
shell sort depends on the type of sequence used for a given input array.
• Some of the optimal sequences used are: Shell’s original sequence: N/2 , N/4 , …, 1
void shell(int arr[], int num)
{
int gap, j, k, tmp;
for (gap = num / 2; gap > 0; gap = gap / 2)
{
for (j = gap; j <= num; j++)
{
• Steps: for(k = j - gap; k >= 0; k = k - gap)
{
• Step 1 − Initialize the value of h if (arr[k+gap] >= arr[k])
break;
• Step 2 − Divide the list into else
smaller sub-list of equal interval {
tmp = arr[k];
h arr[k] = arr[k+gap];
• Step 3 − Sort these sub-lists arr[k+gap] = tmp;
}
using insertion sort }
}
• Step 4 − Repeat until complete }
list is sorted for (int k = 0; k <= num; k++)
printf("%d ", arr[k]);
}
• Suppose, we need to sort the following array.
9, 8, 3, 7, 5, 6, 4, 1
• We are using the shell’s original sequence (N/2, N/4, ...1) as intervals in our
algorithm.
• In the first loop, if the array size is N = 8 then, the elements lying at the interval of N/2 = 4
are compared and swapped if they are not in order.
• [if the 0th element is greater than the 4th one then, the 4th element is first stored in temp
variable and the 0th element (ie. greater element) is stored in the 4th position and the
element stored in temp is stored in the 0th position.]
• This process goes on for all the remaining elements.
• In the second loop, an interval of N/4 = 8/4 = 2 is taken and again the elements
lying at these intervals are sorted.
• The elements at 2nd and 0th position are also compared. The elements at 4th and 2nd
position are compared. All the elements in the array lying at the current interval are
compared.
• The same process goes on for remaining elements.
• Finally, when the interval is N/8 = 8/8 =1 then the array elements lying at the
interval of 1 are sorted. The array is now completely sorted.
Exchange Sort:
• The exchange sort is similar to its cousin, the bubble sort, in that it
compares elements of the array and swaps those that are out of order.
• The exchange sort compares the first element with each fo
• When the first pass through the array is complete, the exchange sort then
takes the second element and compares it with each following element
of the array swapping elements that are out of order.
• This sorting process continues until the entire array is ordered.
• The exchange sort, in some situations, is slightly more efficient than the
bubble sort. It is not necessary for the exchange sort to make that final
complete pass needed by the bubble sort to determine that it is finished.
Exchange Sort:
• elements again using an exchange sort for descending order.
Remember, a "pass" is defined as one full trip through the array
comparing and if necessary, swapping elements
public static void ExchangeSort ( int [ ] num )
{
int i, j, temp; //be sure that the temp variable is the same "type" as the array
for ( i = 0; i < num.length - 1; i ++ )
{
for ( j = i + 1; j < num.length; j ++ )
{
if( num[ i ] < num[ j ] ) //sorting into descending order
{
temp = num[ i ]; //swapping
num[ i ] = num[ j ];
num[ j ] = temp;
}
}
}
}
Exchange sort algorithm:
• The exchange sort is almost similar as the bubble sort.
• The exchange sort compares each element of an array and swap those elements
that are not in their proper position, just like a bubble sort does.
• The only difference between the two sorting algorithms is the manner in which
they compare the elements.
• The exchange sort compares the first element with each element of the array,
making a swap where is necessary.
• The exchange sort compares the first element with each following element of the
array, making any necessary swaps.
• When the first pass through the array is complete, the exchange sort then takes
the second element and compares it with each following element of the array
swapping elements that are out of order. This sorting process continues until the
entire array is ordered.
int main(void)
{
int array[5]; // An array of integers.
int length = 5; // Lenght of the array.
int i, j;
int temp;
//Some input
for (i = 0; i < 5; i++)
{
cout << "Enter a number: ";
cin >> array[i];
}
//Algorithm
for(i = 0; i < (length -1); i++)
{
for (j=(i + 1); j < length; j++)
{
if (array[i] < array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
//Some output
for (i = 0; i < 5; i++)
{
cout << array[i] << endl;
}
}
Divide and conquer algorithm
• Divide and conquer is an algorithm design paradigm based on multi-
branched recursion.
• A divide-and-conquer algorithm works by recursively breaking down a
problem into two or more sub-problems of the same or related type, until
these become simple enough to be solved directly.
• The solutions to the sub-problems are then combined to give a solution to
the original problem.
• It consists of three phases
1. Divide: Break the given problem into sub problems of same type.
2. Conquer: Recursively solve these sub problems
3. Combine: Appropriately combine the answers
• There are different type of divide and conquer sorting algorithm.
Merge sort
• It works on the principle of Divide and Conquer
• It divides input array in two halves, calls itself for the two halves and then merges the two sorted halves.
The merge () function is used for merging two halves.
• Merge sort is one of the most efficient sorting algorithms. Merge sort repeatedly breaks down a list into
several sub-lists until each sub-list consists of a single element and merging those sub-lists in a manner
that results into a sorted list.
• The division operation in merge sort is
• If r > l
1. Find the middle point to divide the array into two halves:
middle m = (l+r)/2
2. Call mergeSort for first half:
Call mergeSort(arr, l, m)
3. Call mergeSort for second half:
Call mergeSort(arr, m+1, r)
• 4. Merge the two halves sorted in step 2 and 3:
Call merge(arr, l, m, r)
Quick sort
• Quick sort is based on the divide-and-conquer approach based on
the idea of choosing one element as a pivot element and partitioning
the array around it, such that: Left side of pivot contains all the
elements that are less than the pivot element Right side contains all
elements greater than the pivot.
• It reduces the space complexity and removes the use of the auxiliary
array that is used in merge sort. Selecting a random pivot in an array
results in an improved time complexity in most of the cases.
Steps:

• Find a “pivot” item in the array. This item is the basis for comparison for a single
round.
• Place pivot at the proper place in list. For placing pivot at its proper place it follows
following steps
• Compare the pivot element one by one from right to left for getting the element
which has value less than pivot element. If found goto step 4
• Compare the pivot element one by one from current location of left to right for
getting the element which has value greater than pivot element. if found , go to
step 5
• If the left pointer is less than or equal to the right pointer, then swap the values at
these locations in the array.
• Repeat steps 3, 4 and 5 until right pointer is less than left pointer. Swap the pivot
pointer with the right pointer. And Create two subsists left & right side of pivot.
• Repeat the same process until all elements of first are at proper position in list.
QuickSort(A,l,r)
{
if(l<r)
{
p = Partition(A,l,r);
QuickSort(A,l,p-1);
QuickSort(A,p+1,r);
} }
Partition(A,l,r)
{
x =l;
y =r ;
p = A[l];
while(x<y)
{
while(A[x] <= p)
x++;
while(A[y] >=p)
y--;
if(x<y)
swap(A[x],A[y]);
}
A[l] = A[y];
A[y] = p;
return y; } //return position of pivot
• Time Complexity:
• Best Case:
• Divides the array into two partitions of equal size, therefore
• T(n) = 2T(n/2) + O(n) , Solving this recurrence we get,
• T(n)=O(nlogn)
• Worst case:
• when one partition contains the n-1 elements and another partition contains only one
element. Therefore its recurrence relation is:
• T(n) = T(n-1) + O(n), Solving this recurrence we get
• T(n)=O(n2)
• Average case:
• Good and bad splits are randomly distributed across throughout the tree
• T1(n)= 2T'(n/2) + O(n) Balanced
• T'(n)= T(n –1) + O(n) Unbalanced
• Solving:
• B(n)= 2(B(n/2 –1) + Θ(n/2)) + Θ(n)
= 2B(n/2 –1) + Θ(n)
= O(nlogn)
=>T(n)=O(nlogn)
Heap sort:
• A heap is a binary tree that satisfied the following propertiers:-
• ® shape property
• ® Order property.
• By the shape property we mean that heap must be a complete binary tree
where as by order property we mean that for every node in the heap the
value store in the heap node is greater than or equal to the value to the
value an each of its’ children. A heap that satisfied this property is known
as max heap.
• However, If the order property is such that for every node. In the heap the
value stored in that node is less than or equal to the value in each of it’s
children.That heap is known as minimum heap
Algorithm:

• Working of Heap Sort


• Since the tree satisfies Max-Heap property, then the largest item is
stored at the root node.
• Swap: Remove the root element and put at the end of the array (nth
position) Put the last item of the tree (heap) at the vacant place.
• Remove: Reduce the size of the heap by 1.
• Heapify: Heapify the root element again so that we have the highest
element at root.
• The process is repeated until all the items of the list are sorted.
Binary sort:
Radix sort:
• In radix sort, we sort the item in terms of it’s digits. If we have list of
nos.then there will be 10 parts from 0 to 9 because radix is 10.
• Algo
• consider the list n digit of nos, then there will be 10 parts from 0 to 9.
• in the first pass take the nos in parts on the basis of unit digits.
• In the second pass the base will be ten digit.
• similarly Repeat for n passes for n digits.

You might also like