Week 1
Week 1
h>
using namespace std;
// Selection Sort
int selectionSort(vector<int>& arr) {
int comparisons = 0;
int n = arr.size();
for (int i = 0; i < n - 1; ++i) {
int min_index = i;
for (int j = i + 1; j < n; ++j) {
comparisons++;
if (arr[j]<arr[min_index]) {
min_index = j;
}
}
swap(arr[i], arr[extremeIdx]);
}
return comparisons;
}
// Bubble Sort
int bubbleSort(vector<int>& arr) {
int comparisons = 0;
int n = arr.size();
for (int i = 0; i < n - 1; ++i) {
for (int j = 0; j < n - i - 1; ++j) {
comparisons++;
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
}
}
return comparisons;
}
// Insertion Sort
int insertionSort(vector<int>& arr, char order) {
int comparisons = 0;
int n = arr.size();
for (int i = 1; i < n; ++i) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
// Quick Sort
void quickSort(vector<int>& arr, int low, int high) {
// Merge Sort
int mergeSort(vector<int>& arr, int left, int right, char order) {
int comparisons = 0;
if (left < right) {
int mid = left + (right - left) / 2;
comparisons += mergeSort(arr, left, mid, order);
comparisons += mergeSort(arr, mid + 1, right, order);
comparisons += merge(arr, left, mid, right, order);
}
return comparisons;
}
int merge(vector<int>& arr, int left, int mid, int right, char order) {
int comparisons = 0;
int n1 = mid - left + 1;
int n2 = right - mid;
vector<int> L(n1), R(n2);
for (int i = 0; i < n1; ++i) L[i] = arr[left + i];
for (int j = 0; j < n2; ++j) R[j] = arr[mid + 1 + j];
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
comparisons++;
if ((order == 'a' && L[i] <= R[j]) || (order == 'd' && L[i] >= R[j])) {
arr[k++] = L[i++];
} else {
arr[k++] = R[j++];
}
}
while (i < n1) arr[k++] = L[i++];
while (j < n2) arr[k++] = R[j++];
return comparisons;
}
// Bubble Sort
temp = arr;
int bubComp = bubbleSort(temp, 'a');
cout << "\nBubble Sort Comparisons: " << bubComp << "\nSorted Array: ";
printArray(temp);
// Insertion Sort
temp = arr;
int insComp = insertionSort(temp, 'a');
cout << "\nInsertion Sort Comparisons: " << insComp << "\nSorted Array: ";
printArray(temp);
// Merge Sort
temp = arr;
int mergeComp = mergeSort(temp, 0, temp.size() - 1, 'a');
cout << "\nMerge Sort Comparisons: " << mergeComp << "\nSorted Array: ";
printArray(temp);
return 0;
}
ANS 2)
#include <bits/stdc++.h>
using namespace std;
// Selection Sort
int selectionSort(vector<int>& arr) {
int comparisons = 0;
int n = arr.size();
for (int i = 0; i < n - 1; ++i) {
int min_index = i;
for (int j = i + 1; j < n; ++j) {
comparisons++;
if (arr[j] < arr[min_index]) {
min_index = j;
}
}
swap(arr[i], arr[min_index]);
}
return comparisons;
}
// Bubble Sort
int bubbleSort(vector<int>& arr) {
int comparisons = 0;
int n = arr.size();
for (int i = 0; i < n - 1; ++i) {
for (int j = 0; j < n - i - 1; ++j) {
comparisons++;
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
}
}
}
return comparisons;
}
// Insertion Sort
int insertionSort(vector<int>& arr) {
int comparisons = 0;
int n = arr.size();
for (int i = 1; i < n; ++i) {
int key = arr[i];
int j = i - 1;
while (j >= 0) {
comparisons++;
if (arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
} else {
break;
}
}
arr[j + 1] = key;
}
return comparisons;
}
// Quick Sort
int quickSort(vector<int>& arr, int low, int high) {
static int comparisons = 0;
if (low < high) {
int pi = partition(arr, low, high, comparisons);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
return comparisons;
}
// Merge Sort
int mergeSort(vector<int>& arr, int left, int right) {
int comparisons = 0;
if (left < right) {
int mid = left + (right - left) / 2;
comparisons += mergeSort(arr, left, mid);
comparisons += mergeSort(arr, mid + 1, right);
comparisons += merge(arr, left, mid, right);
}
return comparisons;
}
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
comparisons++;
if (L[i] <= R[j]) {
arr[k++] = L[i++];
} else {
arr[k++] = R[j++];
}
}
while (i < n1) arr[k++] = L[i++];
while (j < n2) arr[k++] = R[j++];
return comparisons;
}
// Selection Sort
temp = arr;
int selComp = selectionSort(temp);
cout << "\nSelection Sort Comparisons: " << selComp << endl;
// Bubble Sort
temp = arr;
int bubComp = bubbleSort(temp);
cout << "Bubble Sort Comparisons: " << bubComp << endl;
// Insertion Sort
temp = arr;
int insComp = insertionSort(temp);
cout << "Insertion Sort Comparisons: " << insComp << endl;
// Quick Sort
temp = arr;
int quickComp = quickSort(temp, 0, temp.size() - 1);
cout << "Quick Sort Comparisons: " << quickComp << endl;
// Merge Sort
temp = arr;
int mergeComp = mergeSort(temp, 0, temp.size() - 1);
cout << "Merge Sort Comparisons: " << mergeComp << endl;
return 0;
}
ANS 3)
#include <bits/stdc++.h>
using namespace std;
// Selection Sort
int selectionSort(vector<int>& arr, char order) {
int comparisons = 0;
int n = arr.size();
for (int i = 0; i < n - 1; ++i) {
int extreme_index = i;
for (int j = i + 1; j < n; ++j) {
comparisons++;
if ((order == 'a' && arr[j] < arr[extreme_index]) ||
(order == 'd' && arr[j] > arr[extreme_index])) {
extreme_index = j;
}
}
swap(arr[i], arr[extreme_index]);
}
return comparisons;
}
// Bubble Sort
int bubbleSort(vector<int>& arr, char order) {
int comparisons = 0;
int n = arr.size();
for (int i = 0; i < n - 1; ++i) {
for (int j = 0; j < n - i - 1; ++j) {
comparisons++;
if ((order == 'a' && arr[j] > arr[j + 1]) ||
(order == 'd' && arr[j] < arr[j + 1])) {
swap(arr[j], arr[j + 1]);
}
}
}
return comparisons;
}
// Insertion Sort
int insertionSort(vector<int>& arr, char order) {
int comparisons = 0;
int n = arr.size();
for (int i = 1; i < n; ++i) {
int key = arr[i];
int j = i - 1;
while (j >= 0) {
comparisons++;
if ((order == 'a' && arr[j] > key) || (order == 'd' && arr[j] < key)) {
arr[j + 1] = arr[j];
j = j - 1;
} else {
break;
}
}
arr[j + 1] = key;
}
return comparisons;
}
// Quick Sort
int quickSort(vector<int>& arr, int low, int high, char order) {
static int comparisons = 0;
if (low < high) {
int pi = partition(arr, low, high, comparisons, order);
quickSort(arr, low, pi - 1, order);
quickSort(arr, pi + 1, high, order);
}
return comparisons;
}
int partition(vector<int>& arr, int low, int high, int& comparisons, char order) {
int pivot = arr[high];
int i = low - 1;
for (int j = low; j <= high - 1; ++j) {
comparisons++;
if ((order == 'a' && arr[j] < pivot) || (order == 'd' && arr[j] > pivot)) {
++i;
swap(arr[i], arr[j]);
}
}
swap(arr[i + 1], arr[high]);
return i + 1;
}
// Merge Sort
int mergeSort(vector<int>& arr, int left, int right, char order) {
int comparisons = 0;
if (left < right) {
int mid = left + (right - left) / 2;
comparisons += mergeSort(arr, left, mid, order);
comparisons += mergeSort(arr, mid + 1, right, order);
comparisons += merge(arr, left, mid, right, order);
}
return comparisons;
}
int merge(vector<int>& arr, int left, int mid, int right, char order) {
int comparisons = 0;
int n1 = mid - left + 1;
int n2 = right - mid;
vector<int> L(n1), R(n2);
for (int i = 0; i < n1; ++i) L[i] = arr[left + i];
for (int j = 0; j < n2; ++j) R[j] = arr[mid + 1 + j];
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
comparisons++;
if ((order == 'a' && L[i] <= R[j]) || (order == 'd' && L[i] >= R[j])) {
arr[k++] = L[i++];
} else {
arr[k++] = R[j++];
}
}
while (i < n1) arr[k++] = L[i++];
while (j < n2) arr[k++] = R[j++];
return comparisons;
}
// Selection Sort
temp = arr;
int selComp = selectionSort(temp, 'a');
cout << "\nSelection Sort Comparisons (Ascending): " << selComp << endl;
// Bubble Sort
temp = arr;
int bubComp = bubbleSort(temp, 'a');
cout << "Bubble Sort Comparisons (Ascending): " << bubComp << endl;
// Insertion Sort
temp = arr;
int insComp = insertionSort(temp, 'a');
cout << "Insertion Sort Comparisons (Ascending): " << insComp << endl;
// Quick Sort
temp = arr;
int quickComp = quickSort(temp, 0, temp.size() - 1, 'a');
cout << "Quick Sort Comparisons (Ascending): " << quickComp << endl;
// Merge Sort
temp = arr;
int mergeComp = mergeSort(temp, 0, temp.size() - 1, 'a');
cout << "Merge Sort Comparisons (Ascending): " << mergeComp << endl;
return 0;
}
ANS 4)
#include <bits/stdc++.h>
using namespace std;
// Selection Sort
int selectionSort(vector<int>& arr, char order) {
int comparisons = 0;
int n = arr.size();
for (int i = 0; i < n - 1; ++i) {
int extreme_index = i;
for (int j = i + 1; j < n; ++j) {
comparisons++;
if ((order == 'a' && arr[j] < arr[extreme_index]) ||
(order == 'd' && arr[j] > arr[extreme_index])) {
extreme_index = j;
}
}
swap(arr[i], arr[extreme_index]);
}
return comparisons;
}
// Bubble Sort
int bubbleSort(vector<int>& arr, char order) {
int comparisons = 0;
int n = arr.size();
for (int i = 0; i < n - 1; ++i) {
for (int j = 0; j < n - i - 1; ++j) {
comparisons++;
if ((order == 'a' && arr[j] > arr[j + 1]) ||
(order == 'd' && arr[j] < arr[j + 1])) {
swap(arr[j], arr[j + 1]);
}
}
}
return comparisons;
}
// Insertion Sort
int insertionSort(vector<int>& arr, char order) {
int comparisons = 0;
int n = arr.size();
for (int i = 1; i < n; ++i) {
int key = arr[i];
int j = i - 1;
while (j >= 0) {
comparisons++;
if ((order == 'a' && arr[j] > key) || (order == 'd' && arr[j] < key)) {
arr[j + 1] = arr[j];
j = j - 1;
} else {
break;
}
}
arr[j + 1] = key;
}
return comparisons;
}
// Quick Sort
int quickSort(vector<int>& arr, int low, int high, char order) {
static int comparisons = 0;
if (low < high) {
int pi = partition(arr, low, high, comparisons, order);
quickSort(arr, low, pi - 1, order);
quickSort(arr, pi + 1, high, order);
}
return comparisons;
}
int partition(vector<int>& arr, int low, int high, int& comparisons, char order) {
int pivot = arr[high];
int i = low - 1;
for (int j = low; j <= high - 1; ++j) {
comparisons++;
if ((order == 'a' && arr[j] < pivot) || (order == 'd' && arr[j] > pivot)) {
++i;
swap(arr[i], arr[j]);
}
}
swap(arr[i + 1], arr[high]);
return i + 1;
}
// Merge Sort
int mergeSort(vector<int>& arr, int left, int right, char order) {
int comparisons = 0;
if (left < right) {
int mid = left + (right - left) / 2;
comparisons += mergeSort(arr, left, mid, order);
comparisons += mergeSort(arr, mid + 1, right, order);
comparisons += merge(arr, left, mid, right, order);
}
return comparisons;
}
int merge(vector<int>& arr, int left, int mid, int right, char order) {
int comparisons = 0;
int n1 = mid - left + 1;
int n2 = right - mid;
vector<int> L(n1), R(n2);
for (int i = 0; i < n1; ++i) L[i] = arr[left + i];
for (int j = 0; j < n2; ++j) R[j] = arr[mid + 1 + j];
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
comparisons++;
if ((order == 'a' && L[i] <= R[j]) || (order == 'd' && L[i] >= R[j])) {
arr[k++] = L[i++];
} else {
arr[k++] = R[j++];
}
}
while (i < n1) arr[k++] = L[i++];
while (j < n2) arr[k++] = R[j++];
return comparisons;
}
// Selection Sort
temp = arr;
int selComp = selectionSort(temp, 'd');
cout << "Selection Sort Comparisons (Descending): " << selComp << endl;
// Bubble Sort
temp = arr;
int bubComp = bubbleSort(temp, 'd');
cout << "Bubble Sort Comparisons (Descending): " << bubComp << endl;
// Insertion Sort
temp = arr;
int insComp = insertionSort(temp, 'd');
cout << "Insertion Sort Comparisons (Descending): " << insComp << endl;
// Quick Sort
temp = arr;
int quickComp = quickSort(temp, 0, temp.size() - 1, 'd');
cout << "Quick Sort Comparisons (Descending): " << quickComp << endl;
// Merge Sort
temp = arr;
int mergeComp = mergeSort(temp, 0, temp.size() - 1, 'd');
cout << "Merge Sort Comparisons (Descending): " << mergeComp << endl;
return 0;
}
ans 5)
#include <bits/stdc++.h>
using namespace std;
ANS 6)
ANS 7)
ANS 8)
ANS 9)
ANS 10)
ANS 11)
#include <iostream>
#include <vector>
int main() {
vector<int> V = {2, 3, 8, -1, 7, 10};
vector<int> sortedV = sorter(V);
for (int v : sortedV) {
cout << v << " ";
}
cout << endl;
return 0;
}
ans 12)
#include <iostream>
#include <vector>
#include <algorithm>
sort(even.begin(), even.end());
sort(odd.begin(), odd.end(), greater<int>());
return result;
}
int main() {
vector<int> V = {0, 2, 1, 3, 4, 5, 6, 7};
vector<int> sortedV = sorter(V);
for (int v : sortedV) {
cout << v << " ";
}
cout << endl;
return 0;
}