Sort Algorithms

You might also like

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

Sort Algorithms

Sort algorithms are ways to sort an array of unsorted numbers. There are different
algorithms as following.

1. Selection Sort
One of the simplest sorting algorithms works as follows: first find the smallestelement in the
array and exchange it with the element in the first position,then find the second smallest element
and exchange it with the element inthe second position, continuing in this way until the entire
array is sorted.
This method is called selection sort because it works by repeatedly “selecting”the smallest
remaining element. The following program sorts a [10] intonumerical order:

Input: An array A storing N items


Output: A sorted in ascending order
Algorithm Selection_Sort (A, N):

//Selection Sort
#include<iostream>
int main()
{ using namespace std;

inti, j, min, arr[10]={55,9,35,4,1,16,20,7,8,11};

for(i=0;i<10;i++)
{ min=arr[i];
for(j=i+1;j<10;j++)
if(min>arr[j]){
min=arr[j];
arr[j]=arr[i];
arr[i]=min;
}
}
for(i=0;i<10;i++)
cout<<arr[i]<<" ";
system("pause");
return 0;
}
The result is going to be the following
1, 4, 7, 8, 9, 11, 16, 20, 35, 55

We can sort the array up to down by finding the max number in the array

2. Bubble sort

Bubble sort is a sorting algorithm that works by repeatedly stepping through data
structure (array) that need to be sorted, comparing each pair of adjacent items and
swapping them if they are in the wrong order. This passing procedure is repeated until no
swaps are required, indicating that the array is sorted. Bubble sort gets its name because
smaller elements bubble toward the left of the array and bigger elements toward the right
of the array.

The algorithm:
Input: An array A storing N items
Output: A sorted in ascending order
Algorithm Bubble_Sort (A, N):
for(i=1;i<n-1;i++)
for(j=0;j<n-i; j++)
if(arr[j]>arr[j+1]) { temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}

The following program sorts a [10] into numerical order:

//Bubble sort
#include<iostream>
int main()
{using namespace std;

inti,n=10, j , temp, arr[10]={55,9,35,4,1,16,20,7,8,11};

for(i=1;i<n-1;i++)
for(j=0;j<n-i; j++)
if(arr[j]>arr[j+1]){
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}

for(i=0;i<10;i++)
cout<<arr[i]<<" ";

cout<<endl;
system("pause");
return 0;
}
Ex.1
Write a C++ program to generate integer values in array A[n] using Rand function
and then sort A using Selection Sort Algorithm.

#include<iostream>
#include<stdlib.h>
int main()
{ using namespace std;

int i, j, min,n;
cout<<"enter the number of elements in array A ";
cin>>n;
int a[n];

for(i=0;i<n;i++)
a[i]=rand() % 1000;
for(i=0;i<n;i++)
{ min=a[i];
for(j=i+1;j<n;j++)
if(min>a[j])
{ min=a[j];
a[j]=a[i];
a[i]=min; }
}
for(i=0;i<n;i++)
cout<<a[i]<<" ";
system("pause");
return 0;
}

Ex.2
Write a C++ program to generate integer values in array A[n] using Rand function
and then move all odd numbers to array b and even numbers to array c.
Furthermore, sort b using Selection Sort and c using Bubble Sort Algorithm.
#include<iostream>
#include<stdlib.h>
int main()
{ using namespace std;

int i, j, min,temp,n,c1=0,c2=0;
cout<<"enter the number of elements in array A ";
cin>>n;
int a[n];
// Generates and check how many odd and even numbers in A
for(i=0;i<n;i++)
{
a[i]=rand() % 1000;
if(a[i]%2==1)
c1++;
else
c2++;
}
// Move all odd numbers to array b and even number to array c
int b[c1],c[c2],k=0,s=0;
for(i=0;i<n;i++)

if(a[i]%2==1)
{
b[k]=a[i];
k++;
}
else
{
c[s]=a[i];
s++;
}
//Sort odd numbers
for(i=0;i<c1;i++)
{ min=b[i];
for(j=i+1;j<c1;j++)
if(min>b[j])
{
min=b[j];
b[j]=b[i];
b[i]=min;
}
}

//Sort even numbers


for(i=1;i<c2-1;i++)
for(j=0;j<c2-i; j++)
if(c[j]>c[j+1])
{ temp=c[j];
c[j]=c[j+1];
c[j+1]=temp;
}
// print odd numbers
for(i=0;i<c1;i++)
cout<<b[i]<<" ";
cout<<endl;
// print even numbers
for(i=0;i<c2;i++)
cout<<c[i]<<" ";

return 0;
}

You might also like