You are on page 1of 22

Sorting algorithms

S.L.dr.ing. Cornelia Melenti


cornelia.melenti@cs.utcluj.ro
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);
■}
Divide et impera method
■ The concept of Divide et problem
Impera involves three
steps DIVIDE
sub-problem sub-problem
■ Divide the problem into
small problem
■ Conquer the small solution to solution to
problem by solving them sub-problem CONQUER sub-problem
■ Combine the solutions
of the small problem to find problem
the solution of the problem
COMBINE
Merge Sort Algorithm
The steps involved in merge sort (for sorting a given array in ascending order):
1. We take a variable p and store the starting index of our array in this. And we take
another variable r and store the last index of array in it.

2. Then we find the middle of the array using the formula (p + r)/2 and mark the
middle index as q, and break the array into two subarrays, from p to q and from q
+ 1 to r index.

3. Then we divide these 2 subarrays again, just like we divided our main array and
this continues.

4. Once we have divided the main array into subarrays with single elements, then we
start merging the subarrays.
Merge Sort Algorithm
void mergeSort(int a[], int p, int r)
{
int q;
if(p < r)
{
q = (p + r) / 2;
mergeSort(a, p, q);
mergeSort(a, q+1, r);
merge(a, p, q, r);
}
}

n - dimension of a
Merge Sort Algorithm
// function to merge the subarrays
void merge(int a[], int p, int q, int r)
{ int b[n]; //same size of a[]
int i, j, k;
k = 0; i = p;j = q + 1;
while(i <= q && j <= r)
{ if(a[i] < a[j])
{ b[k] = a[i]; k++; i++; }
else
{ b[k] = a[j]; k++; j++; }
}
while(i <= q)
{ b[k] = a[i]; k++; i++; }
while(j <= r)
{ b[k] = a[j]; k++; j++; }
for(i=r; i >= p; i--)
{
a[i] = b[k]; // copying back the sorted list to a[]
k--;
}
}
Merge Sort Algorithm
Complexity
● Worst Case Time Complexity [ Big-O ]: O(n*log n)
● Best Case Time Complexity [Big-omega]: O(n*log n)
● Average Time Complexity [Big-theta]: O(n*log n)
● Space Complexity: O(n)

Note
● It requires equal amount of additional space as the unsorted array.
Hence its not at all recommended for searching large unsorted arrays.
● It is the best Sorting technique used for sorting Linked Lists.
Merge Sort Algorithm
23 8 12 4 22 7 15

23 8 12 4 22 7 15

23 8 12 4 22 7 15

DIVIDE
22 7
23 8 12 4

8 23 4 12 7 22 SOLVE

7 22
8 23 4 12 MERGE

4 8 12 23 7 15 22

4 7 8 12 15 22 23
Quick Sort Algorithm
It is also called partition-exchange sort.

This algorithm divides the list into three main parts:

1. Elements less than the Pivot element

2. Pivot element(Central element)

3. Elements greater than the pivot element


Pivot element can be any element from the array, it can be the first element,
the last element or any random element. In this tutorial, we will take the
rightmost element or the last element as pivot.
Quick Sort Algorithm
The steps involved in quick sort (for sorting a given array in ascending order):
1. After selecting an element as pivot, which is the last index of the array in
our case, we divide the array for the first time.
2. In quick sort, we call this partitioning. It is not simple breaking down of
array into 2 subarrays, but in case of partitioning, the array elements are
so positioned that all the elements smaller than the pivot will be on the
left side of the pivot and all the elements greater than the pivot will be on
the right side of it.
3. And the pivot element will be at its final sorted position.
4. The elements to the left and right, may not be sorted.
5. Then we pick subarrays, elements on the left of pivot and elements on
the right of pivot, and we perform partitioning on them by choosing a
pivot in the subarrays.
Quick Sort Algorithm
// quick sort
/*
a[] is the array, p is starting index, that is 0,
and r is the last index of array.
*/
void quicksort(int a[], int p, int r)
{
if(p < r)
{
int q;
q = partition(a, p, r);
quicksort(a, p, q);
quicksort(a, q+1, r);
}
}
Quick Sort Algorithm
// partioning function
int partition (int a[], int low, int high)
{
int pivot = arr[high]; // selecting last element as pivot
int i = (low - 1); // index of smaller element

for (int j = low; j <= high- 1; j++)


{
// If current element is smaller than or equal to pivot
if (arr[j] <= pivot)
{
i++; // increment index of smaller element
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
Quick Sort Algorithm
23 8 12 4 22 7 15 PIVOT

8 12 4 7 15 23 22

7 22
4 8 12 23

23
4 8 12

4 7 8 12 15 22 23
Quick Sort Algorithm
Complexity
● Worst Case Time Complexity [ Big-O ]: O(n*n)
● Best Case Time Complexity [Big-omega]: O(n*log n)
● Average Time Complexity [Big-theta]: O(n*log n)
● Space Complexity: O(n*log n)

Note
● Space required by quick sort is very less, only O(n*log n) additional space is required.

● Quick sort is not a stable sorting technique, so it might change the occurence of two similar elements in the

list while sorting.


Shell Sort Algorithm
Algorithm
● Start
● Step 1 − Initialize the value of h (gap size)
● Step 2 − Divide the list into smaller sub-list of equal interval h
● Step 3 − Sort these sub-lists using insertion sort
● Step 4 − Repeat until complete list is sorted
● Stop.

Complexity
● Worst Case Time Complexity [ Big-O ]: O(n*n)
● Best Case Time Complexity [Big-omega]: O(n*log n)
● Average Time Complexity [Big-theta]: O(n*log n2)
● Space Complexity: O(1)
Note:
- it be useful for a large dataset
- isn’t stable
Shell Sort Algorithm
Example: h=3

Original List: {5, 43, 22, 64, 55, 78, 76, 14, 32}

Sublists: {5, 55, 32} {43, 78} {22, 76} {64, 14}
Insertion sort (sublists): {5, 32, 55} {43, 78} {22, 76} {14, 64}

Merge 1: {5, 32, 43, 55, 78} {14, 22, 64, 76}

Merge 2: {5, 14, 22, 32, 43, 55, 64, 76, 78}
Counting Sort Algorithm
Counting Sort Algorithm is an efficient sorting algorithm that can be used for sorting
elements within a specific range. This sorting technique is based on the
frequency/count of each element to be sorted and works using the following algorithm-
Input: Unsorted array A[] of n elements
Output: Sorted arrayB[]
The steps involved in counting sort
1: Consider an input array A having n elements in the range of 0 to k, where n and k are
positive integer numbers. These n elements have to be sorted in ascending order using
the counting sort technique. Also note that A[] can have distinct or duplicate elements
2: The count/frequency of each distinct element in A is computed and stored in another
array, say count, of size k+1. Let u be an element in A such that its frequency is stored
at count[u].
Counting Sort Algorithm
3: Update the count array so that element at each index, say i, is equal to -

count[i]=sum(count[u]; where 0<=u<=i

4: The updated count array gives the index of each element of array A in the
sorted sequence. Assume that the sorted sequence is stored in an output array, say
B, of size n.

5: Add each element from input array A to B as follows:


a.Set i=0 and t = A[i]
b. Add t to B[v] such that v = (count[t]-1).
c. Decrement count[t] by 1
d. Increment i by 1
Repeat steps (a) to (d) till i = n-1

Time Complexity: O(n)


Auxiliary space: O(n)
Counting Sort Algorithm
Example:

A = {5, 4, 6, 8, 1, 2, 8, 8, 5, 5, 2, 4,1}
C = {0, 2, 2, 0, 2, 3, 1, 0, 3}

ind 1 2 3 4 5 6 7 8
ex

C 2 2 0 2 3 1 0 3

C’ 2 4 4 6 9 10 10 13

Vectorul sortat B

1 2 3 4 5 6 7 8 9 10 11 12 13
B 1 1 2 2 4 4 5 5 5 6 8 8 8
Radix and bucket Sort Algorithm
Radix Sort- for an array of integer
Bucket sort - for the float elements between 0 and 1

The steps involved in Radix sort (for sorting a given array in ascending order):
1. we fill in with 0 in front to have the same number of digits for each element
2. the given array is ordered according to the digit of units
3. the resulting array is ordered according to the digit of tens
4. the resulting array is ordered according to the digit of hundreds
5. and so on ….

For the bucket sort, a similar procedure is used.


Note: considering that the numbers are sub-units, they are first ordered by the first
digit after the comma, then by the second, etc.,
Radix Sort Algorithm
Radix sort - example
Given array: 77, 55, 65, 44, 304, 22, 100, 13
Step 1: 077, 055, 065, 044, 304, 022, 100, 013
Step 2: 100, 022, 013, 044, 304, 055, 065, 077
Step 3: 100, 304, 013, 022, 044, 055, 065, 077
Step 4: 013, 022, 044, 055, 065, 077, 100, 304
Bucket Sort Algorithm
Bucket sort - steps
bucketSort(arr[], n)
0 0.01 0.005
1) Create n empty buckets (Or lists).
2) Do following for every array element arr[i]. 1 0.111
.......a) Insert arr[i] into bucket[n*array[i]] 2 /
3) Sort individual buckets using insertion sort. 3 0.325
4) Concatenate all sorted buckets. 4 /
5 /
Bucket sort example: 6 /
Given array: 0.78, 0.325, 0.01, 0.005, 0.111, 0.709
7 0.780 0.709
Step 1: 0.780, 0.325, 0.010, 0.005, 0.111, 0.709
Step 2: 0.010, 0.005, 0.111, 0.325, 0.780, 0.709 8 /

Step 3: 0.005, 0.010, 0.111, 0.325, 0.709, 0.780 9 /

You might also like