0% found this document useful (0 votes)
43 views

Templates For DSA9

The document contains code snippets for three different sorting algorithms: Merge Sort, Quick Sort, and Disjoint Set. Merge Sort code shows how to merge two sorted arrays back into the original array. Quick Sort code uses partitioning to recursively sort an array by picking a pivot element and placing all smaller elements before and larger elements after it. Disjoint Set code uses union by rank and path compression to efficiently perform union operations on sets with a time complexity of O(logV).

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)
43 views

Templates For DSA9

The document contains code snippets for three different sorting algorithms: Merge Sort, Quick Sort, and Disjoint Set. Merge Sort code shows how to merge two sorted arrays back into the original array. Quick Sort code uses partitioning to recursively sort an array by picking a pivot element and placing all smaller elements before and larger elements after it. Disjoint Set code uses union by rank and path compression to efficiently perform union operations on sets with a time complexity of O(logV).

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/ 4

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

/* Merge the temp arrays back into arr[l..r]*/


i = 0; // Initial index of first subarray
j = 0; // Initial index of second subarray
k = l; // Initial index of merged subarray
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}

/* Copy the remaining elements of L[], if there


are any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}

/* Copy the remaining elements of R[], if there


are any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}

public:
void mergeSort(int arr[], int l, int r)
{
if (l < r)
{
// Same as (l+r)/2, but avoids overflow for
// large l and h
int m = l+(r-l)/2;

// Sort first and second halves


mergeSort(arr, l, m);
mergeSort(arr, m+1, r);

merge(arr, l, m, r);
}
}
};

-----------------------------------
Quick Sort::

#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;

/* Function to print an array */


void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}

// } Driver Code Ends


class Solution
{
public:
//Function that takes last element as pivot, places the pivot element at
//its correct position in sorted array, and places all smaller elements
//to left of pivot and all greater elements to right of pivot.
int partition (int arr[], int low, int high)
{
//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