Nothing Special   »   [go: up one dir, main page]

#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

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

A sorted array is an array in which each of the elements are sorted in some order such

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>

using namespace std;

void selectionSort(int a[], int n) {

int i, j, min, temp;

for (i = 0; i < n - 1; i++) {

min = i;

for (j = i + 1; j < n; j++)

if (a[j] < a[min])

min = j;

temp = a[i];

a[i] = a[min];

a[min] = temp;

int main() {

int a[] = { 22, 91, 35, 78, 10, 8, 75, 99, 1, 67 };

int n = sizeof(a)/ sizeof(a[0]);

int i;

cout<<"Given array is:"<<endl;

for (i = 0; i < n; i++)

cout<< a[i] <<" ";

cout<<endl;
selectionSort(a, n);

printf("\nSorted array is: \n");

for (i = 0; i < n; i++)

cout<< a[i] <<" ";

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.

void selectionSort(int a[], int n) {

int i, j, min, temp;

for (i = 0; i < n - 1; i++) {

min = i;

for (j = i + 1; j < n; j++)

if (a[j] < a[min])

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 n = sizeof(a)/ sizeof(a[0]);

int i;

cout<<"Given array is:"<<endl;

for (i = 0; i < n; i++)

cout<< a[i] <<" ";

cout<<endl;

selectionSort(a, n);

printf("\nSorted array is: \n");

for (i = 0; i < n; i++)

cout<< a[i] <<" ";

return 0;

C++ Program to Sort Elements of Array in Ascending Order

#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

Enter any 10 num in array:

2 5 1 7 5 3 8 9 11 4

Data After Sorting: 1 2 3 4 5 7 8 9 11

C++ Program to Sort an Array Elements in Descending Order.

For Sort Elements of Array in Descending Order we print all Elements of array from

last index to first. For example arr[10], arr[9], arr[8],....arr[0]

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

Enter any 10 num in array:

2 5 1 7 5 3 8 9 11 4

Data After Sorting: 11 9 8 7 5 4 3 2 1


Binary Search in C++
Here you will learn about binary search in C++.
Binary search is an algorithm used to search for an element in a sorted
array. In this algorithm the targeted element is compared with middle
element. If both elements are equal then position of middle element is
returned and hence targeted element is found.
If both elements are unequal then if targeted element is less or more than
middle element we discard the lower or upper half and the search continues
by finding new middle element.

Program for Binary Search in C++


#include<iostream>

using namespace std;

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];
}

cout<<"\nEnter element to search:";


cin>>e;

res=search(a,n,e);

if(res!=-1)
cout<<"\nElement found at position "<<res+1;
else
cout<<"\nElement is not found....!!!";

return 0;
}

int search(int a[],int n,int e)


{
int f,l,m;
f=0;
l=n-1;

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

/* C++ Program - Binary Search */


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n, i, arr[50], search, first, last, middle;
cout<<"Enter total number of elements :";
cin>>n;
cout<<"Enter "<<n<<" number :";
for (i=0; i<n; i++)
{
cin>>arr[i];
}
cout<<"Enter a number to find :";
cin>>search;
first = 0;
last = n-1;
middle = (first+last)/2;
while (first <= last)
{
if(arr[middle] < search)
{
first = middle + 1;

}
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):

Above C++ Programming Example Output (Element Not found):

Bubble sort is a sorting technique in which each pair of adjacent elements


are compared, if they are in wrong order we swap them. This algorithm is
named as bubble sort because, same as like bubbles the smaller or lighter
elements comes up (at start) and bigger or heavier elements goes down (at
end). Below I have shared a program for bubble sort in C++ which sorts a
list of numbers in ascending order.

Program for Bubble Sort in C++

#include<iostream>

using namespace std;

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;
}
}

cout<<"Array after bubble sort:";


for(i=0;i<n;++i)
cout<<" "<<a[i];

return 0;
}

Output

Program for Selection Sort in C++


#include<iostream>

using namespace std;

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;
}

cout<<"\nSorted list is as follows\n";


for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}

return 0;
}

You might also like