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

C++ Array Sorting Algorithms

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)
5 views3 pages

C++ Array Sorting Algorithms

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

#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<[Link]()-1; i++)
{
for(int j=0;j<[Link]()-i-1; j++)
{
if([Link](j)>[Link](j+1))
{
int t = [Link][j];
[Link][j]=[Link][j+1];
[Link][j+1]=t;
}
}
}
cout<<"\n Elements after sorting are : ";
for (int i=0; i<[Link](); i++)
cout<<[Link][i];
}

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

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

};

int main()
{
Sort s;
[Link]();
[Link]();
[Link](s);
[Link](s);
[Link](s);
return 0;
}

You might also like