I want to create a menu like this
Menu List Type -
1 for Bubble Sort
2 for Insertion Sort
3 Selection Sort
4 Stack
5 Queues
heres the code of bubble sort
#include <iostream.h>
#include <stdlib.h>
const int MAXSIZE = 10;
void bubbleSort(int arr[], int size);
void swap(int& x, int& y);
int main()
{
int nums[] = { 1, 7, 5, 3, 15, 11, 13, 17, 21, 19 };
int k;
cout << "BEFORE SORT: ";
for (k = 0; k < MAXSIZE; k++)
cout << nums[k] << " ";
bubbleSort(nums, MAXSIZE);
cout << endl << endl;
cout << "AFTER SORT: ";
for (k = 0; k < MAXSIZE; k++)
cout << nums[k] << " ";
cout << endl << endl << endl;
system("PAUSE");
return 0;
}// end main()
void bubbleSort(int arr[], int size)
{
int last = size - 2;
int isChanged = 1;
while (last >= 0 && isChanged)
{
isChanged = 0;
for (int k = 0; k <= last; k++)
if (arr[k] < arr[k+1])
{
swap(arr[k], arr[k+1]);
isChanged = 1;
}
last--;
}
}// end bubbleSort()
void swap(int& x, int& y)
{
int temp;
temp = x;
x = y;
y = temp;
}// end swap()
Code for Insertion Sort
#include <iostream.h>
#include <stdlib.h>
void insertPosition(int arr[], int& size, int element, int position);
int main()
{
int nums[10] = { 34, 12, 89, 22, 21, 45, 55, 93, 27, 76 };
int size = 10;
int element;
int position;
for (int k = 0; k < size; k++)
cout << nums[k] << " ";
do
{
cout << endl << endl << "Enter the position in the array for insertion (0 - 9): ";
cin >> position;
} while (position < 0 || position > 9);
cout << "Enter the value to be inserted into position [" << position
<< "] of the array: ";
cin >> element;
insertPosition(nums, size, element, position);
cout << endl << endl;
for (int k = 0; k < size; k++)
cout << nums[k] << " ";
cout << endl << endl << endl;
system("PAUSE");
return 0;
}// end main()
void insertPosition(int arr[], int& size, int element, int position)
{
for (int k = size - 1; k >= position; k--)
arr[k+1] = arr[k];
arr[position] = element;
size++;
}// end insertPosition()
Code for Selection Sort
#include <iostream.h>
#include <stdlib.h>
const int MAXSIZE = 10;
void selectionSort(int arr[], int size);
void swap(int& x, int& y);
int main()
{
int numbers[] = { 13, 5, 1, 7, 9, 11, 3, 17, 19, 15 };
int k;
cout << "BEFORE SORT: ";
for (k = 0; k < MAXSIZE; k++)
cout << numbers[k] << " ";
selectionSort(numbers, MAXSIZE);
cout << endl << endl;
cout << "AFTER SORT: ";
for (k = 0; k < MAXSIZE; k++)
cout << numbers[k] << " ";
cout << endl << endl << endl;
system("PAUSE");
return 0;
}// end main()
void selectionSort(int arr[], int size)
{
int indexOfMin, pass, j;
for (pass = 0; pass < size - 1; pass++)
{
indexOfMin = pass;
for (j = pass + 1; j < size; j++)
if (arr[j] < arr[indexOfMin])
indexOfMin = j;
swap(arr[pass], arr[indexOfMin]);
}
}// end selectionSort()
void swap(int& x, int& y)
{
int temp;
temp = x;
x = y;
y = temp;
}// end swap()
Code for Stack
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> iL;
int k;
// load the stack with numbers 0 to 9, last number entered would be on top
for(k = 0; k < 10; k++)
iL.push_front(k);
// now unload the stack and display
cout << "Unloading the stack in order:\n";
for(k = 0; k < 10; k++)
{
// read the top element of the list
cout << iL.front() << endl;
// now pop it (removes top element)
iL.pop_front();
}
cin.get(); // wait
return 0;
}
Code for Queues
#include <iostream.h>
struct Node{
int data;
Node *prev;
Node *next;
};
class TQueue{
public:
TQueue(){
head = NULL;
currItem = NULL;
}
~TQueue(){
delete [] head;
}
void AddItem(int Data){
Node *prevItem = 0;
Node *newNode = new Node;
newNode->data = Data;
if(head == NULL){
head = newNode;
currItem = head;
head->next = NULL;
head->prev = NULL;
}
else{
currItem = prevItem = head;
while(currItem->next != NULL){
currItem = currItem->next;
}
currItem->next = newNode;
currItem = currItem->next;
while(prevItem->next != currItem){
prevItem = prevItem->next;
}
currItem->prev = prevItem;
}
}
int GetItem(){
if(!IsEmpty()){
if(currItem->prev != NULL){
currItem = currItem->prev;
return currItem->next->data;
}
else
return currItem->data;
}
else
return 0;
}
bool IsEmpty(){
return (head == NULL);
}
private:
Node *head;
Node *currItem;
};
int main(){
TQueue tester;
tester.AddItem(353);
tester.AddItem(53);
tester.AddItem(563);
cout << tester.GetItem() << endl;
cout << tester.GetItem() << endl;
cout << tester.GetItem() << endl;
cin.get();
return 0;
}
Sample If I press 1 then the bubble sort appears
then if I press 2 Insertion Sort appears thx
then 3 4 5