0% found this document useful (0 votes)
3 views15 pages

Week 1

algo week lab

Uploaded by

Jdjdd Dkdldldk
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)
3 views15 pages

Week 1

algo week lab

Uploaded by

Jdjdd Dkdldldk
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/ 15

#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[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) {

if (low < high) {

int pi = partition(arr, low, high);

quickSort(arr, low, pi - 1);


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

int partition(vector<int>& arr, int low, int 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]);


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;
}

// Main function to demonstrate sorting and comparison counting


int main() {
vector<int> arr = {64, 34, 25, 12, 22, 11, 90};
vector<int> temp;

cout << "Original Array: ";


printArray(arr);
// Selection Sort
temp = arr;
int selComp = selectionSort(temp, 'a');
cout << "\nSelection Sort Comparisons: " << selComp << "\nSorted Array: ";
printArray(temp);

// 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);

// Quick Sort (Pivot as first index)


temp = arr;
int quickComp = quickSort(temp, 0, temp.size() - 1, 'a', 1);
cout << "\nQuick Sort Comparisons (Pivot as first): " << quickComp << "\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;
}

void printArray(const vector<int>& arr) {


for (int num : arr) {
cout << num << " ";
}
cout << endl;
}

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;
}

int partition(vector<int>& arr, int low, int high, int& comparisons) {


int pivot = arr[high];
int i = low - 1;
for (int j = low; j <= high - 1; ++j) {
comparisons++;
if (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) {
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 merge(vector<int>& arr, int left, int mid, int right) {


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 (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;
}

// Utility function to print an array


void printArray(const vector<int>& arr) {
for (int num : arr) {
cout << num << " ";
}
cout << endl;
}

// Main function to demonstrate sorting and comparison counting


int main() {
vector<int> arr = {11, 12, 22, 25, 34, 64, 90}; // Sorted array from part A
vector<int> temp;

cout << "Sorted Array: ";


printArray(arr);

// 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;
}

// Utility function to print an array


void printArray(const vector<int>& arr) {
for (int num : arr) {
cout << num << " ";
}
cout << endl;
}

// Main function to demonstrate sorting and comparison counting


int main() {
vector<int> arr = {11, 12, 22, 25, 34, 64, 90}; // Sorted array from part A
vector<int> temp;

cout << "Sorted Array: ";


printArray(arr);

// 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;
}

// Utility function to print an array


void printArray(const vector<int>& arr) {
for (int num : arr) {
cout << num << " ";
}
cout << endl;
}

// Main function to demonstrate sorting and comparison counting


int main() {
vector<int> arr = {11, 12, 22, 25, 34, 64, 90}; // Sorted array from part A
vector<int> temp;

cout << "Sorted Array (Ascending): ";


printArray(arr);

cout << "\nReversing Order (Descending):" << endl;

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

// Utility function to swap elements


void swap(int& a, int& b) {
int temp = a;
a = b;
b = temp;
}

// Partition function for pivot as the first element


int partitionFirst(vector<int>& arr, int low, int high, int& comparisons) {
int pivot = arr[low];
int i = low + 1;

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


comparisons++;
if (arr[j] < pivot) {
swap(arr[i], arr[j]);
i++;
}
}
swap(arr[low], arr[i - 1]);
return i - 1;
}

// Quick Sort with pivot as the first element


void quickSortFirst(vector<int>& arr, int low, int high, int& comparisons) {
if (low < high) {
int pi = partitionFirst(arr, low, high, comparisons);
quickSortFirst(arr, low, pi - 1, comparisons);
quickSortFirst(arr, pi + 1, high, comparisons);
}
}

// Partition function for pivot as the last element


int partitionLast(vector<int>& arr, int low, int high, int& comparisons) {
int pivot = arr[high];
int i = low - 1;

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


comparisons++;
if (arr[j] < pivot) {
i++;
swap(arr[i], arr[j]);
}
}
swap(arr[i + 1], arr[high]);
return i + 1;
}

// Quick Sort with pivot as the last element


void quickSortLast(vector<int>& arr, int low, int high, int& comparisons) {
if (low < high) {
int pi = partitionLast(arr, low, high, comparisons);
quickSortLast(arr, low, pi - 1, comparisons);
quickSortLast(arr, pi + 1, high, comparisons);
}
}

// Partition function for pivot as the middle element


int partitionMiddle(vector<int>& arr, int low, int high, int& comparisons) {
int mid = low + (high - low) / 2;
swap(arr[mid], arr[low]); // Swap middle element with the first for consistency
return partitionFirst(arr, low, high, comparisons);
}

// Quick Sort with pivot as the middle element


void quickSortMiddle(vector<int>& arr, int low, int high, int& comparisons) {
if (low < high) {
int pi = partitionMiddle(arr, low, high, comparisons);
quickSortMiddle(arr, low, pi - 1, comparisons);
quickSortMiddle(arr, pi + 1, high, comparisons);
}
}

// Utility function to print an array


void printArray(const vector<int>& arr) {
for (int num : arr) {
cout << num << " ";
}
cout << endl;
}

// Main function to demonstrate overloaded quick sort functions


int main() {
vector<int> arr = {64, 34, 25, 12, 22, 11, 90};
vector<int> temp;
int comparisons;

cout << "Original Array: ";


printArray(arr);

// Quick Sort with pivot as the first element


temp = arr;
comparisons = 0;
quickSortFirst(temp, 0, temp.size() - 1, comparisons);
cout << "\nQuick Sort (Pivot: First Element): ";
printArray(temp);
cout << "Comparisons: " << comparisons << endl;

// Quick Sort with pivot as the last element


temp = arr;
comparisons = 0;
quickSortLast(temp, 0, temp.size() - 1, comparisons);
cout << "\nQuick Sort (Pivot: Last Element): ";
printArray(temp);
cout << "Comparisons: " << comparisons << endl;

// Quick Sort with pivot as the middle element


temp = arr;
comparisons = 0;
quickSortMiddle(temp, 0, temp.size() - 1, comparisons);
cout << "\nQuick Sort (Pivot: Middle Element): ";
printArray(temp);
cout << "Comparisons: " << comparisons << endl;
return 0;
}

ANS 6)

ANS 7)
ANS 8)

ANS 9)

ANS 10)

ANS 11)
#include <iostream>
#include <vector>

using namespace std;

vector<int> sorter(const vector<int>& V) {


int n = V.size();
vector<int> Arr;
int a = 0;
int b = n / 2;

while (a < n / 2 && b < n) {


if (V[a] < V[b]) {
Arr.push_back(V[a]);
a++;
} else {
Arr.push_back(V[b]);
b++;
}
}
while (a < n / 2) {
Arr.push_back(V[a]);
a++;
}
while (b < n) {
Arr.push_back(V[b]);
b++;
}
return Arr;
}

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>

using namespace std;

vector<int> sorter(const vector<int>& V) {


vector<int> even;
vector<int> odd;

for (size_t i = 0; i < V.size(); i++) {


if (i % 2 == 0) {
even.push_back(V[i]);
} else {
odd.push_back(V[i]);
}
}

sort(even.begin(), even.end());
sort(odd.begin(), odd.end(), greater<int>());

vector<int> result = even;


result.insert(result.end(), odd.begin(), odd.end());

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;
}

You might also like