#Include: STD Selectionsort A N I J Min Temp I I N I Min I J I J N J A J A Min Min J Temp A I A I A Min A Min Temp
#Include: STD Selectionsort A N I J Min Temp I I N I Min I J I J N J A J A Min Min J Temp A I A I A Min A Min Temp
#Include: STD Selectionsort A N I J Min Temp I I N I Min I J I J N J A J A Min Min J Temp A I A I A Min A Min Temp
as numerical, alphabetical etc. There are many algorithms to sort a numerical array such
as bubble sort, insertion sort, selection sort, merge sort, quick sort, heap sort etc. More
details about sorting the array using selection sort are given below.
The selection sort is a sorting method that yields a sorted array. It does so by repeatedly
finding the smallest element in the array and interchanging it with the element at the
starting of the unsorted part.
A program that implements a sorted array using selection sort is given as follows
#include<iostream>
min = i;
min = j;
temp = a[i];
a[i] = a[min];
a[min] = temp;
int main() {
int i;
cout<<endl;
selectionSort(a, n);
return 0;
output
Given array is:
22 91 35 78 10 8 75 99 1 67
Sorted array is:
1 8 10 22 35 67 75 78 91 99
In the above program, selectionSort() is a function that sorts the array a[] using selection
sort. There are two for loops in selectionSort(). In each iteration of the outer for loop, the
minimum element in the remaining array after i is found and then interchanged with the
element currently at i. This is repeated until the array is sorted. This is shown below.
min = i;
min = j;
temp = a[i];
a[i] = a[min];
a[min] = temp;
In the main() function, the array a[] is defined. Then the function selectionSort() is called
with the array a[] and its size n. Finally, the sorted array is displayed. This is shown
below.
int main() {
int a[] = { 22, 91, 35, 78, 10, 8, 75, 99, 1, 67 };
int i;
cout<<endl;
selectionSort(a, n);
return 0;
#include<iostream.h>
#include<conio.h>
void main()
{
int i,a[10],temp,j;
clrscr();
cout<<"Enter any 10 num in array: \n";
for(i=0;i<=10;i++)
{
cin>>a[i];
}
cout<<"\nData before sorting: ";
for(j=0;j<10;j++)
{
cout<<a[j];
}
for(i=0;i<=10;i++)
{
for(j=0;j<=10-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
cout<<"\nData after sorting: ";
for(j=0;j<10;j++)
{
cout<<a[j];
}
getch();
}
Output
2 5 1 7 5 3 8 9 11 4
For Sort Elements of Array in Descending Order we print all Elements of array from
Example
#include<iostream.h>
#include<conio.h>
void main()
{
int i,a[10],temp,j;
clrscr();
cout<<"Enter any 10 num in array : \n";
for(i=0;i<=10;i++)
{
cin>>a[i];
}
cout<<"\n\nData before sorting: ";
for(j=0;j<10;j++)
{
cout<<a[j];
}
for(i=0;i<=10;i++)
{
for(j=0;j<=10-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
cout<<"\nData after sorting: ";
for(j=9;j>=0 ;j--)
{
cout<<a[j];
}
getch();
}
Download Code
Output
2 5 1 7 5 3 8 9 11 4
int main()
{
int search(int [],int,int);
int n,i,a[100],e,res;
cout<<"How Many Elements:";
cin>>n;
cout<<"\nEnter Elements of Array in Ascending order\n";
for(i=0;i<n;++i)
{
cin>>a[i];
}
res=search(a,n,e);
if(res!=-1)
cout<<"\nElement found at position "<<res+1;
else
cout<<"\nElement is not found....!!!";
return 0;
}
while(f<=l)
{
m=(f+l)/2;
if(e==a[m])
return(m);
else
if(e>a[m])
f=m+1;
else
l=m-1;
}
return -1;
}
Output
How Many Elements:5
Enter Elements of Array in Ascending order
12 39 40 68 77
Enter element to search:40
Element found at position 3
}
else if(arr[middle] == search)
{
cout<<search<<" found at location
"<<middle+1<<"\n";
break;
}
else
{
last = middle - 1;
}
middle = (first + last)/2;
}
if(first > last)
{
cout<<"Not found! "<<search<<" is not present in the
list.";
}
getch();
}
When the above C++ program is compile and executed, it will produce the
following result. Above C++ Programming Example Output (Element found):
#include<iostream>
int main()
{
int a[50],n,i,j,temp;
cout<<"Enter the size of array: ";
cin>>n;
cout<<"Enter the array elements: ";
for(i=0;i<n;++i)
cin>>a[i];
for(i=1;i<n;++i)
{
for(j=0;j<(n-i);++j)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
return 0;
}
Output
int main()
{
int i,j,n,loc,temp,min,a[30];
cout<<"Enter the number of elements:";
cin>>n;
cout<<"\nEnter the elements\n";
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<n-1;i++)
{
min=a[i];
loc=i;
for(j=i+1;j<n;j++)
{
if(min>a[j])
{
min=a[j];
loc=j;
}
}
temp=a[i];
a[i]=a[loc];
a[loc]=temp;
}
return 0;
}