Skip to content

voyager2005/java-sorting-techniques

Repository files navigation

java-sorting-techniques

This REPO contains some commonly used array sorting techniques
Please ⭐ this repository if you found it helpful so that more people can benifit from this code 😃.

Get current updates open issues and pull requests in this repository

This is the selection sort technique. You can find the whole program including accepting the array in this repo.

//selection sort techinique
for(int i = 0 ; i < n-1 ; i++)
{
  //initialization 
  smallest = a[i] ; 
  indexOfSmallest = i ; 
  
  for(int j = i+1 ; j < n ; j++)
  {
    if(a[j] < smallest)
    {
      smallest = a[j]; 
      indexOfSmallest = j ; 
    }
  }
            
  //swpping the array
  temp = a[i];
  a[i] = a[indexOfSmallest];
  a[indexOfSmallest] = temp;
}

image8 1 0

This is the bubble sort technique. You can find the whole program including accepting the array in this repo.

//bubble sort techinique 
for(int i = 0 ;  i < n-1 ; i++)
{
  for(int j = 0 ; j < n-i-1 ; j++)
  {
    if(a[j] > a[j+1])
    {
      temp = a[j] ;
      a[j] = a[j+1];
      a[j+1] = temp; 
    }
  }
}

image8 1 1