Know Some Sorting Techniques

Revision en1, by parth_1818, 2023-08-13 08:38:03

Today, I will discuss Some important Sorting Techniques with their code. 1)Selection Sort Explanation: We will select minimum among all unsorted elements and swap it with first unsorted element. Time complexity: O(N^2)

int n=5;
int arr[]={9,1,5,6,3};
for(int i=0 ; i<5 ; i++){
  int pos=0,mn=1e5;
  for(int j=i ; j<n ; j++){
    if(arr[j]<mn){
      pos=j;
      mn=arr[j];
    }
  }
  swap(arr[i],arr[pos]);
  }
for(int i=0 ; i<n ; i++){
 cout<<arr[i]<<' ';
}

2)Bubble Sort Explanation: We will send largest element to last by repeatedly swapping the adjacent elements if they are in the wrong order. tome complexity O(N^2) int n=5; int arr[]={9,1,5,6,3}; for(int i=n-1 ; i>=0 ; i--){ for(int j=0 ; j<i ; j++){ if(arr[j]>arr[j+1]) swap(arr[j],arr[j+1]); } } for(int i=0 ; i<n ; i++) cout<<arr[i]<<' ';

Tags sorting algorithms, sorting, c++

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English parth_1818 2024-01-08 17:45:47 7650 Tiny change: 'duction:\n\nSortin' -> 'duction:\n=============\n\nSortin' (published)
en1 English parth_1818 2023-08-13 08:38:03 898 Initial revision (saved to drafts)