0% found this document useful (0 votes)
164 views11 pages

Cole G. Botin

This document contains C++ code that implements various sorting algorithms including bubble sort, selection sort, and heap sort. It defines functions for inputting data, performing the different sorting techniques, converting data between 1D and 2D arrays, and displaying output. The main function allows the user to select an algorithm and provides a menu to continue running additional sorts or exit the program.

Uploaded by

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

Cole G. Botin

This document contains C++ code that implements various sorting algorithms including bubble sort, selection sort, and heap sort. It defines functions for inputting data, performing the different sorting techniques, converting data between 1D and 2D arrays, and displaying output. The main function allows the user to select an algorithm and provides a menu to continue running additional sorts or exit the program.

Uploaded by

Marvin Cinco
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

#include <iostream>

using namespace std;

#include <bits/stdc++.h>

//global

int i;

int row,col;

const int sizeRow=4;

const int sizeCol=5;

//function

void tryAgain();//for try again statement

char getMenu(char ans);//returns the correct choice otherwise returns -1 if not.

void getInput(int list[],int dim);//input for both option 1 and 2

void getBubble(int list[],int dim);//perform the bubble sorting

void getSelect(int list[],int dim);//perform the selection sorting

void convert2DimSort(int list[],int dim);//convert one dimensional to 2-dimensional for both option 1
and 2

void get2DOut(int list1[][sizeCol]);//displaying the values in ascending and descending order 2-D

void getHeap(int list3[],int dimen);//process heapyfication or heap sort 1-D

void heapifyAlgo(int arr1[], int n1, int i1);//for heapifying arr and helper function for heap sort

int main()

char sagot;

do

char choice='y';
sagot=getMenu(choice);

switch(sagot)

case '1'://bubblesort

cout<<"Processing Bubble Sorting Technique..."<<endl;

cout<<"enter 20elements "<<endl;

int dim=20;

int list[20];

//call getInput function

getInput(list,dim);

//call getBubble function

getBubble(list,dim);

//call convert2DimSort

convert2DimSort(list,dim);

break;

case '2':

cout<<"Processing Selection Sorting Technique..."<<endl;

cout<<"enter 20elements "<<endl;

int dim=20;

int list[20];

//call getInput function


getInput(list,dim);

//call getSelect function

getSelect(list,dim);

//call convert2DimSort

convert2DimSort(list,dim);

break;

case '3'://heapsort

cout<<"Processing Selection Sorting Technique..."<<endl;

cout<<"Enter 10 numbers "<<endl;

int size=10;

int arr[10];

for(int i=0;i<10;i++)

cout<<"Enter Value of the list["<<i+1<<"] :";

int s;

cin>>s;

arr[i]=s;

getHeap(arr,size);

cout<<endl<<"Heapyfied values ..."<<endl;

cout<<endl;

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

cout<<"Arr["<<i+1<<"] :"<<arr[i]<<endl;

break;

//exit code

case '4': cout<<"end of program.....\n";

exit(1);

char c;

//take y or n character to continue program

while(1)

tryAgain();

cin>>c;

if(c=='y'||c=='n')

break;

if(c=='n'){

cout<<"leaving the program now..."<<endl;

break;

} while(1);

}
/////////////////////////////////

//menu function

char getMenu(char ans)

char ch;

if(ans=='y')

cout<<"----------SORTING----------"<<endl;

cout<<"[1]bubble sort"<<endl;

cout<<"[2]selection sort"<<endl;

cout<<"[3]heap sort"<<endl;

cout<<"[4]QUIT "<<endl;

cout<<"---------------------------"<<endl;

cout<<"Enter your choice:";

cin>>ch;

if(ch=='1'||ch=='3'||ch=='2'||ch=='4')

return ch;

else

return -1;

////////////////////////////////////

//get input function


void getInput(int list[],int dim)// for both option 1 and 2 only

for(int i=0;i<20;i++)

//take user input

cout<<"loc"<<"["<<i<<"]"<<":";

int a;

cin>>a;

list[i]=a;

//////////////////////////////////////

//2d array conversion

void convert2DimSort(int list[],int dim)

int inList[sizeRow][sizeCol];

int t=0;

//inserted data into 2d from one day array

for(int i=0;i<sizeRow;i++)

for(int j=0;j<sizeCol;j++)

inList[i][j]=list[t];

//cout<<list[t]<<" ";

t++;

}
}

//call get 2d function

get2DOut(inList);

////////////////////////////////////////////////

//get2d out function

void get2DOut(int list1[][sizeCol])

cout<<"Ascending Order\n";

for(int i=0;i<sizeRow;i++)

{ //start from index 0 for row and column

for(int j=0;j<sizeCol;j++)

//print output

cout<<right<<setw(5)<<list1[i][j]<<" ";

cout<<endl;

cout<<"\nDescending Order\n";

for(int i=sizeRow-1;i>=0;i--)

{ //start from last index for row and column

for(int j=sizeCol-1;j>=0;j--)

//print data on the screen


cout<<right<<setw(5)<<list1[i][j]<<" ";

cout<<endl;

////////////////////////////////////////////

//perform bubble sort

void getBubble(int list[],int dim)

//solution for bubble sort

int i, j;

for (i = 0; i < dim-1; i++)

// Last i elements , in place

for (j = 0; j < dim-i-1; j++)

if (list[j] > list[j+1])

swap(list[j], list[j+1]);

///////////////////////////////////////////////

//selection sort perform


void getSelect(int list[],int dim)

//solution to selection sort

int i, j, min_idx1;

// move boundary unsorted array

for (i = 0; i < dim-1; i++)

// Find minimum element in array

min_idx1 = i;

for (j = i+1; j < dim; j++)

if (list[j] < list[min_idx1])

min_idx1 = j;

// Swap

swap(list[min_idx1], list[i]);

//////////////////////////////////////////////

//heapifyAlgo for getHeap

void heapifyAlgo(int arr1[], int n1, int i1)

int largest1 = i1; // largest is root

int l1 = 2*i1 + 1; // left child 2*i1 + 1

int r1 = 2*i1 + 2; // right child 2*i1 + 2

// If left child grater than root


if (l1 < n1 && arr1[l1] > arr1[largest1])

largest1 = l1;

// If right child is grater than largest

if (r1 < n1 && arr1[r1] > arr1[largest1])

largest1 = r1;

// If largest not root

if (largest1 != i1)

swap(arr1[i1], arr1[largest1]);

// Recursively heapify

heapifyAlgo(arr1, n1, largest1);

//getHeap function

void getHeap(int list3[],int dimen)

// Build heap with given data

for (int i = dimen / 2 - 1; i >= 0; i--)

heapifyAlgo(list3, dimen, i);

// extract element from heap

for (int i=dimen-1; i>=0; i--)

//swap element
swap(list3[0], list3[i]);

//call heapify

heapifyAlgo(list3, i, 0);

///////////////////////////////////////////////

//tryAgain function

void tryAgain()

cout<<"Do you want to try again[y/n]:";

You might also like