Aoa Lab
Aoa Lab
Aoa Lab
#include <iostream>
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
return 0;
}
Selection Sort
#include <iostream>
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
selectionSort(arr, n);
return 0;
}
Insertion Sort
#include <iostream>
arr[j + 1] = key;
}
}
int main() {
int arr[] = {12, 11, 13, 5, 6};
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
return 0;
}
Quick Sort
#include <iostream>
int main() {
int arr[] = {12, 11, 13, 5, 6};
int n = sizeof(arr) / sizeof(arr[0]);
std::cout << "Original array: ";
for (int i = 0; i < n; i++) {
std::cout << arr[i] << " ";
}
quickSort(arr, 0, n - 1);
return 0;
}
Merge Sort
#include <iostream>
#include <vector>
std::vector<int> leftArray(n1);
std::vector<int> rightArray(n2);
int main() {
std::vector<int> arr = {12, 11, 13, 5, 6};
int n = arr.size();
mergeSort(arr, 0, n - 1);
return 0;
}
Topological Sort
#include <iostream>
#include <vector>
#include <stack>
class Graph {
public:
Graph(int vertices);
void addEdge(int from, int to);
void topologicalSort();
private:
int vertices;
std::vector<std::vector<int>> adjacencyList;
void topologicalSortUtil(int v, std::vector<bool>& visited, std::stack<int>&
stack);
};
stack.push(v);
}
void Graph::topologicalSort() {
std::stack<int> stack;
std::vector<bool> visited(vertices, false);
int main() {
Graph g(6);
g.addEdge(5, 2);
g.addEdge(5, 0);
g.addEdge(4, 0);
g.addEdge(4, 1);
g.addEdge(2, 3);
g.addEdge(3, 1);
g.topologicalSort();
return 0;
}