You are on page 1of 17

Bubble Sort

 Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements
if they are in wrong order.
 This algorithm is suitable for small data sets
 Its average and worst case complexity are of (n^2) where n is the number of items.
 It is known as bubble sort, because with every complete iteration the largest element bubbles up
towards the last place or the highest index just like a water bubble rises up to the water surface.

Algorithm :
 Step 1 – Starting with the first element(index = 0), compare the current element with the next element
of the array.
 Step 2 – If the current element is greater than the next element of the array, swap them.
 Step 3 – If the current element is less than the next element, move to the next element.
 Step 4 – Repeat Step 1 till the list is sorted.
Insertion Sort:

In insertion sort algorithm, every iteration moves an element from unsorted portion to sorted
portion until all the elements are sorted in the list.

Complexity of the Insertion Sort Algorithm


To sort an unsorted list with 'n' number of elements, we need to make (1+2+3+......+n-1) = (n (n-
1))/2 number of comparisions in the worst case. If the list is already sorted then it
requires 'n' number of comparisions.
Worst Case : O(n2)

https://www.youtube.com/watch?v=UAmaFHWBoYQ

https://www.hackerearth.com/practice/algorithms/sorting/insertion-sort/visualize/
INSERTION SORT

Suppose we need to sort the following array.

1. The first element in the array is assumed to be sorted. Take the second element and
store it separately in key.

Compare key with the first element. If the first element is greater than key, then key is
placed in front of the first elemet.
2. Now, the first two elements are sorted.

Take the third element and compare it with the elements on the left of it. Placed it
just behind the element smaller than it. If there is no element smaller than it, then
place it at the beginning of the array.
3. In a similar way, place every unsorted element at its correct position.
voidinsertionSort(int array[], int size)
{
for (int step = 1; step < size; step++)
{
int key = array[step];
int j = step - 1;
while (key < array[j] && j >= 0)
{
array[j + 1] = array[j];
--j;
}
array[j + 1] = key;
}
}
Quick sort:

Quick sort algorithm first selects a value that is to be used as split-point (pivot element) from
the list of given numbers. Elements are arranged so that, all the numbers smaller than the
split-point are brought to one side of the list and the rest on the other. This operation is
called splitting.

After this, the list is divided into sub lists, one sub list containing the elements less than the
pivot element and the other containing the elements more than the pivot element. These two
lists are again individually sorted using Quick sort algorithm that is by again finding a split-
point and dividing into two parts. The process is recursively done until all the elements are
arranged in order. So it is called divide and conquer algorithm.

Select the first element as pivot element and place it, at its position using an algorithm

p=a[start]; /* Here start is starting index */

We will find the biggest element than the pivot element from first using “i” and
smallest element from the last using “j” and interchange them

i=start;
j=end;

while(a[i]<=p) /* continues until get biggest element than pivot element*/


i++;
while(a[j]>=p) /* continues until get smallest element than pivot element*/
j++;
if(i<j)
temp=a[i],a[i]=a[j],a[j]=temp; /* inter changing a[i] and a[j] */
follow the same procedure as long as i<j

We follow the same procedure as long as i<j

As i<j is false, the current process is stopped and a[start] and a[j] are interchanged.

Now the pivot element is at its position “j”, which is the split position for next sub arrays
because all elements to its left are smaller and to its right are greater.

a[start]=a[j];

a[j]=p;

returnj; /*returning splitting position */

Quick sort – Splitting position:


intsplit(intstart,intend)
{
intp=a[start];
inti=start,j=end,temp;
while(i<j)
{
while(a[i]<=p)
i++;
while(a[j]>p)
j--;
if(i<j)
temp=a[i],a[i]=a[j],a[j]=temp;
}
a[start]=a[j];
a[j]=p;
returnj;
}
Now this procedure accepts starting, ending index of a sub array and returns split point by
arranging the elements so that, all the elements left to split point are smaller than pivot
element and right to split point are greater than pivot element

We continue the same process recursively with other sub arrays. It can be done using a
recursive procedure

voidqsort(intstart,intend)
{
Ints;
if(start>=end)
return;
s=split(start,end);
qsort(start,s-1); /* takes left sub array to split point */
qsort(s+1,end); /* takes right sub array to split point */
}
Quick sort implementing in C:
#include<stdio.h>
inta[50];
voidqsort(int,int);
intsplit(int,int);
intmain()
{
Intn,i;
printf("How many elements?");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
qsort(0,n-1);
printf("The resultant array:\n");
for(i=0;i<n;i++)
printf("%5d",a[i]);
return0;
}
Voidqsort(intstart,intend)
{
ints;
if(start>=end)
return;
s=split(start,end);
qsort(start,s-1);
qsort(s+1,end);
}
intsplit(intstart,intend)
{
Intp=a[start];
Inti=start,j=end,temp;
while(i<j)
{
while(a[i]<=p)
i++;
while(a[j]>p)
j--;
if(i<j)
temp=a[i],a[i]=a[j],a[j]=temp;
}
a[start]=a[j];
a[j]=p;
returnj;
}
Radix Sort Algorithm (non comparison sorting)

In radix sort algorithm, a list of integer numbers will be sorted based on the digits of

individual numbers. Sorting is performed from least significant digit to the most

significant digit. Radix sort algorithm requires the number of passes which are equal to the

number of digits present in the largest number among the list of numbers. For example, if

the largest number is a 3 digit number then that list is sorted with 3 passes.

Step by Step Process

• Step 1 - Define 10 queues each representing a bucket for each digit from 0 to 9.

Step 2 - Consider the least significant digit of each number in the list which is to be

sorted.

• Step 3 - Insert each number into their respective queue based on the least significant

digit.

• Step 4 - Group all the numbers from queue 0 to queue 9 in the order they have inserted

into their respective queues.

• Step 5 - Repeat from step 3 based on the next least significant digit.

• Step 6 - Repeat from step 2 until all the numbers are grouped based on the

most significant digit.

Example

You might also like