Sorting is used to organize data, enable faster searches, generate reports, manage databases, process large datasets.
Note
Use the table of contents to navigate to the portion that you are interested in.
1. Introduction
Sorting is a process of arranging data in a specific order. ususally ascending or descending. It helps to find elements faster. also it organise data for better understanding.
This visualizer gives you the clear understanding of all the sorting algos. How it works internally and how the elements are being compared everything.
3. Bubble sort
Bubble sort is a simple comparison-based sorting algorithm. it repeatedly compares adjacent elements and swap them if they are in the wrong order. In each iteration the largest element bubbles up to the end of the list. You can visualize it above in the visualizer.
#include<iostream>usingnamespacestd;voidbubbleSort(intarr[],intn){for(inti=0;i<n-1;i++){for(intj=0;j<n-i-1;j++){if(arr[j]>arr[j+1]){swap(arr[j],arr[j+1]);}}}}// Example Use
intmain(){intarr[]={10,7,8,9,1,5};intn=sizeof(arr)/sizeof(arr[0]);bubbleSort(arr,n);for(inti=0;i<n;i++){cout<<arr[i]<<" ";}return0;}
// javascript code
functionbubbleSort(arr){constn=arr.length;for(leti=0;i<n-1;i++){for(letj=0;j<n-i-1;j++){if(arr[j]>arr[j+1]){lettemp=arr[j];arr[j]=arr[j+1];arr[j+1]=temp;}}}returnarr;}// example use
letarr=[10,7,8,9,1,5];bubbleSort(arr);console.log(arr);
// java codepublicstaticvoidbubbleSort(int[]arr){intn=arr.length;for(inti=0;i<n-1;i++){for(intj=0;j<n-i-1;j++){if(arr[j]>arr[j+1]){inttemp=arr[j];arr[j]=arr[j+1];arr[j+1]=temp;}}}}// example usepublicclassMain{publicstaticvoidmain(String[]args){int[]arr={10,7,8,9,1,5};bubbleSort(arr);for(intnum:arr){System.out.print(num+" ");}}}
Bubble Sort in Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# python codedefbubbleSort(arr):n=len(arr)foriinrange(n-1):forjinrange(n-i-1):ifarr[j]>arr[j+1]:# swaparr[j],arr[j+1]=arr[j+1],arr[j]returnarr# Example usearr=[10,7,8,9,1,5]bubbleSort(arr)print(arr)
4. Insertion Sort
Insertion Sort is a simple comparison-based sorting algo. it builds the sorted list one element at a time. It works by taking each element and inserting it into its correct postion within the already sorted part of the array.
#include<iostream>usingnamespacestd;voidinsertionSort(intarr[],intn){for(inti=1;i<n;i++){intvalue=arr[i];intj=i-1;while(j>=0&&arr[j]>value){arr[j+1]=arr[j];j--;}arr[j+1]=value;}}// Example use
intmain(){intarr[]={10,7,8,9,1,5};intn=sizeof(arr)/sizeof(arr[0]);insertionSort(arr,n);for(inti=0;i<n;i++){cout<<arr[i]<<" ";}return0;}
// javascript code
functioninsertionSort(arr){constn=arr.length;for(leti=1;i<n;i++){letvalue=arr[i];letj=i-1;while(j>=0&&arr[j]>value){arr[j+1]=arr[j];j--;}arr[j+1]=value;}returnarr;}// example use
letarr=[10,7,8,9,1,5];insertionSort(arr);console.log(arr);
# python codedefinsertionSort(arr):n=len(arr)foriinrange(1,n):value=arr[i]j=i-1whilej>=0andarr[j]>value:arr[j+1]=arr[j]j-=1arr[j+1]=valuereturnarr# Example usearr=[10,7,8,9,1,5]insertionSort(arr)print(arr)
5. Selection Sort
Selection Sort is a simple comparison-based sorting algorithm it works by repeatedly selecting the smallest (or largest) element from the unsorted part of the array and placing it at the correct position.It maintains two subarrays. a sorted part and an unsorted part. It is easy to understand and implement.
#include<iostream>usingnamespacestd;// Selection Sort function
voidselectionSort(intarr[],intlength){for(inti=0;i<length;i++){intminIndex=i;for(intj=i+1;j<length;j++){if(arr[minIndex]>arr[j]){minIndex=j;}}if(minIndex!=i){swap(arr[i],arr[minIndex]);}}}// Example use
intmain(){intarr[]={10,7,8,9,1,5};intn=sizeof(arr)/sizeof(arr[0]);selectionSort(arr,n);for(inti=0;i<n;i++){cout<<arr[i]<<" ";}return0;}
// javascript code
functionselectionSort(arr){constn=arr.length;for(leti=0;i<n;i++){letminIndex=i;for(letj=i+1;j<n;j++){if(arr[minIndex]>arr[j]){minIndex=j;}}if(minIndex!=i){// swap
lettemp=arr[i];arr[i]=arr[minIndex];arr[minIndex]=temp;}}returnarr;}// example use
letarr=[10,7,8,9,1,5];selectionSort(arr);console.log(arr);
# python codedefselectionSort(arr):n=len(arr)foriinrange(n):minIndex=iforjinrange(i+1,n):ifarr[minIndex]>arr[j]:minIndex=j;ifminIndex!=i:# swaparr[j],arr[j+1]=arr[j+1],arr[j]returnarr# Example usearr=[10,7,8,9,1,5]selectionSort(arr)print(arr)
6. Quick Sort
Quick Sort is a divide-and-conquer sorting algorithm that picks a pivot element, partitions the array into elements less than and greater than the pivot and then recursively sorts the partitions. Generally it is unstable.
#include<iostream>usingnamespacestd;intpartition(intarr[],intlow,inthigh){intpivot=arr[low];inti=low+1;intj=high;while(true){while(i<=high&&arr[i]<=pivot){i++;}while(arr[j]>pivot){j--;}if(i<j){swap(arr[i],arr[j]);}else{break;}}swap(arr[low],arr[j]);returnj;}voidquickSort(intarr[],intlow,inthigh){if(low<high){intp=partition(arr,low,high);quickSort(arr,low,p-1);quickSort(arr,p+1,high);}}// example use
intmain(){intarr[]={10,7,8,9,1,5};intn=sizeof(arr)/sizeof(arr[0]);quickSort(arr,0,n-1);for(inti=0;i<n;i++){cout<<arr[i]<<" ";}return0;}
// javascript code
functionpartition(arr,low,high){letpivot=arr[low];leti=low+1;letj=high;while(true){while(i<=high&&arr[i]<=pivot){i++;}while(arr[j]>pivot){j--;}if(i<j){lettemp=arr[i];arr[i]=arr[j];arr[j]=temp;}else{break;}}lettemp=arr[low];arr[low]=arr[j];arr[j]=temp;returnj;}functionquickSort(arr,low,high){if(low<high){letpartitionIndex=partition(arr,low,high);quickSort(arr,low,partitionIndex-1);quickSort(arr,partitionIndex+1,high);}}// example use
letarr=[10,7,8,9,1,5];quickSort(arr,0,arr.length-1);console.log(arr);
# python codedefpartition(arr,low,high):pivot=arr[low]i=low+1j=highwhileTrue:whilei<=highandarr[i]<=pivot:i+=1whilearr[j]>pivot:j-=1ifi<j:arr[i],arr[j]=arr[j],arr[i]else:breakarr[low],arr[j]=arr[j],arr[low]returnjdefquickSort(arr,low,high):iflow<high:p=partition(arr,low,high)quick_sort(arr,low,p-1)quick_sort(arr,p+1,high)# Example usearr=[10,7,8,9,1,5]quickSort(arr,0,len(arr)-1)print(arr)
7. Merge Sort
Merge Sort is a divide-and-conquer algorithm it sorts an array by spliting it into two halves, sorting each half recursively and then merging the sorted halves. it is stable also it is efficient for large datasets. also it recquires extra memory for merging.
// 5. Merge Sort
#include<stdio.h>intarr[]={12,6,47,65,38,24,98,75,82,13,44,37};// global array Decleration for all the sorting Algorithum
voidmerge(int*arr,ints,inte){intmid=(s+e)/2;// finding midpoint of the array
intlen1=mid-s+1;//length of left part of the array
intlen2=e-mid;// length of right part of the array
// dynamic memory allocate
int*first=newint[len1];// left array
int*second=newint[len2];// right array
// copy values in left part
intmainArrayIndex=s;for(inti=0;i<len1;i++){first[i]=arr[mainArrayIndex++];}// copy values in right part
mainArrayIndex=mid+1;for(inti=0;i<len2;i++){second[i]=arr[mainArrayIndex++];}//merging 2 sorted arrays
intindex1=0;intindex2=0;mainArrayIndex=s;while(index1<len1&&index2<len2){if(first[index1]<second[index2]){arr[mainArrayIndex++]=first[index1++];}else{arr[mainArrayIndex++]=second[index2++];}}while(index1<len1){arr[mainArrayIndex++]=first[index1++];}while(index2<len2){arr[mainArrayIndex++]=second[index2++];}// free the memory
delete[]first;delete[]second;}voidmergesort(int*arr,ints,inte){// base case
if(s>=e){return;}// mid point
intmid=(s+e)/2;// sorting left part
mergesort(arr,s,mid);// sorting right part
mergesort(arr,mid+1,e);// merging sorted arrays
merge(arr,s,e);}// For Displaying the sorted array or unsorted also
voidDisplay(int*arr,intlength){for(inti=0;i<length;i++){printf("%d ",arr[i]);}}intmain(){// int arr[10]= {1,34,32,25,76,68,45,98,23,44};
intn=12;mergesort(arr,0,n-1);Display(arr,n);}
// cpp code
#include<bits/stdc++.h>usingnamespacestd;voidmerge(intarr[],ints,inte){intmid=s+(e-s)/2;intlen1=mid-s+1;intlen2=e-mid;int*first=newint[len1];int*second=newint[len2];// Copy left part
intmainArrayIndex=s;for(inti=0;i<len1;i++){first[i]=arr[mainArrayIndex++];}// Copy right part
mainArrayIndex=mid+1;for(inti=0;i<len2;i++){second[i]=arr[mainArrayIndex++];}// Merge two sorted arrays
intindex1=0,index2=0;mainArrayIndex=s;while(index1<len1&&index2<len2){if(first[index1]<second[index2]){arr[mainArrayIndex++]=first[index1++];}else{arr[mainArrayIndex++]=second[index2++];}}while(index1<len1){arr[mainArrayIndex++]=first[index1++];}while(index2<len2){arr[mainArrayIndex++]=second[index2++];}// Free memory
delete[]first;delete[]second;}voidmergeSort(intarr[],ints,inte){if(s>=e){return;}intmid=s+(e-s)/2;mergeSort(arr,s,mid);mergeSort(arr,mid+1,e);merge(arr,s,e);}intmain(){intarr[]={12,6,47,65,38,24,98,75,82,13,44,37};intn=sizeof(arr)/sizeof(arr[0]);mergeSort(arr,0,n-1);for(inti=0;i<n;i++){cout<<arr[i]<<" ";}return0;}
// java codepublicclassMergeSort{publicstaticvoidmerge(int[]arr,ints,inte){intmid=s+(e-s)/2;intlen1=mid-s+1;intlen2=e-mid;int[]first=newint[len1];int[]second=newint[len2];// Copy left partintmainArrayIndex=s;for(inti=0;i<len1;i++){first[i]=arr[mainArrayIndex++];}// Copy right partmainArrayIndex=mid+1;for(inti=0;i<len2;i++){second[i]=arr[mainArrayIndex++];}// Merge two sorted arraysintindex1=0;intindex2=0;mainArrayIndex=s;while(index1<len1&&index2<len2){if(first[index1]<second[index2]){arr[mainArrayIndex++]=first[index1++];}else{arr[mainArrayIndex++]=second[index2++];}}while(index1<len1){arr[mainArrayIndex++]=first[index1++];}while(index2<len2){arr[mainArrayIndex++]=second[index2++];}}publicstaticvoidmergeSort(int[]arr,ints,inte){if(s>=e){return;}intmid=s+(e-s)/2;mergeSort(arr,s,mid);mergeSort(arr,mid+1,e);merge(arr,s,e);}// Example usepublicstaticvoidmain(String[]args){int[]arr={12,6,47,65,38,24,98,75,82,13,44,37};mergeSort(arr,0,arr.length-1);for(intnum:arr){System.out.print(num+" ");}}}
// javascript code
functionmerge(arr,s,e){constmid=Math.floor(s+(e-s)/2);constleft=arr.slice(s,mid+1);constright=arr.slice(mid+1,e+1);leti=0,j=0,k=s;// Merge the two sorted arrays
while(i<left.length&&j<right.length){if(left[i]<right[j]){arr[k++]=left[i++];}else{arr[k++]=right[j++];}}// Copy remaining elements
while(i<left.length){arr[k++]=left[i++];}while(j<right.length){arr[k++]=right[j++];}}functionmergeSort(arr,s,e){if(s>=e)return;constmid=Math.floor(s+(e-s)/2);mergeSort(arr,s,mid);mergeSort(arr,mid+1,e);merge(arr,s,e);}// Example usage
letarr=[12,6,47,65,38,24,98,75,82,13,44,37];mergeSort(arr,0,arr.length-1);console.log(arr);
# python codedefmerge(arr,s,e):mid=s+(e-s)//2# Left and right subarraysleft=arr[s:mid+1]right=arr[mid+1:e+1]i=j=0k=s# Merge the two sorted halveswhilei<len(left)andj<len(right):ifleft[i]<right[j]:arr[k]=left[i]i+=1else:arr[k]=right[j]j+=1k+=1# Copy remaining elementswhilei<len(left):arr[k]=left[i]i+=1k+=1whilej<len(right):arr[k]=right[j]j+=1k+=1defmerge_sort(arr,s,e):ifs>=e:returnmid=s+(e-s)//2merge_sort(arr,s,mid)merge_sort(arr,mid+1,e)merge(arr,s,e)# Example usagearr=[12,6,47,65,38,24,98,75,82,13,44,37]merge_sort(arr,0,len(arr)-1)print(arr)
8. Comparison
Algorithm
Best Use Case / Pros
Cons / Limitations
Bubble Sort
Simple, easy to implement, works for small or nearly sorted arrays
Very slow for large arrays, O(n²) time
Insertion Sort
Efficient for small or nearly sorted arrays
O(n²) time for large arrays
Selection Sort
Simple, small datasets, in-place
Always O(n²), inefficient for large arrays, unstable
Merge Sort
Large datasets, stable sort, predictable O(n log n) time
Uses extra memory O(n)
Quick Sort
Very fast on average, in-place O(log n) space, good cache performance
Worst case O(n²), unstable, pivot choice matters
Which Sorting algo should we use:
Small and nearly sorted data: Insertion sort
Large datasets: Merge Sort/ Quick sort
Memory COnstraints: Heap Sort
Integer Data with small range: Counting Sort
General Purpose: Quick Sort
Thanks for reading…Don’t forget to leave a comment. 👇