Sorting

Imagine, you have a list of 1000 names taken randomly. Only when you’ll arrange it or sort it, you’ll easily search a specific name from the list. You can easily do so in Java. 

Types Of Sorting:

There are 5 types of sorting. The easiest of them are:

1. Selection Sort:

Suppose,  you want to arrange numbers in ascending order from unsorted data. In this sort, the smallest numbers are placed first. This is how selection sort does it:

Let the array be:

It finds the least value in the array and swap it with 0th position element.

It finds the next least value in the array and swap it with 1st position element.

Now it finds the next least value and swap it with 2nd position element.

Then it finds the next least value and swap it with 3rd position element.

Finally it finds the next least value and swap it with 4th position element.

Hence, the largest element remains at the end.

Program For Selection Sort:

public class Selection_sort
{
    public static void main()
    {
        int arr[]={30,20,10,60,50,40},min=0,e=0;
        //min to store minimum value in the array
        //e to store the swapping element
       for(int i=0;i<5;i++)
       {
           min=i;
           for(int j=i+1;j<6;j++)
           {
                if(arr[j]<arr[min])
                min=j;
            }
            e=arr[i];
            arr[i]=arr[min];
            arr[min]=e;
        }
        for(int i=0;i<6;i++)
        System.out.print(arr[i]+” “);
    }
}

This program will arrange the given unsorted array and print it.

2. Bubble Sort:

The coding of this sort is shorter than any other sort but it is also system heavy sorting process. You should see the process to discover why:

Let the array be:

Comparison begins:

0th element > 1st element. Swap. 

1st element > 2nd element. Swap. 

2nd element < 3rd element. No Swap. 

3rd element > 4th element. Swap. 

4th element > 5th element. Swap. 

This is the array after one iteration. Highest element is pushed in the last position. Similary, the process can be applied to push the second highest number to the second last position and so on…

Program For Bubble Sort:

public class Bubble_sort
{
    public static void main()
    {
        int arr[]={30,20,10,60,50,40},e=0;
        //e stores swapping element
        for(int i=0;i<5;i++)
        {
            for(int j=0;j<(5-i);j++)
            {
                if(arr[j]>arr[j+1])
                {
                    e=arr[j];
                    arr[j]=arr[j+1];
                    arr[j+1]=e;
                }
            }
        }
        for(int i=0;i<6;i++)
        System.out.print(arr[i]+” “);
    }
}

This program will arrange the given unsorted array and print it.

Conclusion:

You can sort data easily with what you’ve learnt just now. These two sorts have the easiest code of the five but are heavy on computer. The rest have long and difficult codes but are easy on computer. 

As you’re a beginner, selection sort and bubble sort are better for you. When you have covered all the basics, it’ll be easier for you to understand other types of sorts.