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> usingnamespacestd; voidCreate(int A[],int n) { for (int i=0;i<n;i++) cin>>A[i]; } voidBBsort(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]<<","; } voidISort(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]<<","; } voidSSort(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]<<","; } intmain() { int A[5] ; Create(A,5) ; cout<<endl; BBsort(A,5); cout<<endl; ISort(A,5); cout<<endl; SSort(A,5); cout<<endl; return0; }
Comments