0% found this document useful (0 votes)
27 views7 pages

Aoa Lab

The document contains code implementations for several sorting algorithms: - Bubble sort compares adjacent elements and swaps them if they are in the wrong order until the array is fully sorted. - Selection sort finds the minimum element in the unsorted part of the array and swaps it with the first element until the array is fully sorted. - Insertion sort takes each array element and inserts it into the sorted left part of the array until the array is fully sorted. - Quick sort selects a pivot element and partitions the array into sub-arrays based on element values relative to the pivot. It recursively sorts the sub-arrays. - Merge sort divides the array into single elements, recursively merges sub-arrays into sorted sub-

Uploaded by

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

Aoa Lab

The document contains code implementations for several sorting algorithms: - Bubble sort compares adjacent elements and swaps them if they are in the wrong order until the array is fully sorted. - Selection sort finds the minimum element in the unsorted part of the array and swaps it with the first element until the array is fully sorted. - Insertion sort takes each array element and inserts it into the sorted left part of the array until the array is fully sorted. - Quick sort selects a pivot element and partitions the array into sub-arrays based on element values relative to the pivot. It recursively sorts the sub-arrays. - Merge sort divides the array into single elements, recursively merges sub-arrays into sorted sub-

Uploaded by

anshumanasr02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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