You are on page 1of 9

Comparison of

sorting algorithms
Tudoracu Mircea Laureniu
West University of Timioara
Romnia
mircea.tudorascu96@e-uvt.ro

1
Contents
Introduction............................................................................................................................................3
What is an algorithm..............................................................................................................................4
Bubble Sort.............................................................................................................................................4
Insertion Sort..........................................................................................................................................5
Selection Sort.........................................................................................................................................6
Quick Sort...............................................................................................................................................7
Merge Sort.............................................................................................................................................8
Comparing the algorithms......................................................................................................................9

2
Introduction

In this paper, there will be a small and brief analysis of some of the most known sorting
algorithms: BubbleSort, InsertionSort, SelectionSort, MergeSort, QuickSort. The ideas behind
these methods are different, with different results in terms of complexity and running time.

These five algorithms will pe run and tested on my personal PC:

processor: Intel Core i5 6200U @ 2.4 GHz


memory: 12 Gb of RAM
operating system: Windows 10 Education 64-bit
IDE: Code::Blocks v16.01
compiler: GNU GCC compiler

3
What is an algorithm

An algorithm can be defined as a series of actions / instructions in a certain order to


accomplish a task. An algorithm can perform better than others based on the number of
steps it takes so accomplish a certain task / result and / or used memory.

Of course, the same algorithm run on different computers may have different running
time, as the components of the computers may differ (e.g. processors from different
generations, more / less memory).

Here these five sorting algorithms will be run to sort (big) sets of natural numbers,
randomly generated: sets of 3000 numbers, sets of 20.000 numbers, sets of 50.000 numbers,
sets of 120.000 numbers, sets of 500.000 numbers and sets of 800.000.

Bubble Sort

Bubble Sort is a sorting algorithm that compares each pair of adjacent elements in an
array (elements that are in positions k and k+1) and, if they are not in the correct order (e.g.
sorting should be done in ascending order and element[k] is bigger than element[k+1]), they
are swapped. These steps are repeated until all the elements are in the correct order. The
idea behind this algorithm is simple, but the speed is not good. Worst case is when the inpus
is the list of elements in reverse order as we want, when Bubble Sort will do n2 steps. The
complexity is O(n2).

Here is the implementation of Bubble Sort in C programming language:

void bubble_sort(int v[], int n) aux=v[i];

{ v[i]=v[i+1];

int i, ok, aux; v[i+1]=aux;

do ok=0;

{ }

ok=1; }

for(i=0; i<n-1; i++) while(ok==0);

if(v[i]>v[i+1]) }

4
Insertion Sort

Insertion Sort is a sorting algorithm that goes / iterates through a list of elements, one
element at a time. For every element is found the correct location and inserts there. These
steps are repeats until there are no more elements to sort. The worst case is when the list of
numbers is in the inverse order that is wanted. Then there will be n2 swaps, so the complexity
is O(n2).

An implementation for Insertion Sort in C programming language:

void insertion_sort(int v[], int n)

int i, j, elem;

for(i=0;i<n;i++)

elem=v[i];

j=i-1;

while (j>=0 && v[j]>elem)

v[j+1]=v[j];

j-=1;

v[j+1]=elem;

Selection Sort

5
Selection Sort is a sorting algorithm that divides the list of the numbers in two: a sorted
part and an unsorted part. The idea is to find the smallest number in the unsorted part and
swap with the number from the unsored part that is most to the left. After that, the element
is in the correct place. The worst case of Selection Sort is O(n 2), when the elements are in the
inverse order than wanted.

An implementation of Selection Sort in C programming language is:

void selection_sort(int v[], int n)

int i, j, mn, aux;

for(i=0;i<n-1;i++)

mn=i;

for(j=i+1;j<n;j++)

if(v[j]<v[mn])

mn=j;

aux=v[i];

v[i]=v[i+1];

v[i+1]=aux;

Quick Sort

Quick Sort is one of the most used sorting algorithms, known for its efficency.

6
The steps of Quick Sort are fairly simple. First, an element from the array is picked, that
element being called a pivot. Then, reorder the array so all the elements smaller than the
pivot are on its left and all the elements that are bigger on its right. It is not necesarry that,
after this step, the elements on the left / right are in the right order / sorted. They just have
to be in the correct place (left or right) with respect to the pivot. After that, the pivot-
element is in its right place. The last step is to recursively apply these steps for the left and
right lists of elements (the list of elements that are smaller than the first pivot and the list
that contains the bigger elements).

The complexity of Quick Sort is O(nlogn), even though in the worst case, it makes O(n 2)
comparisons.

An implementation of Quick Sort in C programming language is:

void quick_sort(int inf, int sup) {

int x, i, j, t;

i = inf;

j = sup;

x = a[(i + j) / 2];

do {

while ( (i < sup) && (a[i] < x) ) i++;

while ( (j > inf) &&(a[j] > x) ) j--;

if ( i<= j ) {

t = a[i];

a[i] = a[j];

a[j] = t;

i++;

j--;

} while ( i <= j );

if ( inf < j ) quick_sort(inf, j);

if ( i < sup ) quick_sort(i, sup);

7
Merge Sort

Merge Sort is a very used sorting algorithm that has O(nlogn) complexity.

The steps for Merge Sort are:

If the list is of length 0 or 1, that it is sorted


If not, the list is divided in two sub lists of approximately the same length
Sort recursively every sub list by applying Merge Sort
Merge the two sub lists to get the original list sorted.

An implementation of Merge Sort in C programming language is:

void merging(int low, int mid, int high) void sorts(int low, int high) {
{
int mid;
int l1, l2, i;

for(l1 = low, l2 = mid + 1, i = low; l1


<= mid && l2 <= high; i++) { if(low < high) {

if(a[l1] <= a[l2]) mid = (low + high) / 2;

b[i] = a[l1++]; sorts(low, mid);

else sorts(mid+1, high);

b[i] = a[l2++]; merging(low, mid, high);

} } else {

while(l1 <= mid) return;

b[i++] = a[l1++]; }

while(l2 <= high) }

b[i++] = a[l2++];

for(i = low; i <= high; i++) f

a[i] = b[i]; f

8
Comparing the algorithms

The comparing tests will be on 3000, 20.000, 50.000, 120.000, 500.000, 800.000. If
the time to run will go beyond 75 seconds, it will be marked as too much.

Time to sort 3000 numbers for each Time to sort 500.000 numbers for
algorithm in seconds is: each algorithm in seconds is:

Bubble Sort: 0.044 Bubble Sort: too much


Insertion Sort: 0.020 Insertion Sort: too much
Selection Sort: 0.024 Selection Sort: too much
Merge Sort: 0.011 Merge Sort: 0.411
Quick Sort: 0.011 Quick Sort: 0.384

Time to sort 20.000 numbers for Time to sort 800.000 numbers for
each algorithm in seconds is: each algorithm in seconds is:

Bubble Sort: 1.901 Bubble Sort: too much


Insertion Sort: 0.334 Insertion Sort: too much
Selection Sort: 0.603 Selection Sort: too much
Merge Sort: 0.026 Merge Sort: 0.670
Quick Sort: 0.024 Quick Sort: 0.633

Time to sort 50.000 numbers for


each algorithm in seconds is:

Bubble Sort: 12.504


Insertion Sort: 1.984
Selection Sort: 3.670
Merge Sort: 0.051
Quick Sort: 0.047

Time to sort 120.000 numbers for


each algorithm in seconds is:

Bubble Sort: 73.166


Insertion Sort: 11.238
Selection Sort: 21.003
Merge Sort: 0.106
Quick Sort: 0.101

You might also like