Advanced Data Structure & Algorithm_2_1724990251486
Advanced Data Structure & Algorithm_2_1724990251486
Sorting Terminology:
In-place Sorting: An in-place sorting algorithm uses constant space for producing the output
(modifies the given array only) or copying elements to a temporary storage. Examples:
Selection Sort, Bubble Sort Insertion Sort and Heap Sort.
Internal Sorting: Internal Sorting is when all the data is placed in the main
memory or internal memory. In internal sorting, the problem cannot take input beyond its
size. Example: heap sort, bubble sort, selection sort, quick sort, shell sort, insertion sort.
External Sorting : External Sorting is when all the data that needs to be sorted cannot be
placed in memory at a time, the sorting is called external sorting. External Sorting is used for
the massive amount of data. Examples: Merge sort, Tag sort, Polyphase sort, Four tape sort,
External radix sort, etc.
Stable sorting: When two same items appear in the same order in sorted data as in the
original array called stable sort. Examples: Merge Sort, Insertion Sort, Bubble Sort.
Unstable sorting: When two same data appear in the different order in sorted data it is
called unstable sort. Examples: Selection Sort, Quick Sort, Heap Sort, Shell Sort.
Time Complexity: Time complexity, a measure of how long it takes to run an algorithm, is
used to categorize sorting algorithms. The worst-case, average-case, and best-case
performance of a sorting algorithm can be used to quantify the time complexity of the
process.
Auxiliary Space : This is the amount of extra space (apart from input array) needed to sort.
For example, Merge Sort requires O(n) and Insertion Sort O(1) auxiliary space
Stability: A sorting algorithm is said to be stable if the relative order of equal elements is
preserved after sorting. This is important in certain applications where the original order of
equal elements must be maintained.
In-Place Sorting: An in-place sorting algorithm is one that does not require additional
memory to sort the data. This is important when the available memory is limited or when
the data cannot be moved.
Adaptivity: An adaptive sorting algorithm is one that takes advantage of pre-existing order in
the data to improve performance. For example insertion sort takes time proportional
to number of inversions in the input array.
Data management: Sorting data makes it easier to search, retrieve, and analyze. For example
the order by operation in SQL queries requires sorting.
Machine learning: Sorting is used to prepare data for training machine learning models.
Data Analysis: Sorting helps in identifying patterns, trends, and outliers in datasets. It plays a
vital role in statistical analysis, financial modeling, and other data-driven fields.
Operating Systems: Sorting algorithms are used in operating systems for tasks like task
scheduling, memory management, and file system organization
Merge Sort:
else
#include <iostream>
{
using namespace std;
arr[k]=b[j]
void mergeSort(int arr[],int l, int r)
k++;
{
j++;
if (l<r)
}
{
}
int mid=(l+r)/2;
while(i<n1)
mergeSort(arr,l,mid);
{
mergeSort(arr,mid+1,r);
arr[k]=a[i];
merge(arr, l,mid,r);
k++;
}
i++;
}
}
void merge(int arr[],int l,int mid,int r)
while(j<n2)
{
{
int n1=mid-l+1;
arr[k]=b[j];
int n2=r-mid;
k++;
int a[n1],b[n2];
j++;
for(int i=0;i< n1;i++)
}
{
}
a[i]=arr[l+i]; int main()
} {
for(int i=0;i< n2;i++) int arr[]={5,4,3,2,1};
{ // int length=sizeof(arr);
b[i]=arr[mid+1+i]; mergeSort(arr,0,4);
} for(int i=0;i<5;i++)
int i=0;j=0,k=l; {
while(i<n1 && j< n2) cout<< arr[i]<< " ";
{ }
if (a[i]< b[j]) cout<< endl;
{
return 0;
arr[k]=a[i];
}
k++;
i++;
}
Quick Sort:
{ int main()
if (left < right) {
{ int n;
int pivotIndex = partition(arr, left, right); cout<<"Enter the size of array n: \n";
quickSort(arr, left, pivotIndex-1); cin>>n;
quickSort(arr, pivotIndex+1, right); int arr[n];
} cout<<"Enter the elements of the array: \n";
} for(int i = 0;i < n;i++){
int partition(int arr[], int left, int right) cin>>arr[i];
{ }
int pivot = arr[right]; //object of the class//
int l = left - 1; QuickSort qSort;
for (int r = left; r < right; r++) qSort.sort(arr, n);
{ for(int i = 0;i < n;i++){
if (arr[r] < pivot) cout<<arr[i]<<" ";
{ } return 0;
l++; }
swap(arr[l], arr[r]);
}
Quicksort complexity
1. Time Complexity
o Best Case Complexity - In Quicksort, the best-case occurs when the pivot element is the
middle element or near to the middle element. The best-case time complexity of quicksort
is O(n*logn).
o Average Case Complexity - It occurs when the array elements are in jumbled order that is
not properly ascending and not properly descending. The average case time complexity of
quicksort is O(n*logn).
o Worst Case Complexity - In quick sort, worst case occurs when the pivot element is either
greatest or smallest element. Suppose, if the pivot element is always the last element of the
array, the worst case would occur when the given array is sorted already in ascending or
descending order. The worst-case time complexity of quicksort is O(n2).
Though the worst-case complexity of quicksort is more than other sorting algorithms such as Merge
sort and Heap sort,
2. Space Complexity
Radix Sort:
• Radix sort is the linear sorting algorithm that is used for integers.
• In Radix sort, there is digit by digit sorting is performed that is started from the least
significant digit to the most significant digit.
where d is the no. of digits, n is the total no. of elements and k is the base of the number system.
It is a stable sort.
#include <iostream>
// value in arr[]
int mx = arr[0];
mx = arr[i];
return mx;
// represented by exp.
// Output array
int output[n];
int i, count[10] = { 0 };
// in count[]
arr[i] = output[i];
countSort(arr, n, exp);
// Driver Code
int main()
// Function Call
radixsort(arr, n);
print(arr, n);
return 0;
Bucket Sort:
Bucket sort is a sorting algorithm that separate the elements into multiple groups said to be buckets.
Elements in bucket sort are first uniformly divided into groups called buckets, and then they are
sorted by any other sorting algorithm. After that, elements are gathered in a sorted manner.
o It is not an in-place sorting algorithm, because some extra space is required to sort the
buckets.
The best and average-case complexity of bucket sort is O(n + k), and the worst-case complexity of
bucket sort is O(n2), where n is the number of items.
1. bucketSort(a[], n)
4. Put array elements into buckets, i.e. insert a[i] into bucket[n*a[i]]
7. End bucketSort
Algorithm
1. Bucket Sort(A[])
3. n=length[A]
6. for i=1 to n
11. End
Let's take an unsorted array to understand the process of bucket sort. It will be easier to understand
the bucket sort via an example.
Now, create buckets with a range from 0 to 25. The buckets range are 0-5, 5-10, 10-15, 15-20, 20-25.
Elements are inserted in the buckets according to the bucket range. Suppose the value of an item is
16, so it will be inserted in the bucket with the range 15-20. Similarly, every item of the array will
insert accordingly.
#include <iostream>
int getMax(int a[], int n) // function to get maximum element from the given array
max = a[i];
return max;
{
int max = getMax(a, n); //max is the maximum element of array
int bucket[max], i;
bucket[i] = 0;
bucket[a[i]]++;
a[j++] = i;
bucket[i]--;
cout<<a[i]<<" ";
int main()
printArr(a, n);
bucket(a, n);
cout<<"\nAfter sorting array elements are - \n";
printArr(a, n);
Output
bucket sort
using System;
class Bucket {
static int getMax(int[] a) // function to get maximum element from the given array
int n = a.Length;
max = a[i];
return max;
int n = a.Length;
bucket[i] = 0;
bucket[a[i]]++;
a[j++] = i;
bucket[i]--;
int i;
int n = a.Length;
printArr(a);
bucket(a);
printArr(a);