0% found this document useful (0 votes)
16 views5 pages

Codes For A Lot of Things

The document contains code implementations for various sorting algorithms including Bubble Sort, Insertion Sort, Merge Sort, and Quick Sort, as well as data structures like Stack and Queue. Each algorithm and data structure is accompanied by example code in C++. The document provides a comprehensive overview of these fundamental programming concepts.

Uploaded by

aditya271005
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)
16 views5 pages

Codes For A Lot of Things

The document contains code implementations for various sorting algorithms including Bubble Sort, Insertion Sort, Merge Sort, and Quick Sort, as well as data structures like Stack and Queue. Each algorithm and data structure is accompanied by example code in C++. The document provides a comprehensive overview of these fundamental programming concepts.

Uploaded by

aditya271005
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/ 5

Codes for a lot of things:

(1) Bubblesort
#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;
}
}
}
}
(2) 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 = j - 1;
}
arr[j + 1] = key;
}
}
(3) Merge sort
#include <iostream>

void merge(int arr[], int l, int m, int r) {


int n1 = m - l + 1;
int n2 = r - m;

// Create temporary arrays


int L[n1], R[n2];

// Copy data to temporary arrays L[] and R[]


for (int i = 0; i < n1; ++i)
L[i] = arr[l + i];
for (int j = 0; j < n2; ++j)
R[j] = arr[m + 1 + j];

// Merge the temporary arrays back into arr[l..r]


int i = 0, j = 0, k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
++i;
} else {
arr[k] = R[j];
++j;
}
++k;
}

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


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

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


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

void mergeSort(int arr[], int l, int r) {


if (l < r) {
int m = l + (r - l) / 2;

// Sort first and second halves


mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);

// Merge the sorted halves


merge(arr, l, m, r);
}
}

int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
mergeSort(arr, 0, n - 1);
cout << "Sorted array is: ";
for (int i = 0; i < n; ++i) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
(4) Quicksort
#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 last element as the pivot
int i = low - 1; // Index of the smaller element

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


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

// Function to implement Quick Sort


void quickSort(int arr[], int low, int high) {
if (low < high) {
// pi is the partitioning index, arr[pi] is now at the correct position
int pi = partition(arr, low, high);

// Separately sort elements before and after partition


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

int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
cout << "Sorted array is: ";
for (int i = 0; i < n; ++i) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
(5) Stacks #LIFO
#include <iostream>
using namespace std;
// Define the maximum size of the stack
const int MAX_SIZE = 4;

class Stack {
private:
int arr[MAX_SIZE];
int top;

public:
// Constructor
Stack() {
top = -1;
}

// Function to check if the stack is empty


bool isEmpty() {
return (top == -1);
}

// Function to check if the stack is full


bool isFull() {
return (top == MAX_SIZE - 1);
}

// Function to push an element onto the stack


void push(int element) {
if (isFull()) {
cout << "Stack Overflow\n";
return;
}
arr[++top] = element;
}

// Function to pop an element from the stack


int pop() {
if (isEmpty()) {
cout << "Stack Underflow\n";
return -1;
}
return arr[top--];
}
};

int main() {
Stack myStack;
int numElements;

cout << "Enter the number of elements you want to push into the stack: ";
cin >> numElements;

int element;
for (int i = 0; i < numElements; ++i) {
cout << "Enter element " << i + 1 << ": ";
cin >> element;
myStack.push(element);
}

cout << "Elements pushed into the stack: ";


while (!myStack.isEmpty()) {
cout << myStack.pop() << " ";
}

cout << endl;

return 0;
}
(6) Queue
#include <iostream>
using namespace std;
// Define the maximum size of the queue
const int MAX_SIZE = 100;

class Queue {
private:
int arr[MAX_SIZE];
int front, rear;

public:
// Constructor
Queue() {
front = -1;
rear = -1;
}

// Function to check if the queue is empty


bool isEmpty() {
return (front == -1 && rear == -1);
}

// Function to check if the queue is full


bool isFull() {
return (rear == MAX_SIZE - 1);
}

// Function to add an element to the rear of the queue


void enqueue(int element) {
if (isFull()) {
cout << "Queue Overflow\n";
return;
}
if (isEmpty()) {
front = 0;
}
arr[++rear] = element;
}

// Function to remove an element from the front of the queue


int dequeue() {
if (isEmpty()) {
cout << "Queue Underflow\n";
return -1;
}
int removedElement = arr[front];
if (front == rear) {
front = rear = -1;
} else {
++front;
}
return removedElement;
}
};

int main() {
Queue myQueue;
int numElements;

cout << "Enter the number of elements you want to enqueue into the queue: ";
cin >> numElements;

int element;
for (int i = 0; i < numElements; ++i) {
cout << "Enter element " << i + 1 << ": ";
cin >> element;
myQueue.enqueue(element);
}

cout << "Elements enqueued into the queue: ";


while (!myQueue.isEmpty()) {
cout << myQueue.dequeue() << " ";
}

cout << endl;

return 0;
}

You might also like