To Sort Array Of Integers Using Bubble,Insertion And Selection Sort














































To Sort Array Of Integers Using Bubble,Insertion And Selection Sort



Description:
Bubble Sort: 
It is an algorithm that works by simply swapping adjacent elements if they are in the wrong order.
Insertion Sort:
Insertion Sort is the sorting mechanism where the sorted array is built having one item at a time. The array elements are compared with each other sequentially and then arranged.
Selection Sort:
Selection Sort sorts an array by repeatedly finding the smallest element.
Program Code: 
#include<iostream>
using namespace std;
void Create(int A[],int n)
{
for (int i=0;i<n;i++)
cin>>A[i];
}
void BBsort(int A[],int n)
{
for (int r=0;r<n;r++)
{
for (int s=0;s<n-r-1;s++)
if(A[s]>A[s+1])
{
int t=A[s];
A[s]=A[s+
1];
A[s+
1]=t;
}
}
for (int k=0;k<n;k++)
cout<<A[k]<<",";
}
void ISort(int A[],int n)
{
for (int i=1;i<n;i++)
{
int T=A[i],j=i-1;
while(T<A[j]&&j>=0)
{
A[j+
1]=A[j];
j--;
}
A[j+
1]=T;
}
for (int k=0;k<n;k++)
cout<<A[k]<<",";
}
void SSort(int A[],int n)
{ for (int i=1;i<n-1;i++)
{
int small=i;
int j;
for ( j=i+1;j<n;j++)
if(A[small]>A[j])
small=j;
if (small!=i)
{
int t=A[small];
A[small]=A[j];
A[j]=t;
}
}
for (int k=0;k<n;k++)
cout<<A[k]<<",";
}
int main()
{ int A[5] ;
Create(A,
5) ;
cout<<endl;
BBsort(A,
5);
cout<<endl;
ISort(A,
5);
cout<<endl;
SSort(A,
5);
cout<<endl;
return 0;
}

Output:



More Articles of Avikrit Kohli:

Name Views Likes
C++ Program To Insert At Bottom Of Stack Using Recursion 1529 1
Python Program To Extract Data From Excel 822 1
C++ Program To Reverse Line In Text File 4594 1
C++ Program To Implement Simpsons 3/8th rule 1376 2
C++ Program To Read Text File Word By Word 4554 1
Program To Display Level Order Transversal Using Queue 643 1
C++ Program To Find Length Of Linked List 2798 1
C++ Program To Search An Element Recursively In Linked List 1332 1
C++ Program To Delete Element In Array At Particular Position 1595 1
C++ Program To Find The Minimum In Subarray Of Size k Using Deque 761 2
To Find Negation of Number Using Operator Overloading 1041 1
C++ Program To Reverse Linked List Recursively 3104 1
C++ Program To Sort Two Arrays And Inserting Result In Third array 1366 1
To Sort Array Of Integers Using Bubble,Insertion And Selection Sort 1673 1
C++ Program To Implement Stack Using Arrays 2919 1
C++ Program To Balance Parenthesis Using Stack 2211 1
Python Program To Add Source Code of A Webpage 755 1
C++ Program To Insert Element In Array At Given Position 2297 1
C++ Program To Reverse Each Word In Text File 1243 1
C++ Program to Swap two numbers without using third variable 765 2
C++ Program To Reverse String Without Using String Functions 1391 20
C++ Program To Reverse Stack Recursively 1630 1
C++ Program To Find Sum Of Fibonacci Series Using Recursion 3993 1
C++ Program To Create And Display Text File 1080 1
C++ Program To Find Root Using Bisection Method 6384 1
C++ Program To Implement Queue Using Array 4919 2
C++ Program To Sort Stack Recursively 2063 1

Comments