0% found this document useful (0 votes)
41 views2 pages

Templates For DSA11

The document describes two algorithms: 1) Merge Sort, which sorts an array by dividing it into halves, sorting each half recursively, and merging the sorted halves. 2) Quicksort, which selects a pivot element and partitions the array into two halves based on element values relative to the pivot, and recursively sorts the two halves. It also describes using disjoint sets with union by rank and path compression to keep track of connected components in a graph, achieving time complexity of O(logV) for find and union operations on V vertices.

Uploaded by

Sourav Malik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views2 pages

Templates For DSA11

The document describes two algorithms: 1) Merge Sort, which sorts an array by dividing it into halves, sorting each half recursively, and merging the sorted halves. 2) Quicksort, which selects a pivot element and partitions the array into two halves based on element values relative to the pivot, and recursively sorts the two halves. It also describes using disjoint sets with union by rank and path compression to keep track of connected components in a graph, achieving time complexity of O(logV) for find and union operations on V vertices.

Uploaded by

Sourav Malik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Merge Sort:: Self code fully YEAH!!!!!!......

class Solution
{
public:

void merge(int arr[], int l, int m, int r)


{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;

/* create temp arrays */


int L[n1], R[n2];

/* Copy data to temp arrays L[] and R[] */


for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j =
//Picking the pivot.
int pivot = arr[high];
//Index of smaller element and indicates the right position of
//pivot found so far.
int i = low;
for (int j = low; j <= high- 1; j++)
{
//If current element is smaller than or equal to pivot we increment
//the value of i and swap the values at ith and jth index.
if (arr[j] <= pivot)
{
swap(arr[i], arr[j]);
i++;
}
}
//At last, swapping of value at ith and the last index which was
//selected as pivot.
swap(arr[i], arr[high]);
//returning the partitioning index.
return i;
}

//Function to sort an array using quick sort algorithm.


void quickSort(int arr[], int low, int high)
{
if (low < high)
{
//pi is partitioning index, arr[pi] is now at right place.
int pi = partition(arr, low, high);
//Separately sorting elements before and after partitioning index.
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
};

//{ Driver Code Starts.


int main()
{
int arr[1000],n,T,i;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
Solution ob;
ob.quickSort(arr, 0, n-1);
printArray(arr, n);
}
return 0;
}

-------------------------------------------------------------------------

Disjoint Set using rank and path compression-- T.c Vlog(V). Log(v) for finding
absolute parent for all V vertices.

class Solution {
public:
vector<int> parent;
vector<int> rank;
int find(int u ) {
if (u == parent[u])
return parent[u];

return parent[u] = find(parent[u]);


}

void union1(int x, int y) {


int root1 = find(x);
int root2 = find(y);
if (root1 == root2) return ;
if (rank[root2] >= rank[root1]) {
parent[root1] = root2;
rank[root2]++;
}
else {
parent[root2] = root1;
rank[root1]++;
}
}

string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {


int n = s.length();
parent.assign(n, 0);
rank.assign(n, 0);
for (int i = 0; i < n; i++)
parent[i] = i;
}
};

------------------------------------

You might also like