// All Stack Operations 1.Push 2.Pop 3.Peek 4.FullStack View
#include <stdio.h>
int isEmpty(int top)
{
if (top == -1)
{
return 1;
}
else
{
return 0;
}
}
void peek(int stack[], int top)
{
if (!isEmpty(top))
{
printf("\nTop Element in Stack: %d", stack[top]);
}
else
{
printf("Stack is Empty ");
}
}
void seeStack(int stack[], int top)
{
int i;
if (!isEmpty(top))
{
printf("Stack Elements Are: \n");
for (i = top; i >= 0; i--)
{
printf("%d -> %d\n", top - (top - i) + 1, stack[i]);
}
}
else
{
printf("Stack is Empty");
}
}
int isFull(int top)
{
if (top == 4)
{
return 1;
}
else
{
return 0;
}
}
void push(int *stack, int *top)
{
int v;
if (!isFull(*top))
{
printf("Enter Value: ");
scanf("%d", &v);
*top = *top + 1;
stack[*top] = v;
printf("\nPushed Element = %d ", v);
}
else
{
printf("\nStack is Full ");
}
}
void pop(int *stack, int *top)
{
int v;
if (!isEmpty(*top))
{
v = stack[*top];
printf("\nPoped Element = %d ", v);
*top = *top - 1;
}
else
{
printf("\nStack is Empty Already ");
}
}
int main()
{
int stack[5], top = -1, c, t = 1;
while (t == 1)
{
printf("\nEnter Your Choice:\n1.Push\n2.Pop\n3.Peek\n4.See FullStack\n5.Quit\nChoice: ");
scanf("%d", &c);
switch (c)
{
case 1:
{
push(stack, &top);
break;
}
case 2:
{
pop(stack, &top);
break;
}
case 3:
{
peek(stack, top);
break;
}
case 4:
{
seeStack(stack, top);
break;
}
case 5:
{
t = 2;
break;
}
}
}
return 0;
}
<to do>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
// Insertion sort for small subarrays
template <typename T>
void insertionSort(std::vector<T>& arr, int left, int right) {
for (int i = left + 1; i <= right; ++i) {
T key = arr[i];
int j = i - 1;
while (j >= left && arr[j] > key) {
arr[j + 1] = arr[j];
--j;
}
arr[j + 1] = key;
}
}
// Heapify a subtree rooted at index i
template <typename T>
void heapify(std::vector<T>& arr, int n, int i) {
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && arr[left] > arr[largest])
largest = left;
if (right < n && arr[right] > arr[largest])
largest = right;
if (largest != i) {
std::swap(arr[i], arr[largest]);
heapify(arr, n, largest);
}
}
// Heap sort
template <typename T>
void heapSort(std::vector<T>& arr) {
int n = arr.size();
// Build a max heap
for (int i = n / 2 - 1; i >= 0; --i)
heapify(arr, n, i);
// Extract elements from the heap one by one
for (int i = n - 1; i > 0; --i) {
std::swap(arr[0], arr[i]);
heapify(arr, i, 0);
}
}
// Partition function for quicksort
template <typename T>
int partition(std::vector<T>& arr, int low, int high) {
T pivot = arr[high];
int i = (low - 1);
for (int j = low; j <= high - 1; ++j) {
if (arr[j] < pivot) {
++i;
std::swap(arr[i], arr[j]);
}
}
std::swap(arr[i + 1], arr[high]);
return (i + 1);
}
// Quick sort
template <typename T>
void quickSort(std::vector<T>& arr, int low, int high, int depthLimit) {
if (low < high) {
if (depthLimit == 0) {
heapSort(arr);
return;
}
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1, depthLimit - 1);
quickSort(arr, pi + 1, high, depthLimit - 1);
}
}
// Introsort function
template <typename T>
void introSort(std::vector<T>& arr) {
int depthLimit = 2 * static_cast<int>(log2(arr.size()));
quickSort(arr, 0, arr.size() - 1, depthLimit);
insertionSort(arr, 0, arr.size() - 1);
}
int main() {
std::vector<int> data = {5, 2, 8, 1, 9, 3};
std::cout << "Original data: ";
for (int num : data) {
std::cout << num << " ";
}
std::cout << std::endl;
introSort(data);
std::cout << "Sorted data: ";
for (int num : data) {
std::cout << num << " ";
}
std::cout << std::endl;
return 0;
}