Professional Documents
Culture Documents
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.
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
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 -
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.
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 ….