Sorting Algorithms are ways to implement an array or list as an input and arrange the items into
a particular order.
Some of the most common sorting algorithms are:
• Bubble sort
• Selection sort
• Insertion sort
• Merge sort
• Quick sort
• Heap sort
• Radix sort
Bubble sort is a comparison sort technique, where pairs of elements are compared at a time,
across multiple iterations; and in the process, smaller (or larger) elements “bubble up” to the top
of the list until the whole list is sorted.
//Recursive solution for Bubble Sort
#include<bits/stdc++.h>
using namespace std;
void Bubblesort(int input[],int endIndex){
if(endIndex == 0)
return;
for(int i=0;i<endIndex;i++){
if(input[i]>input[i+1]){ //if(input[i]<input[i+1]) for decending order
int temp = input[i];
input[i]=input[i+1];
input[i+1]=temp;
}
}
Bubblesort(input,endIndex-1);
}
, int main()
{
int arr[]={-2,45,0,11,-9};
Bubblesort(arr,4);
for(int i=0;i<5;i++){
cout<<arr[i]<<endl;
}
}
//OUTPUT
[Success] Your code was executed successfully
-9
-2
0
11
45
Selection sort is an in-place sort technique, which divides the input into a sorted region and an
unsorted region. In each iteration, the algorithm picks an appropriate element from the unsorted
region and appends it to the sorted region
img src: https://miro.medium.com/max/463/0*J2ta_c6YA870MKor
//Recursive solution for selection sort
#include<bits/stdc++.h>
using namespace std;
void Selectionsort(int input[],int beginIndex, int size){
if(beginIndex >= size-1)
return;
int minIndex = beginIndex;
for(int i=beginIndex; i<size; i++){
if(input[i]<input[minIndex]){
minIndex = i;
}
}
int temp = input[minIndex];
input[minIndex]=input[beginIndex];
input[beginIndex]=temp;
a particular order.
Some of the most common sorting algorithms are:
• Bubble sort
• Selection sort
• Insertion sort
• Merge sort
• Quick sort
• Heap sort
• Radix sort
Bubble sort is a comparison sort technique, where pairs of elements are compared at a time,
across multiple iterations; and in the process, smaller (or larger) elements “bubble up” to the top
of the list until the whole list is sorted.
//Recursive solution for Bubble Sort
#include<bits/stdc++.h>
using namespace std;
void Bubblesort(int input[],int endIndex){
if(endIndex == 0)
return;
for(int i=0;i<endIndex;i++){
if(input[i]>input[i+1]){ //if(input[i]<input[i+1]) for decending order
int temp = input[i];
input[i]=input[i+1];
input[i+1]=temp;
}
}
Bubblesort(input,endIndex-1);
}
, int main()
{
int arr[]={-2,45,0,11,-9};
Bubblesort(arr,4);
for(int i=0;i<5;i++){
cout<<arr[i]<<endl;
}
}
//OUTPUT
[Success] Your code was executed successfully
-9
-2
0
11
45
Selection sort is an in-place sort technique, which divides the input into a sorted region and an
unsorted region. In each iteration, the algorithm picks an appropriate element from the unsorted
region and appends it to the sorted region
img src: https://miro.medium.com/max/463/0*J2ta_c6YA870MKor
//Recursive solution for selection sort
#include<bits/stdc++.h>
using namespace std;
void Selectionsort(int input[],int beginIndex, int size){
if(beginIndex >= size-1)
return;
int minIndex = beginIndex;
for(int i=beginIndex; i<size; i++){
if(input[i]<input[minIndex]){
minIndex = i;
}
}
int temp = input[minIndex];
input[minIndex]=input[beginIndex];
input[beginIndex]=temp;