Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Aoa Lab

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Bubble sort

#include <iostream>

void bubbleSort(int arr[], int n) {


for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j+1]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);

std::cout << "Original array: ";


for (int i = 0; i < n; i++) {
std::cout << arr[i] << " ";
}

bubbleSort(arr, n);

std::cout << "\nSorted array: ";


for (int i = 0; i < n; i++) {
std::cout << arr[i] << " ";
}

return 0;
}

 Selection Sort
#include <iostream>

void selectionSort(int arr[], int n) {


for (int i = 0; i < n - 1; i++) {
// Find the minimum element in the unsorted part of the array
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}

// Swap the found minimum element with the first element


int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}

int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);

std::cout << "Original array: ";


for (int i = 0; i < n; i++) {
std::cout << arr[i] << " ";
}

selectionSort(arr, n);

std::cout << "\nSorted array: ";


for (int i = 0; i < n; i++) {
std::cout << arr[i] << " ";
}

return 0;
}

 Insertion Sort
#include <iostream>

void insertionSort(int arr[], int n) {


for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;

// Move elements of arr[0..i-1] that are greater than key


// to one position ahead of their current position
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}

arr[j + 1] = key;
}
}

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

insertionSort(arr, n);

std::cout << "\nSorted array: ";


for (int i = 0; i < n; i++) {
std::cout << arr[i] << " ";
}

return 0;
}

 Quick Sort
#include <iostream>

// Function to partition the array and return the pivot index


int partition(int arr[], int low, int high) {
int pivot = arr[high]; // Choose the rightmost element as the pivot
int i = (low - 1); // Initialize the index of the smaller element

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


// If the current element is smaller than or equal to the pivot
if (arr[j] <= pivot) {
i++; // Increment index of the smaller element
std::swap(arr[i], arr[j]);
}
}

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


return (i + 1);
}

// Function to perform Quick Sort


void quickSort(int arr[], int low, int high) {
if (low < high) {
// Partition the array into two sub-arrays and get the pivot index
int pi = partition(arr, low, high);

// Recursively sort the elements before and after the pivot


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

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

std::cout << "\nSorted array: ";


for (int i = 0; i < n; i++) {
std::cout << arr[i] << " ";
}

return 0;
}

 Merge Sort
#include <iostream>
#include <vector>

// Merge two subarrays of arr[]


void merge(std::vector<int>& arr, int left, int middle, int right) {
int n1 = middle - left + 1;
int n2 = right - middle;

std::vector<int> leftArray(n1);
std::vector<int> rightArray(n2);

// Copy data to temp arrays leftArray[] and rightArray[]


for (int i = 0; i < n1; i++) {
leftArray[i] = arr[left + i];
}
for (int i = 0; i < n2; i++) {
rightArray[i] = arr[middle + 1 + i];
}

// Merge the temp arrays back into arr[left..right]


int i = 0, j = 0, k = left;

while (i < n1 && j < n2) {


if (leftArray[i] <= rightArray[j]) {
arr[k] = leftArray[i];
i++;
} else {
arr[k] = rightArray[j];
j++;
}
k++;
}

// Copy the remaining elements of leftArray[], if any


while (i < n1) {
arr[k] = leftArray[i];
i++;
k++;
}

// Copy the remaining elements of rightArray[], if any


while (j < n2) {
arr[k] = rightArray[j];
j++;
k++;
}
}

// Merge Sort function


void mergeSort(std::vector<int>& arr, int left, int right) {
if (left < right) {
// Same as (left+right)/2, but avoids overflow for large left and right
int middle = left + (right - left) / 2;

// Sort first and second halves


mergeSort(arr, left, middle);
mergeSort(arr, middle + 1, right);

// Merge the sorted halves


merge(arr, left, middle, right);
}
}

int main() {
std::vector<int> arr = {12, 11, 13, 5, 6};
int n = arr.size();

std::cout << "Original array: ";


for (int i = 0; i < n; i++) {
std::cout << arr[i] << " ";
}

mergeSort(arr, 0, n - 1);

std::cout << "\nSorted array: ";


for (int i = 0; i < n; i++) {
std::cout << arr[i] << " ";
}

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

Graph::Graph(int vertices) : vertices(vertices) {


adjacencyList.resize(vertices);
}

void Graph::addEdge(int from, int to) {


adjacencyList[from].push_back(to);
}

void Graph::topologicalSortUtil(int v, std::vector<bool>& visited,


std::stack<int>& stack) {
visited[v] = true;

for (const int& neighbor : adjacencyList[v]) {


if (!visited[neighbor]) {
topologicalSortUtil(neighbor, visited, stack);
}
}

stack.push(v);
}

void Graph::topologicalSort() {
std::stack<int> stack;
std::vector<bool> visited(vertices, false);

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


if (!visited[i]) {
topologicalSortUtil(i, visited, stack);
}
}

std::cout << "Topological Sort: ";


while (!stack.empty()) {
std::cout << stack.top() << " ";
stack.pop();
}
std::cout << std::endl;
}

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

You might also like