0% found this document useful (0 votes)
32 views3 pages

Sortcode

The document describes three sorting algorithms: bubble sort, selection sort, and insertion sort. It defines a Sort class with methods to input array size, populate the array, and access elements. Methods are provided to implement each sorting algorithm on an array.

Uploaded by

shivani porwal
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)
32 views3 pages

Sortcode

The document describes three sorting algorithms: bubble sort, selection sort, and insertion sort. It defines a Sort class with methods to input array size, populate the array, and access elements. Methods are provided to implement each sorting algorithm on an array.

Uploaded by

shivani porwal
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/ 3

#include<iostream>

#include<math.h>
#define max 20
using namespace std;

class Sort
{
public:
int arr[max];
int size;

void inputsize()
{
cout<<"\n Emter size of array to be sorted";
cin>>size;
}

void inputarr()
{
cout<<"\n Enter element : ";
for(int i=0; i<size; i++)
{
cin>>arr[i];
if(IsMember(i,arr[i]))
i--;
}
}
bool IsMember(int curpos, int ele)
{

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


{
if(arr[i]==ele)
return true;

}return false;
}

int getsize()
{
return size;
}

int getelement(int i)
{
return arr[i];
}

void BubbleSort(Sort S)
{
for(int i=0; i<S.getsize()-1; i++)
{
for(int j=0;j<S.getsize()-i-1; j++)
{
if(S.getelement(j)>S.getelement(j+1))
{
int t = S.arr[j];
S.arr[j]=S.arr[j+1];
S.arr[j+1]=t;
}
}
}
cout<<"\n Elements after sorting are : ";
for (int i=0; i<S.getsize(); i++)
cout<<S.arr[i];
}

void SelectionSort(Sort S)
{
int small;
for(int i=0; i<S.getsize(); i++)
{
small= S.getelement(i);
int pos =i;
for(int j=(i+1); j<S.getsize(); j++)
{
if(small> S.getelement(j))
{
small= S.arr[j];
pos = j;
}
int t= S.arr[i];
S.arr[i]=S.arr[pos];
S.arr[pos]=t;
}
}
cout<<"\n Elements after sorting are : ";
for (int i=0; i<S.getsize(); i++)
cout<<S.arr[i];
}

void InsertionSort(Sort S)
{
int temp;
for(int i=0; i<S.getsize(); i++)
{
temp= S.getelement(i);
int j=i-1;
while((temp<S.getelement(j)) && (j>=0))
{
S.arr[j+1]=S.arr[j];
j= j-1;
}
S.arr[j+1]=temp;
}
cout<<"\n Elements after sorting are : ";
for (int i=0; i<S.getsize(); i++)
cout<<S.arr[i];
}

};

int main()
{
Sort s;
s.inputsize();
s.inputarr();
s.BubbleSort(s);
s.SelectionSort(s);
s.InsertionSort(s);
return 0;
}

You might also like