0% found this document useful (0 votes)
17 views11 pages

Sorting Algorithms in C++ Code Examples

Uploaded by

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

Sorting Algorithms in C++ Code Examples

Uploaded by

mrudulshah24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Lab - 10

Problem No.1:- Quick Sort 4


CODE:-
#include <iostream>
#include <vector>

using namespace std;

void quickSort(vector<int>& arr, int low, int high) {


if (low < high) {
int pivot = arr[high];
int i = low - 1;

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


if (arr[j] <= pivot) {
i++;
swap(arr[i], arr[j]);
}
}

swap(arr[i + 1], arr[high]);


int pi = i + 1;

quickSort(arr, low, pi - 1);


quickSort(arr, pi + 1, high);
}
}

int main() {
int n;
cin >> n;

vector<int> arr(n);
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
quickSort(arr, 0, n - 1);

for (int i = 0; i < n; i++) {


cout << arr[i] << " ";
}

return 0;
}

CPP CHECK:-

TEST CASES:-
CUSTOM TEST CASES:-

Problem No.2 :- Bipartite Graph 4


CODE:-
#include <iostream>
#include <vector>
#include <queue>

using namespace std;

bool is_bipartite(vector<vector<int>>& graph, int n) {


vector<int> color(n, -1);
queue<int> q;

for (int i = 0; i < n; i++) {


if (color[i] == -1) {
color[i] = 0;
q.push(i);

while (!q.empty()) {
int u = q.front();
q.pop();

for (int v = 0; v < n; v++) {


if (graph[u][v] == 1 && color[v] == -1) {
color[v] = 1 - color[u];
q.push(v);
} else if (graph[u][v] == 1 && color[v] == color[u]) {
return false;
}
}
}
}
}

return true;
}

int main() {
int n;
cin >> n;

vector<vector<int>> graph(n, vector<int>(n));


for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> graph[i][j];
}
}

if (is_bipartite(graph, n)) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}

return 0;
}

CPP CHECK:-
TEST CASES:-

CUSTOM TEST CASES:-


Problem No.3:- Merge Sort 2
CODE:-
#include <bits/stdc++.h>
using namespace std;

void merge(vector<int> &arr, int low, int mid, int high) {


vector<int> temp;
int left = low;
int right = mid + 1;

while (left <= mid && right <= high) {


if (arr[left] <= arr[right]) {
temp.push_back(arr[left]);
left++;
}
else {
temp.push_back(arr[right]);
right++;
}
}

while (left <= mid) {


temp.push_back(arr[left]);
left++;
}

while (right <= high) {


temp.push_back(arr[right]);
right++;
}

for (int i = low; i <= high; i++) {


arr[i] = temp[i - low];
}
}

void mergeSort(vector<int> &arr, int low, int high) {


if (low >= high) return;
int mid = (low + high) / 2 ;
mergeSort(arr, low, mid);
mergeSort(arr, mid + 1, high);
merge(arr, low, mid, high);
}

int main() {
int n,m;
cin >> n;
vector<int> arr;
for(int i=0;i<n;i++){
cin >> m;
arr.push_back(m);
}
mergeSort(arr, 0, n - 1);
for (int i = 0; i < n; i++) {
cout << arr[i] << " " ;
}
cout << endl;
return 0 ;
}

CPP CHECK:-
TEST CASES:-

CUSTOM TEST CASES:-


Problem No.4 :- Heap Sort 3
CODE:-
#include <iostream>
#include <vector>

using namespace std;

void heapify(vector<int>& arr, int n, int i) {


int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;

if (left < n && arr[left] > arr[largest]) {


largest = left;
}

if (right < n && arr[right] > arr[largest]) {


largest = right;
}

if (largest != i) {
swap(arr[i], arr[largest]);
heapify(arr, n, largest);
}
}

void build_max_heap(vector<int>& arr, int n) {


for (int i = n / 2 - 1; i >= 0; i--) {
heapify(arr, n, i);
}
}

void heap_sort(vector<int>& arr, int n) {


build_max_heap(arr, n);
for (int i = n - 1; i >= 0; i--) {
swap(arr[0], arr[i]);
heapify(arr, i, 0);
}
}

int main() {
int n;
cin >> n;

vector<int> arr(n);
for (int i = 0; i < n; i++) {
cin >> arr[i];
}

heap_sort(arr, n);

for (int num : arr) {


cout << num << " ";
}

return 0;
}

CPP CHECK:-

TEST CASES:-
CUSTOM TEST CASES:-

You might also like