Professional Documents
Culture Documents
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.
3
What is an algorithm
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).
{ v[i]=v[i+1];
do ok=0;
{ }
ok=1; }
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).
int i, j, elem;
for(i=0;i<n;i++)
elem=v[i];
j=i-1;
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.
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.
int x, i, j, t;
i = inf;
j = sup;
x = a[(i + j) / 2];
do {
if ( i<= j ) {
t = a[i];
a[i] = a[j];
a[j] = t;
i++;
j--;
} while ( i <= j );
7
Merge Sort
Merge Sort is a very used sorting algorithm that has O(nlogn) complexity.
void merging(int low, int mid, int high) void sorts(int low, int high) {
{
int mid;
int l1, l2, i;
} } else {
b[i++] = a[l1++]; }
b[i++] = a[l2++];
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:
Time to sort 20.000 numbers for Time to sort 800.000 numbers for
each algorithm in seconds is: each algorithm in seconds is: