Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

Proiectarea algoritmilor

Sortari

S.L.dr.ing. Cornelia Melenti


cornelia.melenti@cs.utcluj.ro
Applications of Sorting
● Facilitate searching
● Building indices
● Identify quantiles of a distribution
● Identify unique values
● Browsing data
Sorting efficiency
Sorting = rearrange a set of key in ascending or descending order
x0<x1<x2< …..<xn-2<xn-1

The two main criterias to judge the efficiency:


● time taken to sort the given data
● memory space required to do so
Sorting algorithms
Classification the sorting algorithms:
- elementary methods
- bubble sort
- selection sort
- insertion sort
- shell sort
- divide et impera methods
- merge sort
- quick sort
- heap sort
- for specifical data
- radix sort
- counting sort
- ….
Elementary Methods
Suitable for
• Small datasets
• Specialized applications

But …
● Elementary sorts are very inefficient
● Typically, time requirements are O(N2)
● Probably, most common inefficiency in scientific computing
● Make programs “break” with large datasets
Basic Building Blocks
■ A type for each element
■ #define Item int
■ Compare two elements
■ bool isLess(Item a, Item b)
■ { return a < b; }
■ Exchange two elements
■ void Exchange(Item & a, Item & b)
■ { Item temp = a; a = b; b = temp; }
■ Compare and exchange two elements
■ Item CompExch(Item & a, Item & b)
■ {if (isLess(b, a))
■ Exchange(a, b);
■}
Bubble Sort Algorithm
The steps involved in bubble sort (for sorting a given array in ascending order):

1. Starting with the first element (i = 0), compare the current element with the next

element of the array.

2. If the current element is greater than the next element of the array, swap them.

3. If the current element is less than the next element, move to the next element.

4. Repeat Step 1 with i = i+1, … until i < right limit

5. repeat the step 1 - 4 for n times


Bubble Sort Algorithm
void bubble(int a[ ], int n)
{ int i, j, aux;
for (i = 0; i<n; i++)
for (j = 0, j<n-i-1, j++) //iteratia i
if (a[j] > a[j+1]
{
aux = a[j];
a[j] = a[j+1];
a[j+1] = aux;
}
}
Algorithm efficiency: (n-1)+(n-2)+ …+ 2+1=n*(n-1)/2 interation => O(n2)
- Worst Case Time Complexity [ Big-O ]: O(n2) - when the list is already sorted in reverse order
- Best Case Time Complexity [Big-omega]: O(n) - when the list is already sorted
- Average Time Complexity [Big-theta]: O(n2)
- Space Complexity: O(1) only for aux variable
Bubble Sort Algorithm
Example:
unordered list: 23, 8, 12, 4, 22, 7 yes => swap elem
n=5 no => no swap
i=0 if(a[j] > a[j+1])
j=0 23 > 8 yes => 8, 23, 12, 4, 22, 7
j=1 23 > 12 yes => 8, 12, 23, 4, 22, 7
j=2 23 > 4 yes => 8, 12, 4, 23, 22, 7
j=3 23 > 22 yes => 8, 12, 4, 22, 23, 7
j=4 23 > 7 yes => 8, 12, 4, 22, 7, 23
i=1
j=0 8 > 12 no => 8, 12, 4, 22, 7, 23
j=1 12 > 4 yes => 8, 4, 12, 22, 7, 23
j=2 12 > 22 no => 8, 4, 12, 22, 7, 23
j=3 22 > 7 yes => 8, 4, 12, 7, 22, 23
i=2
j=0 8>4 yes => 4, 8, 12, 7, 22, 23
j=1 8 > 12 no => 4, 8, 12, 7, 22, 23
j=2 12 > 7 yes => 4, 8, 7, 12, 22, 23
i = 3 …..

sorted list: 4, 7, 8, 12, 22, 23


Bubble Sort Algorithm with flag
void bubbleflag(int a[ ], int n)
{ int i, j, aux, flag;
for (i = 0; i<n; i++)
{for (j = 0, j<n-i-1, j++) //iteratia i
{flag = 0;
if (a[j] > a[j+1]
{ aux = a[j];
a[j] = a[j+1];
a[j+1] = aux;
flag = 1;
}
}
if (flag == 0) // nu au mai fost interschimbari
break;
} }
Main advantage: the simplicity of the algorithm
Selection Sort Algorithm
The steps involved in selection sort (for sorting a given array in ascending order):
1. Starting from the first element, we search the smallest element in the array, and
replace it with the element in the first position.

2. We then move on to the second position, and look for smallest element present in
the subarray, starting from index 1, till the last index

3. We replace the element at the second position in the original array, or we can say
at the first position in the subarray, with the second smallest element.

4. This is repeated, until the array is completely sorted.


Selection Sort Algorithm
Functii ajutatoare
void swap(int a[ ], int i1, int i2)
{ int aux;
aux = a[i1];
a[i2] = a[i1];
a[i1] = aux;
}
//find the index for the smallest element in the subarray, starting from index 1, to the last index
int indexminim(int a[ ], int k, int n)
{int i, valmin = a[k], indexmin = k;
for (i=indexmin+1; i<n; i++)
if (a[i] < valmin)
{ indexmin = i;
valmin = a[i];
}
return indexmin;
}
}
Selection Sort Algorithm
void selection(int a[ ], int n)
{ int i, index;
for (i = 0; i<n; i++)
{index = indexminim(a, i, n);
swap (a, i, index);
}
}

Algorithm efficiency:
● Worst Case Time Complexity [ Big-O ]: O(n2)
● Best Case Time Complexity [Big-omega]: O(n2)
● Average Time Complexity [Big-theta]: O(n2)
● Space Complexity: O(1) only for aux variable

Disadvantage: unstable algorithm (if we have equal elements)


Selection Sort Algorithm
Example:
unordered list: a = {23, 8, 12, 4, 22, 7}
n=5

i = 0, a1 = { 8, 12, 4, 22, 7} min = 23


index = 3 => swap a[0] with a[3] => a = {4, 8, 12, 23, 22, 7}

i = 1, a1 = {12, 23, 22, 7} min = 8


index = 5 => swap a[1] with a[5] => a = {4, 7, 12, 23, 22, 8}

i = 2, a1 = {23, 22, 8} min = 12


index = 5 => swap a[2] with a[5] => a = {4, 7, 8, 23, 22, 12}

i = 3, a1 = { 22, 12} min = 23


index = 5 => swap a[3] with a[5] => a = {4, 7, 8, 12, 22, 23}

i = 4, a1 = { 22} min = 23
index = 5 => 22 < 23 => a = {4, 7, 8, 12, 22, 23}

sorted list: 4, 7, 8, 12, 22, 23


Insertion Sort Algorithm
The steps involved in insertion sort (for sorting a given array in ascending order):
1. We start by making the second element of the given array, i.e. element at index 1, the key.
The key element here is the new element that we need to add to our existing sorted set of
elements

2. We compare the key element with the element(s) before it, in this case, element at index 0:
○ If the key element is less than the first element, we insert the key element before the
first element.
○ If the key element is greater than the first element, then we insert it after the first
element.

3. Then, we make the third element of the array as key and will compare it with elements to it's
left and insert it at the right position.

4. And we go on repeating this, until the array is sorted.


Insertion Sort Algorithm
Some characteristic of the insertion sort algorithm
1. It is efficient for smaller data sets, but very inefficient for larger lists.

2. Insertion Sort is adaptive, that means it reduces its total number of steps if a partially sorted
array is provided as input, making it efficient.

3. It is better than Selection Sort and Bubble Sort algorithms.

4. Its space complexity is less. Like bubble Sort, insertion sort also requires a single additional
memory space.

5. It is a stable sorting technique, as it does not change the relative order of elements which are
equal.
Insertion Sort Algorithm
void insertion(int a[ ], int dim)
{ int i, j, key;
for (i = 1; i<dim; i++)
{j = i;
while(j > 0 && (a[j-1] > a[j]))
{key = a[j];
a[j] = a[j-1];
a[j-1] = key;
j--;
}
}
}
Algorithm efficiency:
● Worst Case Time Complexity [ Big-O ]: O(n2)
● Best Case Time Complexity [Big-omega]: O(n)
● Average Time Complexity [Big-theta]: O(n2)
● Space Complexity: O(1) only for aux variable
Insertion Sort Algorithm
Example:
unordered list: a = {23, 8, 12, 4, 22, 7}
n=5
i = 1, j = 1, key = 8 a = {23, 8, 12, 4, 22, 7}
j > 0, 23 > 8 => {8, 23, 12, 4, 22, 7} j-- => j = 0 swap (8, 23)

i = 2, j = 2, key = 12 a = {8, 23, 12, 4, 22, 7}


j > 0, 23 > 12, => {8, 12, 23, 4, 22, 7} j-- => j = 1 swap (12, 23)
j > 0, 18 < 12 => {8, 12, 23, 4, 22, 7} j-- => j = 0 Nothing to change

i = 3, j = 3, key = 4 a = {8, 12, 23, 4, 22, 7}


j > 0, 23 > 4, => {8, 12, 4, 23, 22, 7} j-- => j = 2 swap (4, 23)
j > 0, 12 > 4, => {8, 4, 12, 23, 22, 7} j-- => j = 1 swap (4, 12)
j > 0, 8 > 4, => {4, 8, 12, 23, 22, 7} j-- => j = 0 swap (4, 8)

i = 4, j = 4, key = 22 a = {4, 8, 12, 23, 22, 7}


j > 0, 23 > 22, => {4, 8, 12, 22, 23, 7} j-- => j = 3 swap (22, 23)
j > 0, 12 < 22 => {4, 8, 12, 22, 23, 7} j-- => j = 2 Nothing to change
j > 0, 8 < 12 , => {4, 8, 12, 22, 23, 7} j-- => j = 1 Nothing to change
j > 0, 4 < 8, => {4, 8, 12, 22, 23, 7} j-- => j = 0 Nothing to change

i = 5, j = 5, key = 7, a = {4, 8, 12, 22, 23, 7} …..


NEXT TIME

● MERGE SORT
● QUICK SORT
● SHELL SORT
● RADIX SORT
● COUNTING SORT

You might also like